Skip to content

Commit

Permalink
Filter AT commands at the end of JK messages (#125)
Browse files Browse the repository at this point in the history
* remove AT cmds from end of message
* Update manifest.json
  • Loading branch information
patman15 authored Dec 23, 2024
1 parent ce977bb commit 9911cb3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion custom_components/bms_ble/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,5 @@
"issue_tracker": "https://github.com/patman15/BMS_BLE-HA/issues",
"loggers": ["bleak_retry_connector"],
"requirements": [],
"version": "1.11.0"
"version": "1.11.1"
}
9 changes: 7 additions & 2 deletions custom_components/bms_ble/plugins/jikong_bms.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def _notification_handler(self, _sender, data: bytearray) -> None:

if (
len(self._data) >= self.INFO_LEN
and (data.startswith((BMS.HEAD_RSP, self.HEAD_CMD)))
and (data.startswith((BMS.HEAD_RSP, BMS.HEAD_CMD)))
) or not self._data.startswith(BMS.HEAD_RSP):
self._data = bytearray()

Expand All @@ -133,12 +133,17 @@ def _notification_handler(self, _sender, data: bytearray) -> None:
)
return

# trim AT\r\n message from the end
if self._data.endswith(BMS.BT_MODULE_MSG):
self._log.debug("trimming AT cmd")
self._data = self._data[: -len(BMS.BT_MODULE_MSG)]

# trim message in case oversized
if len(self._data) > BMS.INFO_LEN:
self._log.debug("wrong data length (%i): %s", len(self._data), self._data)
self._data = self._data[: BMS.INFO_LEN]

crc = crc_sum(self._data[:-1])
crc: int = crc_sum(self._data[:-1])
if self._data[-1] != crc:
self._log.debug("invalid checksum 0x%X != 0x%X", self._data[-1], crc)
return
Expand Down
12 changes: 7 additions & 5 deletions tests/test_jikong_bms.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x93"
),
"ack": bytearray(
b"\xaa\x55\x90\xeb\xc8\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x44"
),
b"\xaa\x55\x90\xeb\xc8\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x44\x41\x54\x0d\x0a"
), # ACKnowledge message with attached AT\r\n message (needs to be filtered)
"cell": bytearray( # JK02_24S (SW: 10.08)
b"\x55\xaa\xeb\x90\x02\xc8\xee\x0c\xf2\x0c\xf1\x0c\xf0\x0c\xf0\x0c\xec\x0c\xf0\x0c\xed\x0c"
b"\xed\x0c\xed\x0c\xed\x0c\xf0\x0c\xf1\x0c\xed\x0c\xee\x0c\xed\x0c\x00\x00\x00\x00\x00\x00"
Expand Down Expand Up @@ -76,8 +76,8 @@
b"\x00\xfe\xbf\x21\x06\x00\x00\x00\x00\x00\x00\x00\x00\xd8"
), # Vendor_ID: JK_B2A8S20P, SN: 404092C2262, HW: V11.XA, SW: V11.48, power-on: 7, Version: 4.28.0
"ack": bytearray(
b"\xaa\x55\x90\xeb\xc8\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x44"
),
b"\xaa\x55\x90\xeb\xc8\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x44\x41\x54\x0d\x0a"
), # ACKnowledge message with attached AT\r\n message (needs to be filtered)
"cell": bytearray(
b"\x55\xaa\xeb\x90\x02\xc6\xc1\x0c\xc1\x0c\xc1\x0c\xc1\x0c\xc1\x0c\xc1\x0c\xc1\x0c\xc1\x0c"
b"\xc1\x0c\xc1\x0c\xc1\x0c\xc1\x0c\xc1\x0c\xc1\x0c\xc1\x0c\xc1\x0c\x00\x00\x00\x00\x00\x00"
Expand Down Expand Up @@ -523,7 +523,9 @@ async def test_stream_update(monkeypatch, protocol_type, reconnect_fixture) -> N
async def test_invalid_response(monkeypatch) -> None:
"""Test data update with BMS returning invalid data."""

monkeypatch.setattr("custom_components.bms_ble.plugins.jikong_bms.BMS.BAT_TIMEOUT", 0.1)
monkeypatch.setattr(
"custom_components.bms_ble.plugins.jikong_bms.BMS.BAT_TIMEOUT", 0.1
)

# return type 0x03 (first requested message) with incorrect CRC
monkeypatch.setattr(
Expand Down

0 comments on commit 9911cb3

Please sign in to comment.