From 6d924f74a37f85c49bdf04cda7c76fdf75a0613a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20Delr=C3=A9?= Date: Thu, 14 May 2026 11:54:36 +0200 Subject: [PATCH] fix: read chip ID with single byte read from 0xD0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The official Bosch SensorAPI reads only 1 byte from register 0xD0. Register 0xD1 is undocumented — reading it was a leftover from the original Matt Hawkins driver. Return (chip_id, 0) to preserve the existing tuple API without breaking callers. Update tests to assert read_byte_data is used and called with 0xD0. Co-authored-by: agilicode --- bme280.py | 4 ++-- tests/test_bme280.py | 22 +++++++++++----------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/bme280.py b/bme280.py index 20dc1bc..af2c7cb 100644 --- a/bme280.py +++ b/bme280.py @@ -32,8 +32,8 @@ def _get_uchar(data: list[int], index: int) -> int: def read_id(addr: int = DEVICE_ADDRESS) -> tuple[int, int]: with smbus2.SMBus(I2C_BUS) as bus: - chip_id, chip_version = bus.read_i2c_block_data(addr, 0xD0, 2) - return chip_id, chip_version + chip_id = bus.read_byte_data(addr, 0xD0) + return chip_id, 0 def _wait_nvm_copy(bus: smbus2.SMBus, addr: int) -> None: diff --git a/tests/test_bme280.py b/tests/test_bme280.py index cc7ce23..fbffb2c 100644 --- a/tests/test_bme280.py +++ b/tests/test_bme280.py @@ -3,15 +3,14 @@ import pytest -def make_mock_bus(chip_id: int = 96, chip_version: int = 0) -> MagicMock: +def make_mock_bus(chip_id: int = 96) -> MagicMock: bus = MagicMock() - bus.read_byte_data.return_value = 0 # 0xF3 status: NVM copy done, not measuring + bus.read_byte_data.side_effect = [chip_id, 0, 0] # read_id, NVM done, not measuring bus.read_i2c_block_data.side_effect = [ - [chip_id, chip_version], # read_id - [0] * 24, # cal1 - T and P calibration - [0], # cal2 - H1 - [0] * 7, # cal3 - H2-H6 - [0] * 8, # raw sensor data + [0] * 24, # cal1 - T and P calibration + [0], # cal2 - H1 + [0] * 7, # cal3 - H2-H6 + [0] * 8, # raw sensor data ] return bus @@ -64,11 +63,11 @@ def test_read_id_returns_chip_id_and_version(patched_smbus): import bme280 chip_id, chip_version = bme280.read_id() assert chip_id == 96 - assert chip_version == 0 + assert chip_version == 0 # always 0 — register 0xD1 is undocumented -def test_read_id_custom_chip_id(): - bus = make_mock_bus(chip_id=96, chip_version=0) +def test_read_id_uses_single_byte_read(): + bus = make_mock_bus(chip_id=96) with patch('bme280.smbus2.SMBus') as MockSMBus: instance = MockSMBus.return_value instance.__enter__ = MagicMock(return_value=bus) @@ -76,6 +75,7 @@ def test_read_id_custom_chip_id(): import bme280 chip_id, _ = bme280.read_id() assert chip_id == 96 + bus.read_byte_data.assert_called_with(bme280.DEVICE_ADDRESS, 0xD0) # --- sensor() structure --- @@ -112,7 +112,7 @@ def test_pressure_zero_when_p1_calibration_is_zero(patched_smbus): def test_nvm_copy_timeout_raises_oserror(): bus = MagicMock() - bus.read_byte_data.return_value = 0x01 # NVM copy never completes + bus.read_byte_data.side_effect = [0x01] * 10 # NVM copy never completes with patch('bme280.smbus2.SMBus') as MockSMBus: instance = MockSMBus.return_value instance.__enter__ = MagicMock(return_value=bus)