@@ -39,20 +39,17 @@ class BMS(BaseBMS):
39
39
BT_MODULE_MSG : Final = bytes ([0x41 , 0x54 , 0x0D , 0x0A ]) # AT\r\n from BLE module
40
40
TYPE_POS : Final [int ] = 4 # frame type is right after the header
41
41
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
+ )
56
53
57
54
def __init__ (self , ble_device : BLEDevice , reconnect : bool = False ) -> None :
58
55
"""Intialize private BMS members."""
@@ -237,6 +234,17 @@ def _cell_voltages(data: bytearray, cells: int) -> dict[str, float]:
237
234
for idx in range (cells )
238
235
}
239
236
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
+
240
248
@staticmethod
241
249
def _decode_data (data : bytearray ) -> BMSsample :
242
250
"""Return BMS data from status message."""
@@ -261,6 +269,7 @@ async def _async_update(self) -> BMSsample:
261
269
return {}
262
270
263
271
data = self ._decode_data (self ._data_final )
272
+ data .update (BMS ._temp_sensors (self ._data_final ))
264
273
data .update (BMS ._cell_voltages (self ._data_final , int (data [KEY_CELL_COUNT ])))
265
274
266
275
return data
0 commit comments