|
| 1 | +import sys |
| 2 | +import mock |
| 3 | +import pytest |
| 4 | +import bme680 |
| 5 | +from bme680.constants import CalibrationData |
| 6 | + |
| 7 | + |
| 8 | +class MockSMBus: |
| 9 | + """Mock a basic non-presence SMBus device to cause BME680 to fail. |
| 10 | +
|
| 11 | + Returns 0 in all cases, so that CHIP_ID will never match. |
| 12 | +
|
| 13 | + """ |
| 14 | + |
| 15 | + def __init__(self, bus): # noqa D107 |
| 16 | + pass |
| 17 | + |
| 18 | + def read_byte_data(self, addr, register): |
| 19 | + """Return 0 for all read attempts.""" |
| 20 | + return 0 |
| 21 | + |
| 22 | + |
| 23 | +class MockSMBusPresent: |
| 24 | + """Mock enough of the BME680 for the library to initialise and test.""" |
| 25 | + |
| 26 | + def __init__(self, bus): |
| 27 | + """Initialise with test data.""" |
| 28 | + self.regs = [0 for _ in range(256)] |
| 29 | + self.regs[bme680.CHIP_ID_ADDR] = bme680.CHIP_ID |
| 30 | + |
| 31 | + def read_byte_data(self, addr, register): |
| 32 | + """Read a single byte from fake registers.""" |
| 33 | + return self.regs[register] |
| 34 | + |
| 35 | + def write_byte_data(self, addr, register, value): |
| 36 | + """Write a single byte to fake registers.""" |
| 37 | + self.regs[register] = value |
| 38 | + |
| 39 | + def read_i2c_block_data(self, addr, register, length): |
| 40 | + """Read up to length bytes from register.""" |
| 41 | + return self.regs[register:register + length] |
| 42 | + |
| 43 | + |
| 44 | +@pytest.fixture(scope='function', autouse=False) |
| 45 | +def smbus_notpresent(): |
| 46 | + """Mock smbus module.""" |
| 47 | + smbus = mock.MagicMock() |
| 48 | + smbus.SMBus = MockSMBus |
| 49 | + sys.modules['smbus'] = smbus |
| 50 | + yield smbus |
| 51 | + del sys.modules['smbus'] |
| 52 | + |
| 53 | + |
| 54 | +@pytest.fixture(scope='function', autouse=False) |
| 55 | +def smbus(): |
| 56 | + """Mock smbus module.""" |
| 57 | + smbus = mock.MagicMock() |
| 58 | + smbus.SMBus = MockSMBusPresent |
| 59 | + sys.modules['smbus'] = smbus |
| 60 | + yield smbus |
| 61 | + del sys.modules['smbus'] |
| 62 | + |
| 63 | + |
| 64 | +@pytest.fixture(scope='function', autouse=False) |
| 65 | +def calibration(): |
| 66 | + """Mock bme680 calibration.""" |
| 67 | + calibration = CalibrationData() |
| 68 | + # Dump of calibration data borrowed from: |
| 69 | + # https://github.com/pimoroni/bme680-python/issues/11 |
| 70 | + data = { |
| 71 | + 'par_gh1': -30, |
| 72 | + 'par_gh2': -24754, |
| 73 | + 'par_gh3': 18, |
| 74 | + 'par_h1': 676, |
| 75 | + 'par_h2': 1029, |
| 76 | + 'par_h3': 0, |
| 77 | + 'par_h4': 45, |
| 78 | + 'par_h5': 20, |
| 79 | + 'par_h6': 120, |
| 80 | + 'par_h7': -100, |
| 81 | + 'par_p1': 36673, |
| 82 | + 'par_p10': 30, |
| 83 | + 'par_p2': -10515, |
| 84 | + 'par_p3': 88, |
| 85 | + 'par_p4': 7310, |
| 86 | + 'par_p5': -129, |
| 87 | + 'par_p6': 30, |
| 88 | + 'par_p7': 46, |
| 89 | + 'par_p8': -3177, |
| 90 | + 'par_p9': -2379, |
| 91 | + 'par_t1': 26041, |
| 92 | + 'par_t2': 26469, |
| 93 | + 'par_t3': 3, |
| 94 | + 'range_sw_err': 0, |
| 95 | + 'res_heat_range': 1, |
| 96 | + 'res_heat_val': 48, |
| 97 | + 't_fine': 136667 |
| 98 | + } |
| 99 | + for k, v in data.items(): |
| 100 | + setattr(calibration, k, v) |
| 101 | + return calibration |
0 commit comments