|
6 | 6 | from bleak.backends.characteristic import BleakGATTCharacteristic
|
7 | 7 | from bleak.exc import BleakError
|
8 | 8 | from bleak.uuids import normalize_uuid_str
|
| 9 | +import pytest |
9 | 10 |
|
10 | 11 | from custom_components.bms_ble.plugins.jbd_bms import BMS
|
11 | 12 |
|
@@ -62,34 +63,6 @@ async def write_gatt_char(
|
62 | 63 | self._notify_callback("MockJBDBleakClient", notify_data)
|
63 | 64 |
|
64 | 65 |
|
65 |
| -class MockInvalidBleakClient(MockJBDBleakClient): |
66 |
| - """Emulate a JBD BMS BleakClient returning wrong data.""" |
67 |
| - |
68 |
| - def _response( |
69 |
| - self, char_specifier: BleakGATTCharacteristic | int | str | UUID, data: Buffer |
70 |
| - ) -> bytearray: |
71 |
| - if ( |
72 |
| - isinstance(char_specifier, str) |
73 |
| - and normalize_uuid_str(char_specifier) == normalize_uuid_str("ff02") |
74 |
| - and bytearray(data)[0] == self.HEAD_CMD |
75 |
| - ): |
76 |
| - if bytearray(data)[1:3] == self.CMD_INFO: |
77 |
| - return bytearray( # wrong end |
78 |
| - b"\xdd\x03\x00\x1D\x06\x18\xFE\xE1\x01\xF2\x01\xF4\x00\x2A\x2C\x7C\x00\x00\x00" |
79 |
| - b"\x00\x00\x00\x80\x64\x03\x04\x03\x0B\x8B\x0B\x8A\x0B\x84\xf8\x84\xdd" |
80 |
| - ) |
81 |
| - |
82 |
| - return ( # wrong CRC |
83 |
| - bytearray(b"\xdd\x03\x00\x1d") + bytearray(31) + bytearray(b"\x77") |
84 |
| - ) |
85 |
| - |
86 |
| - return bytearray() |
87 |
| - |
88 |
| - async def disconnect(self) -> bool: |
89 |
| - """Mock disconnect to raise BleakError.""" |
90 |
| - raise BleakError |
91 |
| - |
92 |
| - |
93 | 66 | class MockOversizedBleakClient(MockJBDBleakClient):
|
94 | 67 | """Emulate a JBD BMS BleakClient returning wrong data length."""
|
95 | 68 |
|
@@ -166,19 +139,45 @@ async def test_update(monkeypatch, reconnect_fixture) -> None:
|
166 | 139 | await bms.disconnect()
|
167 | 140 |
|
168 | 141 |
|
169 |
| -async def test_invalid_response(monkeypatch) -> None: |
| 142 | +@pytest.fixture( |
| 143 | + name="wrong_response", |
| 144 | + params=[ |
| 145 | + bytearray( # wrong end |
| 146 | + b"\xdd\x03\x00\x1D\x06\x18\xFE\xE1\x01\xF2\x01\xF4\x00\x2A\x2C\x7C\x00\x00\x00" |
| 147 | + b"\x00\x00\x00\x80\x64\x03\x04\x03\x0B\x8B\x0B\x8A\x0B\x84\xf8\x84\xdd" |
| 148 | + ), |
| 149 | + bytearray(b"\xdd\x04\x00\x1d") |
| 150 | + + bytearray(31) |
| 151 | + + bytearray(b"\x77"), # wrong CRC |
| 152 | + ], |
| 153 | +) |
| 154 | +def response(request) -> bytearray: |
| 155 | + """Return all possible BMS variants.""" |
| 156 | + return request.param |
| 157 | + |
| 158 | + |
| 159 | +async def test_invalid_response(monkeypatch, wrong_response) -> None: |
170 | 160 | """Test data update with BMS returning invalid data (wrong CRC)."""
|
171 | 161 |
|
| 162 | + monkeypatch.setattr( |
| 163 | + "custom_components.bms_ble.plugins.jbd_bms.BAT_TIMEOUT", |
| 164 | + 0.1, |
| 165 | + ) |
| 166 | + |
| 167 | + monkeypatch.setattr( |
| 168 | + "tests.test_jbd_bms.MockJBDBleakClient._response", |
| 169 | + lambda _s, _c_, d: wrong_response, |
| 170 | + ) |
| 171 | + |
172 | 172 | monkeypatch.setattr(
|
173 | 173 | "custom_components.bms_ble.plugins.basebms.BleakClient",
|
174 |
| - MockInvalidBleakClient, |
| 174 | + MockJBDBleakClient, |
175 | 175 | )
|
176 | 176 |
|
177 | 177 | bms = BMS(generate_ble_device("cc:cc:cc:cc:cc:cc", "MockBLEdevice", None, -73))
|
178 | 178 |
|
179 |
| - result = await bms.async_update() |
180 |
| - |
181 |
| - assert result == {} |
| 179 | + with pytest.raises(TimeoutError): |
| 180 | + _result = await bms.async_update() |
182 | 181 |
|
183 | 182 | await bms.disconnect()
|
184 | 183 |
|
|
0 commit comments