diff --git a/.vscode/settings.json b/.vscode/settings.json index 8a25c3f..e137fad 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,7 +1,4 @@ { - "python.testing.pytestArgs": [ - "tests","--no-cov" - ], "python.testing.unittestEnabled": false, "python.testing.pytestEnabled": true } \ No newline at end of file diff --git a/custom_components/bms_ble/plugins/daly_bms.py b/custom_components/bms_ble/plugins/daly_bms.py index 3f8d1c3..fd578fa 100644 --- a/custom_components/bms_ble/plugins/daly_bms.py +++ b/custom_components/bms_ble/plugins/daly_bms.py @@ -114,8 +114,9 @@ def _notification_handler( self._log.debug("response data is invalid") return - crc: Final = crc_modbus(data[:-2]) - if crc != int.from_bytes(data[-2:], byteorder="little"): + if (crc := crc_modbus(data[:-2])) != int.from_bytes( + data[-2:], byteorder="little" + ): self._log.debug( "invalid checksum 0x%X != 0x%X", int.from_bytes(data[-2:], byteorder="little"), diff --git a/custom_components/bms_ble/plugins/ective_bms.py b/custom_components/bms_ble/plugins/ective_bms.py index 8d6b507..f637058 100644 --- a/custom_components/bms_ble/plugins/ective_bms.py +++ b/custom_components/bms_ble/plugins/ective_bms.py @@ -120,8 +120,9 @@ def _notification_handler( self._data.clear() return - crc: Final[int] = BMS._crc(self._data[1 : -BMS._CRC_LEN]) - if crc != int(self._data[-BMS._CRC_LEN :], 16): + if (crc := BMS._crc(self._data[1 : -BMS._CRC_LEN])) != int( + self._data[-BMS._CRC_LEN :], 16 + ): self._log.debug( "invalid checksum 0x%X != 0x%X", int(self._data[-BMS._CRC_LEN :], 16), diff --git a/custom_components/bms_ble/plugins/ej_bms.py b/custom_components/bms_ble/plugins/ej_bms.py index 10be89f..fcf02cb 100644 --- a/custom_components/bms_ble/plugins/ej_bms.py +++ b/custom_components/bms_ble/plugins/ej_bms.py @@ -150,8 +150,7 @@ def _notification_handler( self._data.clear() return - crc: Final = BMS._crc(self._data[1:-3]) - if crc != int(self._data[-3:-1], 16): + if (crc := BMS._crc(self._data[1:-3])) != int(self._data[-3:-1], 16): self._log.debug( "invalid checksum 0x%X != 0x%X", int(self._data[-3:-1], 16), crc ) diff --git a/custom_components/bms_ble/plugins/jikong_bms.py b/custom_components/bms_ble/plugins/jikong_bms.py index 3c35d29..a421a93 100644 --- a/custom_components/bms_ble/plugins/jikong_bms.py +++ b/custom_components/bms_ble/plugins/jikong_bms.py @@ -216,7 +216,14 @@ def _cmd(cmd: bytes, value: list[int] | None = None) -> bytes: @staticmethod def _dec_devinfo(data: bytearray) -> dict[str, str]: - return {"hw_version": data[22:27].decode(), "sw_version": data[30:35].decode()} + fields: Final[dict[str, int]] = { + "hw_version": 22, + "sw_version": 30, + } + return { + key: data[idx : idx + 8].decode(errors="replace").strip("\x00") + for key, idx in fields.items() + } @staticmethod def _cell_voltages(data: bytearray, cells: int) -> dict[str, float]: diff --git a/custom_components/bms_ble/plugins/redodo_bms.py b/custom_components/bms_ble/plugins/redodo_bms.py index 4a91f43..4c7e62c 100644 --- a/custom_components/bms_ble/plugins/redodo_bms.py +++ b/custom_components/bms_ble/plugins/redodo_bms.py @@ -103,8 +103,7 @@ def _notification_handler( self._log.debug("incorrect frame length (%i)", len(data)) return - crc: Final[int] = crc_sum(data[: BMS.CRC_POS]) - if crc != data[BMS.CRC_POS]: + if (crc := crc_sum(data[: BMS.CRC_POS])) != data[BMS.CRC_POS]: self._log.debug( "invalid checksum 0x%X != 0x%X", data[len(data) + BMS.CRC_POS], crc ) diff --git a/hacs.json b/hacs.json index f329c78..cb4b94e 100644 --- a/hacs.json +++ b/hacs.json @@ -1,7 +1,7 @@ { "name": "BLE Battery Management System (BMS)", "filename": "bms_ble.zip", - "homeassistant": "2024.6.0", + "homeassistant": "2025.2.2", "render_readme": true, "zip_release": true } diff --git a/pyproject.toml b/pyproject.toml index 85bcb9b..c7a7bc7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -218,4 +218,4 @@ load-plugins = [ [tool.pylint."MESSAGES CONTROL"] per-file-ignores = [ "/tests/:protected-access", -] \ No newline at end of file +] diff --git a/tests/test_fuzzing.py b/tests/test_fuzzing.py index c2fccc7..1a62f6a 100644 --- a/tests/test_fuzzing.py +++ b/tests/test_fuzzing.py @@ -4,6 +4,7 @@ from types import ModuleType from hypothesis import HealthCheck, given, settings, strategies as st +import pytest from custom_components.bms_ble.plugins.basebms import BaseBMS @@ -18,10 +19,16 @@ max_examples=1000, suppress_health_check=[HealthCheck.function_scoped_fixture] ) async def test_notification_handler( - monkeypatch, plugin_fixture: ModuleType, data: bytearray + monkeypatch, + pytestconfig: pytest.Config, + plugin_fixture: ModuleType, + data: bytearray, ) -> None: """Test the notification handler.""" + if pytestconfig.getoption("--cov") == ["custom_components.bms_ble"]: + pytest.skip("Skipping fuzzing tests due to coverage generation!") + async def patch_init() -> None: return diff --git a/tests/test_ogt_bms.py b/tests/test_ogt_bms.py index 9d0a4ff..c58193f 100644 --- a/tests/test_ogt_bms.py +++ b/tests/test_ogt_bms.py @@ -94,7 +94,7 @@ async def _response( if isinstance(char_specifier, str) and normalize_uuid_str( char_specifier ) == normalize_uuid_str("fff6"): - return bytearray(b"invalid_value") + return bytearray(b"invalid\xF0value") return bytearray()