Skip to content

Commit 0418267

Browse files
authored
Exclude not connected temperature sensors for JK BMS (#118)
* remove non-available temp sensors * added test for missing temp sensor
1 parent 1e88fd2 commit 0418267

File tree

2 files changed

+93
-14
lines changed

2 files changed

+93
-14
lines changed

custom_components/bms_ble/plugins/jikong_bms.py

+23-14
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,17 @@ class BMS(BaseBMS):
3939
BT_MODULE_MSG: Final = bytes([0x41, 0x54, 0x0D, 0x0A]) # AT\r\n from BLE module
4040
TYPE_POS: Final[int] = 4 # frame type is right after the header
4141
INFO_LEN: Final[int] = 300
42-
_FIELDS: Final[
43-
list[tuple[str, int, int, bool, Callable[[int], int | float]]]
44-
] = [ # Protocol: JK02_32S; JK02_24S has offset -32
45-
(KEY_CELL_COUNT, 70, 4, False, lambda x: x.bit_count()),
46-
(ATTR_DELTA_VOLTAGE, 76, 2, False, lambda x: float(x / 1000)),
47-
(ATTR_VOLTAGE, 150, 4, False, lambda x: float(x / 1000)),
48-
(ATTR_CURRENT, 158, 4, True, lambda x: float(x / 1000)),
49-
(ATTR_BATTERY_LEVEL, 173, 1, False, lambda x: x),
50-
(ATTR_CYCLE_CHRG, 174, 4, False, lambda x: float(x / 1000)),
51-
(ATTR_CYCLES, 182, 4, False, lambda x: x),
52-
] + [ # add temperature sensors
53-
(f"{KEY_TEMP_VALUE}{i}", addr, 2, True, lambda x: float(x / 10))
54-
for i, addr in [(0, 144), (1, 162), (2, 164), (3, 256), (4, 258)]
55-
]
42+
_FIELDS: Final[list[tuple[str, int, int, bool, Callable[[int], int | float]]]] = (
43+
[ # Protocol: JK02_32S; JK02_24S has offset -32
44+
(KEY_CELL_COUNT, 70, 4, False, lambda x: x.bit_count()),
45+
(ATTR_DELTA_VOLTAGE, 76, 2, False, lambda x: float(x / 1000)),
46+
(ATTR_VOLTAGE, 150, 4, False, lambda x: float(x / 1000)),
47+
(ATTR_CURRENT, 158, 4, True, lambda x: float(x / 1000)),
48+
(ATTR_BATTERY_LEVEL, 173, 1, False, lambda x: x),
49+
(ATTR_CYCLE_CHRG, 174, 4, False, lambda x: float(x / 1000)),
50+
(ATTR_CYCLES, 182, 4, False, lambda x: x),
51+
]
52+
)
5653

5754
def __init__(self, ble_device: BLEDevice, reconnect: bool = False) -> None:
5855
"""Intialize private BMS members."""
@@ -237,6 +234,17 @@ def _cell_voltages(data: bytearray, cells: int) -> dict[str, float]:
237234
for idx in range(cells)
238235
}
239236

237+
@staticmethod
238+
def _temp_sensors(data: bytearray) -> dict[str, float]:
239+
return {
240+
f"{KEY_TEMP_VALUE}{idx}": int.from_bytes(
241+
data[pos : pos + 2], byteorder="little", signed=False
242+
)
243+
/ 10
244+
for idx, pos in [(0, 144), (1, 162), (2, 164), (3, 256), (4, 258)]
245+
if int.from_bytes(data[pos : pos + 2], byteorder="little", signed=False)
246+
}
247+
240248
@staticmethod
241249
def _decode_data(data: bytearray) -> BMSsample:
242250
"""Return BMS data from status message."""
@@ -261,6 +269,7 @@ async def _async_update(self) -> BMSsample:
261269
return {}
262270

263271
data = self._decode_data(self._data_final)
272+
data.update(BMS._temp_sensors(self._data_final))
264273
data.update(BMS._cell_voltages(self._data_final, int(data[KEY_CELL_COUNT])))
265274

266275
return data

tests/test_jikong_bms.py

+70
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,76 @@ async def test_update(monkeypatch, reconnect_fixture) -> None:
344344
await bms.disconnect()
345345

346346

