17
17
# See "Sensor Data" table at https://bthome.io/format/ for details.
18
18
BATTERY_UINT8 = const (0x01 )
19
19
TEMPERATURE_SINT16 = const (0x02 )
20
- _TEMPERATURE_SINT16_SCALING = const (100 )
21
20
HUMIDITY_UINT16 = const (0x03 )
22
- _HUMIDITY_UINT16_SCALING = const (100 )
23
21
PRESSURE_UINT24 = const (0x04 )
24
- _PRESSURE_UINT24_SCALING = const (100 )
25
22
ILLUMINANCE_UINT24 = const (0x05 )
26
- _ILLUMINANCE_UINT24_SCALING = const (100 )
27
23
MASS_KG_UINT16 = const (0x06 )
28
- _MASS_KG_UINT16_SCALING = const (100 )
29
24
MASS_LB_UINT16 = const (0x07 )
30
- _MASS_LB_UINT16_SCALING = const (100 )
31
25
32
- # Default value decimal places indicate precision
33
- device_name = "BTHome-MPY"
34
- battery = 0 # percent
35
- temperature = 0.00 # degrees Celsius
36
- humidity = 0.00 # percent (relative humidity )
37
- pressure = 0.00 # hectoPascals (millibars)
38
- illuminance = 0.0 # Lux
39
- mass = 0.00 # kg or lb
26
+ # Default value decimal places hint at precision
27
+ battery = 0 # percent
28
+ temperature = 0.00 # degrees Celsius
29
+ humidity = 0.00 # percent (relative humidity)
30
+ pressure = 0.00 # hectoPascals (millibars )
31
+ illuminance = 0.00 # Lux
32
+ mass = 0.00 # kg or lb
33
+ device_name = "BTHome-MPY" # Limit to 10 characters
40
34
41
35
def _pack_device_name ():
42
36
assert len (device_name ) > 0
@@ -46,22 +40,38 @@ def _pack_device_name():
46
40
device_name_bytes = bytes ([len (device_name_bytes )]) + device_name_bytes
47
41
return device_name_bytes
48
42
43
+ # 8-bit unsigned integer with scaling of 1 (no scaling, 0 decimal places)
44
+ def _pack_uint8_x1 (object_id , value ):
45
+ return pack ('BB' , object_id , value )
46
+
47
+ # 16-bit signed integer with scalling of 100 (2 decimal places)
48
+ def _pack_sint16_x100 (object_id , value ):
49
+ return pack ('<Bh' , object_id , round (value * 100 ))
50
+
51
+ # 16-bit unsigned integer with scalling of 100 (2 decimal places)
52
+ def _pack_uint16_x100 (object_id , value ):
53
+ return pack ('<BH' , object_id , round (value * 100 ))
54
+
55
+ # 24-bit unsigned integer with scaling of 100 (2 decimal places)
56
+ def _pack_uint24_x100 (object_id , value ):
57
+ return pack ('<BL' , object_id , round (value * 100 ))[:- 1 ]
58
+
49
59
# The BTHome object ID determines the number of bytes and fixed point decimal multiplier.
50
60
def _pack_bthome_data (object_id ):
51
61
if object_id == BATTERY_UINT8 :
52
- bthome_bytes = pack ( 'BB' , BATTERY_UINT8 , battery )
62
+ bthome_bytes = _pack_uint8_x1 ( BATTERY_UINT8 , battery )
53
63
elif object_id == TEMPERATURE_SINT16 :
54
- bthome_bytes = pack ( '<Bh' , TEMPERATURE_SINT16 , round ( temperature * _TEMPERATURE_SINT16_SCALING ) )
64
+ bthome_bytes = _pack_sint16_x100 ( TEMPERATURE_SINT16 , temperature )
55
65
elif object_id == HUMIDITY_UINT16 :
56
- bthome_bytes = pack ( '<Bh' , HUMIDITY_UINT16 , round ( humidity * _HUMIDITY_UINT16_SCALING ) )
66
+ bthome_bytes = _pack_uint16_x100 ( HUMIDITY_UINT16 , humidity )
57
67
elif object_id == PRESSURE_UINT24 :
58
- bthome_bytes = pack ( '<BL' , PRESSURE_UINT24 , round ( pressure * _PRESSURE_UINT24_SCALING ))[: - 1 ]
68
+ bthome_bytes = _pack_uint24_x100 ( PRESSURE_UINT24 , pressure )
59
69
elif object_id == ILLUMINANCE_UINT24 :
60
- bthome_bytes = pack ( '<BL' , ILLUMINANCE_UINT24 , round ( illuminance * _ILLUMINANCE_UINT24_SCALING ))[: - 1 ]
70
+ bthome_bytes = _pack_uint24_x100 ( ILLUMINANCE_UINT24 , illuminance )
61
71
elif object_id == MASS_KG_UINT16 :
62
- bthome_bytes = pack ( '<Bh' , MASS_KG_UINT16 , round ( mass * _MASS_KG_UINT16_SCALING ) )
72
+ bthome_bytes = _pack_uint24_x100 ( MASS_KG_UINT16 , mass )
63
73
elif object_id == MASS_LB_UINT16 :
64
- bthome_bytes = pack ( '<Bh' , MASS_LB_UINT16 , round ( mass * _MASS_LB_UINT16_SCALING ) )
74
+ bthome_bytes = _pack_uint24_x100 ( MASS_LB_UINT16 , mass )
65
75
else :
66
76
bthome_bytes = bytes ()
67
77
print ("Packing with data:" , bthome_bytes .hex ().upper ())
0 commit comments