Skip to content

Commit 9911cb3

Browse files
authored
Filter AT commands at the end of JK messages (#125)
* remove AT cmds from end of message * Update manifest.json
1 parent ce977bb commit 9911cb3

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

custom_components/bms_ble/manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,5 @@
9090
"issue_tracker": "https://github.com/patman15/BMS_BLE-HA/issues",
9191
"loggers": ["bleak_retry_connector"],
9292
"requirements": [],
93-
"version": "1.11.0"
93+
"version": "1.11.1"
9494
}

custom_components/bms_ble/plugins/jikong_bms.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def _notification_handler(self, _sender, data: bytearray) -> None:
107107

108108
if (
109109
len(self._data) >= self.INFO_LEN
110-
and (data.startswith((BMS.HEAD_RSP, self.HEAD_CMD)))
110+
and (data.startswith((BMS.HEAD_RSP, BMS.HEAD_CMD)))
111111
) or not self._data.startswith(BMS.HEAD_RSP):
112112
self._data = bytearray()
113113

@@ -133,12 +133,17 @@ def _notification_handler(self, _sender, data: bytearray) -> None:
133133
)
134134
return
135135

136+
# trim AT\r\n message from the end
137+
if self._data.endswith(BMS.BT_MODULE_MSG):
138+
self._log.debug("trimming AT cmd")
139+
self._data = self._data[: -len(BMS.BT_MODULE_MSG)]
140+
136141
# trim message in case oversized
137142
if len(self._data) > BMS.INFO_LEN:
138143
self._log.debug("wrong data length (%i): %s", len(self._data), self._data)
139144
self._data = self._data[: BMS.INFO_LEN]
140145

141-
crc = crc_sum(self._data[:-1])
146+
crc: int = crc_sum(self._data[:-1])
142147
if self._data[-1] != crc:
143148
self._log.debug("invalid checksum 0x%X != 0x%X", self._data[-1], crc)
144149
return

tests/test_jikong_bms.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x93"
4040
),
4141
"ack": bytearray(
42-
b"\xaa\x55\x90\xeb\xc8\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x44"
43-
),
42+
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"
43+
), # ACKnowledge message with attached AT\r\n message (needs to be filtered)
4444
"cell": bytearray( # JK02_24S (SW: 10.08)
4545
b"\x55\xaa\xeb\x90\x02\xc8\xee\x0c\xf2\x0c\xf1\x0c\xf0\x0c\xf0\x0c\xec\x0c\xf0\x0c\xed\x0c"
4646
b"\xed\x0c\xed\x0c\xed\x0c\xf0\x0c\xf1\x0c\xed\x0c\xee\x0c\xed\x0c\x00\x00\x00\x00\x00\x00"
@@ -76,8 +76,8 @@
7676
b"\x00\xfe\xbf\x21\x06\x00\x00\x00\x00\x00\x00\x00\x00\xd8"
7777
), # Vendor_ID: JK_B2A8S20P, SN: 404092C2262, HW: V11.XA, SW: V11.48, power-on: 7, Version: 4.28.0
7878
"ack": bytearray(
79-
b"\xaa\x55\x90\xeb\xc8\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x44"
80-
),
79+
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"
80+
), # ACKnowledge message with attached AT\r\n message (needs to be filtered)
8181
"cell": bytearray(
8282
b"\x55\xaa\xeb\x90\x02\xc6\xc1\x0c\xc1\x0c\xc1\x0c\xc1\x0c\xc1\x0c\xc1\x0c\xc1\x0c\xc1\x0c"
8383
b"\xc1\x0c\xc1\x0c\xc1\x0c\xc1\x0c\xc1\x0c\xc1\x0c\xc1\x0c\xc1\x0c\x00\x00\x00\x00\x00\x00"
@@ -523,7 +523,9 @@ async def test_stream_update(monkeypatch, protocol_type, reconnect_fixture) -> N
523523
async def test_invalid_response(monkeypatch) -> None:
524524
"""Test data update with BMS returning invalid data."""
525525

526-
monkeypatch.setattr("custom_components.bms_ble.plugins.jikong_bms.BMS.BAT_TIMEOUT", 0.1)
526+
monkeypatch.setattr(
527+
"custom_components.bms_ble.plugins.jikong_bms.BMS.BAT_TIMEOUT", 0.1
528+
)
527529

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

0 commit comments

Comments
 (0)