88
99# See "Advertising Payload" at https://bthome.io/format/ for details.
1010_ADVERT_LENGTH_MAX = const (255 )
11- _ADVERT_FLAGS = bytes .fromhex ('020106' ) # length (02), flags indicator (01), flag bits (06)
11+ _ADVERT_FLAGS = bytes .fromhex (
12+ "020106"
13+ ) # length (02), flags indicator (01), flag bits (06)
1214_DEVICE_NAME_LENGTH_MAX = const (10 )
1315_SERVICE_DATA_UUID16 = const (0x16 )
14- _SERVICE_UUID16 = const (0xFCD2 ) # See: https://bthome.io/images/License_Statement_-_BTHOME.pdf
15- _DEVICE_INFO_FLAGS = const (0x40 ) # Currently hardcoded: no encryption, regular updates, version 2
16+ _SERVICE_UUID16 = const (
17+ 0xFCD2
18+ ) # See: https://bthome.io/images/License_Statement_-_BTHOME.pdf
19+ _DEVICE_INFO_FLAGS = const (
20+ 0x40
21+ ) # Currently hardcoded: no encryption, regular updates, version 2
1622
1723# See "Sensor Data" table at https://bthome.io/format/ for details.
1824BATTERY_UINT8 = const (0x01 )
2430MASS_LB_UINT16 = const (0x07 )
2531
2632# 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+ battery = 0 # percent
34+ temperature = 0.00 # degrees Celsius
35+ humidity = 0.00 # percent (relative humidity)
36+ pressure = 0.00 # hectoPascals (millibars)
37+ illuminance = 0.00 # Lux
38+ mass = 0.00 # kg or lb
3339device_name = "BTHome-MPY" # Limit to 10 characters
3440
41+
3542def _pack_device_name ():
3643 assert len (device_name ) > 0
3744 assert len (device_name ) <= _DEVICE_NAME_LENGTH_MAX
38- name_type = bytes .fromhex ('09' ) # indicator for complete name
45+ name_type = bytes .fromhex ("09" ) # indicator for complete name
3946 device_name_bytes = name_type + device_name .encode ()
4047 device_name_bytes = bytes ([len (device_name_bytes )]) + device_name_bytes
4148 return device_name_bytes
4249
50+
4351# 8-bit unsigned integer with scaling of 1 (no scaling, 0 decimal places)
4452def _pack_uint8_x1 (object_id , value ):
45- return pack ('BB' , object_id , value )
53+ return pack ("BB" , object_id , value )
54+
4655
4756# 16-bit signed integer with scalling of 100 (2 decimal places)
4857def _pack_sint16_x100 (object_id , value ):
49- return pack ('<Bh' , object_id , round (value * 100 ))
58+ return pack ("<Bh" , object_id , round (value * 100 ))
59+
5060
5161# 16-bit unsigned integer with scalling of 100 (2 decimal places)
5262def _pack_uint16_x100 (object_id , value ):
53- return pack ('<BH' , object_id , round (value * 100 ))
63+ return pack ("<BH" , object_id , round (value * 100 ))
64+
5465
5566# 24-bit unsigned integer with scaling of 100 (2 decimal places)
5667def _pack_uint24_x100 (object_id , value ):
57- return pack ('<BL' , object_id , round (value * 100 ))[:- 1 ]
68+ return pack ("<BL" , object_id , round (value * 100 ))[:- 1 ]
69+
5870
5971# The BTHome object ID determines the number of bytes and fixed point decimal multiplier.
6072def _pack_bthome_data (object_id ):
@@ -77,16 +89,20 @@ def _pack_bthome_data(object_id):
7789 print ("Packing with data:" , bthome_bytes .hex ().upper ())
7890 return bthome_bytes
7991
92+
8093# Concatenate an arbitrary number of sensor readings using parameters of sensor data constants to indicate what's included.
8194def _pack_service_data (* args ):
82- service_data_bytes = pack ('B' , _SERVICE_DATA_UUID16 ) # indicates a 16-bit service UUID follows
83- service_data_bytes += pack ('<h' , _SERVICE_UUID16 )
84- service_data_bytes += pack ('B' , _DEVICE_INFO_FLAGS )
95+ service_data_bytes = pack (
96+ "B" , _SERVICE_DATA_UUID16
97+ ) # indicates a 16-bit service UUID follows
98+ service_data_bytes += pack ("<h" , _SERVICE_UUID16 )
99+ service_data_bytes += pack ("B" , _DEVICE_INFO_FLAGS )
85100 for object_id in args :
86101 service_data_bytes += _pack_bthome_data (object_id )
87- service_data_bytes = pack ('B' , len (service_data_bytes )) + service_data_bytes
102+ service_data_bytes = pack ("B" , len (service_data_bytes )) + service_data_bytes
88103 return service_data_bytes
89104
105+
90106# Construct advertising payload suitable for use by MicroPython's aioble.advertise(adv_data)
91107def pack_advertisement (* args ):
92108 advertisement_bytes = _ADVERT_FLAGS # All BTHome adverts start this way.
0 commit comments