Skip to content

Commit a632e96

Browse files
Format with Ruff
1 parent 640e7b7 commit a632e96

File tree

2 files changed

+44
-23
lines changed

2 files changed

+44
-23
lines changed

bthome.py

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,17 @@
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.
1824
BATTERY_UINT8 = const(0x01)
@@ -24,37 +30,43 @@
2430
MASS_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
3339
device_name = "BTHome-MPY" # Limit to 10 characters
3440

41+
3542
def _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)
4452
def _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)
4857
def _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)
5262
def _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)
5667
def _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.
6072
def _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.
8194
def _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)
91107
def pack_advertisement(*args):
92108
advertisement_bytes = _ADVERT_FLAGS # All BTHome adverts start this way.

main.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@
88
import bthome
99

1010
BLE_ADV_INTERVAL_uS = 250000
11-
AWAKE_TIME_SECS = 60 # How long to spend advertising and servicing clients.
11+
AWAKE_TIME_SECS = 60 # How long to spend advertising and servicing clients.
1212
SLEEP_TIME_SECS = 120 # How long to spend in deep sleep.
1313

1414
base_mac = unique_id() # WiFi MAC
1515
bluetooth_mac = bytearray(base_mac)
16-
bluetooth_mac[5] += 2 # ESP32 Bluetooth MAC is always WiFi MAC + 2
16+
bluetooth_mac[5] += 2 # ESP32 Bluetooth MAC is always WiFi MAC + 2
1717

1818
bthome.device_name = "DIY-sensor"
1919
print(bthome.device_name)
20-
print(hexlify(bluetooth_mac, ':').decode().upper())
20+
print(hexlify(bluetooth_mac, ":").decode().upper())
21+
2122

2223
async def read_sensor():
2324
bthome.temperature = 25 # Mocked up data for testing purposes.
@@ -26,12 +27,15 @@ async def read_sensor():
2627
print("Going to sleep.")
2728
deepsleep(SLEEP_TIME_SECS * 1000) # Helps mitigate sensor self-heating.
2829

30+
2931
async def communicate_readings():
3032
print("Constructing advertising payload")
3133
await aioble.advertise(
3234
BLE_ADV_INTERVAL_uS,
33-
adv_data = bthome.pack_advertisement(bthome.TEMPERATURE_SINT16, bthome.HUMIDITY_UINT16),
34-
connectable = False
35+
adv_data=bthome.pack_advertisement(
36+
bthome.TEMPERATURE_SINT16, bthome.HUMIDITY_UINT16
37+
),
38+
connectable=False,
3539
)
3640

3741

@@ -40,4 +44,5 @@ async def main():
4044
task2 = asyncio.create_task(communicate_readings())
4145
await asyncio.gather(task1, task2)
4246

47+
4348
asyncio.run(main())

0 commit comments

Comments
 (0)