Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bme280.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
22 changes: 11 additions & 11 deletions tests/test_bme280.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -64,18 +63,19 @@ 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)
instance.__exit__ = MagicMock(return_value=False)
import bme280
chip_id, _ = bme280.read_id()
assert chip_id == 96
bus.read_byte_data.assert_called_with(bme280.DEVICE_ADDRESS, 0xD0)


# --- sensor() structure ---
Expand Down Expand Up @@ -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)
Expand Down
Loading