Skip to content

Commit 48a7a0d

Browse files
bthome.py - add packet_id (issue #7)
1 parent 304b01a commit 48a7a0d

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/bthome.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class BTHome:
3636
# BATTERY_CHARGING_BINARY
3737
# All binary sensors are packed as 8-bit unsigned bytes.
3838
# See "Sensor Data" table at https://bthome.io/format/
39+
PACKET_ID_UINT8 = const(0x00)
3940
BATTERY_UINT8_X1 = const(0x01) # %
4041
TEMPERATURE_SINT16_X100 = const(0x02) # °C
4142
HUMIDITY_UINT16_X100 = const(0x03) # %
@@ -105,6 +106,7 @@ class BTHome:
105106
# There is more than one way to represent most sensor properties. This
106107
# dictionary maps the object id to the property name.
107108
_object_id_properties = {
109+
PACKET_ID_UINT8: "packet_id", # 0x00
108110
BATTERY_UINT8_X1: "battery", # 0x01
109111
TEMPERATURE_SINT16_X100: "temperature", # 0x02
110112
HUMIDITY_UINT16_X100: "humidity", # 0x03
@@ -233,6 +235,12 @@ def __init__(self, local_name="BTHome", debug=False):
233235
def local_name(self):
234236
return self._local_name
235237

238+
@property
239+
def packet_id(self):
240+
self._packet_id += 1
241+
self._packet_id &= 0xFF # Truncate to 8-bit max
242+
return self._packet_id
243+
236244
# Technically, the functions below could be static methods, but @staticmethod
237245
# on a dictionary of functions only works with Python >3.10, and MicroPython
238246
# is based on 3.4. Also, __func__ and __get()__ workarounds throw errors in
@@ -292,6 +300,7 @@ def _pack_raw_text(self, object_id, value):
292300
return packed_value
293301

294302
_object_id_functions = {
303+
PACKET_ID_UINT8: _pack_int8_x1, # 0x00
295304
BATTERY_UINT8_X1: _pack_int8_x1, # 0x01
296305
TEMPERATURE_SINT16_X100: _pack_int16_x100, # 0x02
297306
HUMIDITY_UINT16_X100: _pack_int16_x100, # 0x03

0 commit comments

Comments
 (0)