Skip to content

Commit ab2d3fe

Browse files
bthom.py - more sensor objects
1 parent ec54981 commit ab2d3fe

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/bthome.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ class BTHome:
4747
HUMIDITY_UINT8 = const(0x2E)
4848
MOISTURE_UINT8 = const(0x2F)
4949
COUNT_UINT16 = const(0x3D)
50+
COUNT_UINT32 = const(0x3E)
51+
ROTATION_SINT16 = const(0x3F)
5052

5153
# There is more than one way to represent most sensor properties. This
5254
# dictionary maps the object id to the property name.
@@ -70,9 +72,12 @@ class BTHome:
7072
MOISTURE_UINT16: "moisture",
7173
HUMIDITY_UINT8: "humidity",
7274
MOISTURE_UINT8: "moisture",
73-
COUNT_UINT16: "count"
75+
COUNT_UINT16: "count",
76+
COUNT_UINT32: "count",
77+
ROTATION_SINT16: "rotation",
7478
}
7579

80+
# Properties below are updated externally when sensor values are read.
7681
# See "Sensor Data" table at https://bthome.io/format/ Property column.
7782
acceleration = 0
7883
battery = 0
@@ -130,14 +135,18 @@ def pack_local_name(self):
130135
return local_name_bytes
131136

132137
# Technically, the functions below could be static methods, but @staticmethod
133-
# on a dictionary of functions only works with Python >3.10, but MicroPython
138+
# on a dictionary of functions only works with Python >3.10, and MicroPython
134139
# is based on 3.4. Also, __func__ and __get()__ workarounds throw errors in
135140
# MicroPython. [^4]
136141

137142
# 8-bit unsigned integer with scaling of 1 (no decimal places)
138143
def _pack_uint8_x1(self, object_id, value):
139144
return pack("BB", object_id, value)
140145

146+
# 16-bit signed integer with scalling of 10 (1 decimal place)
147+
def _pack_sint16_x10(self, object_id, value):
148+
return pack("<Bh", object_id, round(value * 10))
149+
141150
# 16-bit signed integer with scalling of 100 (2 decimal places)
142151
def _pack_sint16_x100(self, object_id, value):
143152
return pack("<Bh", object_id, round(value * 100))
@@ -162,6 +171,10 @@ def _pack_uint24_x100(self, object_id, value):
162171
def _pack_uint24_x1000(self, object_id, value):
163172
return pack("<BL", object_id, round(value * 1000))[:-1]
164173

174+
# 32-bit unsigned integer with scaling of 1 (no decimal places)
175+
def _pack_uint32_x1(self, object_id, value):
176+
return pack("<BL", object_id, round(value))
177+
165178
_object_id_functions = {
166179
BATTERY_UINT8: _pack_uint8_x1,
167180
TEMPERATURE_SINT16: _pack_sint16_x100,
@@ -182,7 +195,9 @@ def _pack_uint24_x1000(self, object_id, value):
182195
MOISTURE_UINT16: _pack_uint16_x100,
183196
HUMIDITY_UINT8: _pack_uint8_x1,
184197
MOISTURE_UINT8: _pack_uint8_x1,
185-
COUNT_UINT16: _pack_uint16_x1
198+
COUNT_UINT16: _pack_uint16_x1,
199+
COUNT_UINT32: _pack_uint32_x1,
200+
ROTATION_SINT16: _pack_sint16_x10,
186201
}
187202

188203
# Concatenate an arbitrary number of sensor readings using parameters

0 commit comments

Comments
 (0)