347+
async def test_hide_temp_sensors(monkeypatch) -> None:
348+
"""Test Jikong BMS data update with not connected temperature sensors."""
349+
350+
temp2_zero: Final = bytearray(
351+
b"\x55\xaa\xeb\x90\x02\xc6\xc1\x0c\xc1\x0c\xc1\x0c\xc1\x0c\xc1\x0c\xc1\x0c\xc1\x0c\xc1\x0c"
352+
b"\xc1\x0c\xc1\x0c\xc1\x0c\xc1\x0c\xc1\x0c\xc1\x0c\xc1\x0c\xc1\x0c\x00\x00\x00\x00\x00\x00"
353+
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
354+
b"\x00\x00\x00\x00\xff\xff\x00\x00\xc1\x0c\x02\x00\x00\x07\x3a\x00\x3c\x00\x46\x00\x48\x00"
355+
b"\x54\x00\x5c\x00\x69\x00\x76\x00\x7d\x00\x76\x00\x6c\x00\x69\x00\x61\x00\x4b\x00\x47\x00"
356+
b"\x3c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
357+
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xb8\x00\x00\x00\x00\x00\x0a\xcc\x00\x00"
358+
b"\xcd\x71\x08\x00\x9d\xd6\xff\xff\xb5\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x2a\x47\xcb"
359+
b"\x01\x00\xc0\x45\x04\x00\x02\x00\x00\x00\x15\xb7\x08\x00\x64\x00\x00\x00\x6b\xc7\x06\x00"
360+
b"\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00\x01\x00\x00\x00"
361+
b"\xb2\x03\x00\x00\x1c\x00\x54\x29\x40\x40\x00\x00\x00\x00\x67\x14\x00\x00\x00\x01\x01\x01"
362+
b"\x00\x06\x00\x00\xf3\x48\x2e\x00\x00\x00\x00\x00\xb8\x00\xb4\x00\xb7\x00\xb2\x03\xde\xe4"
363+
b"\x5b\x08\x2c\x00\x00\x00\x80\x51\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
364+
b"\x00\xfe\xff\x7f\xdc\x2f\x01\x01\xb0\x07\x00\x00\x00\x1a"
365+
) # {"temperature": 18.4, "voltage": 52.234, "current": -10.595, "battery_level": 42, "cycle_charge": 117.575, "cycles": 2}
366+
367+
monkeypatch.setattr(
368+
"tests.test_jikong_bms.MockJikongBleakClient.CEL_FRAME", temp2_zero
369+
)
370+
371+
monkeypatch.setattr(
372+
"custom_components.bms_ble.plugins.basebms.BleakClient", MockJikongBleakClient
373+
)
374+
375+
bms = BMS(generate_ble_device("cc:cc:cc:cc:cc:cc", "MockBLEdevice", None, -73))
376+
377+
result = await bms.async_update()
378+
379+
assert result == {
380+
"cell_count": 16,
381+
"delta_voltage": 0.002,
382+
"temperature": 18.2,
383+
"voltage": 52.234,
384+
"current": -10.595,
385+
"battery_level": 42,
386+
"cycle_charge": 117.575,
387+
"cycles": 2,
388+
"cell#0": 3.265,
389+
"cell#1": 3.265,
390+
"cell#2": 3.265,
391+
"cell#3": 3.265,
392+
"cell#4": 3.265,
393+
"cell#5": 3.265,
394+
"cell#6": 3.265,
395+
"cell#7": 3.265,
396+
"cell#8": 3.265,
397+
"cell#9": 3.265,
398+
"cell#10": 3.265,
399+
"cell#11": 3.265,
400+
"cell#12": 3.265,
401+
"cell#13": 3.265,
402+
"cell#14": 3.265,
403+
"cell#15": 3.265,
404+
"cycle_capacity": 6141.413,
405+
"power": -553.419,
406+
"battery_charging": False,
407+
"runtime": 39949,
408+
"temp#0": 18.4,
409+
"temp#1": 18.1,
410+
"temp#3": 18.0,
411+
"temp#4": 18.3,
412+
}
413+
414+
await bms.disconnect()
415+
416+
347417
async def test_stream_update(monkeypatch, reconnect_fixture) -> None:
348418
"""Test Jikong BMS data update."""
349419

0 commit comments

Comments
 (0)