Skip to content

Commit 6b34b40

Browse files
bthome.py - Added datatypes
1 parent cc341a3 commit 6b34b40

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/bthome.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
ILLUMINANCE_UINT24 = const(0x05)
2929
MASS_KG_UINT16 = const(0x06)
3030
MASS_LB_UINT16 = const(0x07)
31+
DEWPOINT_SINT16 = const(0x08)
32+
COUNT_UINT8 = const(0x09)
33+
ENERGY_UINT24 = const(0x0A)
34+
POWER_UINT24 = const(0x0B)
35+
VOLTAGE_UINT16 = const(0x0C)
3136

3237
# Default value decimal places hint at precision
3338
battery = 0 # percent
@@ -36,6 +41,11 @@
3641
pressure = 0.00 # hectoPascals (millibars)
3742
illuminance = 0.00 # Lux
3843
mass = 0.00 # kg or lb
44+
dewpoint = 0.00 # degrees
45+
count = 0 # integer
46+
energy = 0.000 # kWh
47+
power = 0.00 # W
48+
voltage = 0.000 # V
3949
device_name = "BTHome-MPY" # Limit to 10 characters
4050

4151

@@ -63,11 +73,21 @@ def _pack_uint16_x100(object_id, value):
6373
return pack("<BH", object_id, round(value * 100))
6474

6575

76+
# 16-bit unsigned integer with scalling of 1000 (3 decimal places)
77+
def _pack_uint16_x1000(object_id, value):
78+
return pack("<BH", object_id, round(value * 1000))
79+
80+
6681
# 24-bit unsigned integer with scaling of 100 (2 decimal places)
6782
def _pack_uint24_x100(object_id, value):
6883
return pack("<BL", object_id, round(value * 100))[:-1]
6984

7085

86+
# 24-bit unsigned integer with scaling of 1000 (3 decimal places)
87+
def _pack_uint24_x1000(object_id, value):
88+
return pack("<BL", object_id, round(value * 1000))[:-1]
89+
90+
7191
# The BTHome object ID determines the number of bytes and fixed point decimal multiplier.
7292
def _pack_bthome_data(object_id):
7393
if object_id == BATTERY_UINT8:
@@ -84,6 +104,16 @@ def _pack_bthome_data(object_id):
84104
bthome_bytes = _pack_uint24_x100(MASS_KG_UINT16, mass)
85105
elif object_id == MASS_LB_UINT16:
86106
bthome_bytes = _pack_uint24_x100(MASS_LB_UINT16, mass)
107+
elif object_id == DEWPOINT_SINT16:
108+
bthome_bytes = _pack_sint16_x100(DEWPOINT_SINT16, dewpoint)
109+
elif object_id == COUNT_UINT8:
110+
bthome_bytes = _pack_uint8_x1(COUNT_UINT8, count)
111+
elif object_id == ENERGY_UINT24:
112+
bthome_bytes = _pack_uint24_x1000(ENERGY_UINT24, energy)
113+
elif object_id == POWER_UINT24:
114+
bthome_bytes = _pack_uint24_x100(POWER_UINT24, power)
115+
elif object_id == VOLTAGE_UINT16:
116+
bthome_bytes = _pack_uint16_x1000(VOLTAGE_UINT16, voltage)
87117
else:
88118
bthome_bytes = bytes()
89119
print("Packing with data:", bthome_bytes.hex().upper())

0 commit comments

Comments
 (0)