Skip to content

Commit

Permalink
Merge branch 'main' into 168-add-sok-bms-support
Browse files Browse the repository at this point in the history
  • Loading branch information
patman15 committed Mar 2, 2025
2 parents 48e71f2 + 2c52f33 commit fd540d9
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 16 deletions.
3 changes: 0 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
{
"python.testing.pytestArgs": [
"tests","--no-cov"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
}
5 changes: 3 additions & 2 deletions custom_components/bms_ble/plugins/daly_bms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
5 changes: 3 additions & 2 deletions custom_components/bms_ble/plugins/ective_bms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
3 changes: 1 addition & 2 deletions custom_components/bms_ble/plugins/ej_bms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
9 changes: 8 additions & 1 deletion custom_components/bms_ble/plugins/jikong_bms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
3 changes: 1 addition & 2 deletions custom_components/bms_ble/plugins/redodo_bms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
2 changes: 1 addition & 1 deletion hacs.json
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,4 @@ load-plugins = [
[tool.pylint."MESSAGES CONTROL"]
per-file-ignores = [
"/tests/:protected-access",
]
]
9 changes: 8 additions & 1 deletion tests/test_fuzzing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/test_ogt_bms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit fd540d9

Please sign in to comment.