1717# See "Sensor Data" table at https://bthome.io/format/ for details.
1818BATTERY_UINT8 = const (0x01 )
1919TEMPERATURE_SINT16 = const (0x02 )
20- _TEMPERATURE_SINT16_SCALING = const (100 )
2120HUMIDITY_UINT16 = const (0x03 )
22- _HUMIDITY_UINT16_SCALING = const (100 )
2321PRESSURE_UINT24 = const (0x04 )
24- _PRESSURE_UINT24_SCALING = const (100 )
2522ILLUMINANCE_UINT24 = const (0x05 )
26- _ILLUMINANCE_UINT24_SCALING = const (100 )
2723MASS_KG_UINT16 = const (0x06 )
28- _MASS_KG_UINT16_SCALING = const (100 )
2924MASS_LB_UINT16 = const (0x07 )
30- _MASS_LB_UINT16_SCALING = const (100 )
3125
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
4034
4135def _pack_device_name ():
4236 assert len (device_name ) > 0
@@ -46,22 +40,38 @@ def _pack_device_name():
4640 device_name_bytes = bytes ([len (device_name_bytes )]) + device_name_bytes
4741 return device_name_bytes
4842
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+
4959# The BTHome object ID determines the number of bytes and fixed point decimal multiplier.
5060def _pack_bthome_data (object_id ):
5161 if object_id == BATTERY_UINT8 :
52- bthome_bytes = pack ( 'BB' , BATTERY_UINT8 , battery )
62+ bthome_bytes = _pack_uint8_x1 ( BATTERY_UINT8 , battery )
5363 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 )
5565 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 )
5767 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 )
5969 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 )
6171 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 )
6373 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 )
6575 else :
6676 bthome_bytes = bytes ()
6777 print ("Packing with data:" , bthome_bytes .hex ().upper ())
0 commit comments