Debian Codegraph @

tests/test_parser_v2.py

raw symbols SCIP
1 """Tests for the parser of BLE advertisements in BTHome V2 format."""
2
3 import logging
4 from datetime import UTC, datetime
5 from unittest.mock import patch
6
7 import pytest
8 from bluetooth_sensor_state_data import SensorUpdate
9 from habluetooth import BluetoothServiceInfoBleak
10 from sensor_state_data import (
11 BinarySensorDescription,
12 BinarySensorDeviceClass,
13 BinarySensorValue,
14 DeviceKey,
15 Event,
16 SensorDescription,
17 SensorDeviceClass,
18 SensorDeviceInfo,
19 SensorValue,
20 Units,
21 )
22
23 from bthome_ble.const import ExtendedSensorDeviceClass
24 from bthome_ble.parser import BTHomeBluetoothDeviceData, BTHomeVersion, EncryptionScheme
25
26 ADVERTISEMENT_TIME = 1709331995.5181565
27
28 KEY_ACCELERATION = DeviceKey(key="acceleration", device_id=None)
29 KEY_BATTERY = DeviceKey(key="battery", device_id=None)
30 KEY_BINARY_GENERIC = DeviceKey(key="generic", device_id=None)
31 KEY_BINARY_OPENING = DeviceKey(key="opening", device_id=None)
32 KEY_BINARY_MOTION = DeviceKey(key="motion", device_id=None)
33 KEY_BINARY_POWER = DeviceKey(key="power", device_id=None)
34 KEY_BINARY_WINDOW = DeviceKey(key="window", device_id=None)
35 KEY_BUTTON = DeviceKey(key="button", device_id=None)
36 KEY_CO2 = DeviceKey(key="carbon_dioxide", device_id=None)
37 KEY_COMMAND = DeviceKey(key="command", device_id=None)
38 KEY_DIMMER = DeviceKey(key="dimmer", device_id=None)
39 KEY_DISTANCE = DeviceKey(key="distance", device_id=None)
40 KEY_COUNT = DeviceKey(key="count", device_id=None)
41 KEY_CURRENT = DeviceKey(key="current", device_id=None)
42 KEY_DEW_POINT = DeviceKey(key="dew_point", device_id=None)
43 KEY_DURATION = DeviceKey(key="duration", device_id=None)
44 KEY_ENERGY = DeviceKey(key="energy", device_id=None)
45 KEY_GAS = DeviceKey(key="gas", device_id=None)
46 KEY_GYROSCOPE = DeviceKey(key="gyroscope", device_id=None)
47 KEY_HUMIDITY = DeviceKey(key="humidity", device_id=None)
48 KEY_ILLUMINANCE = DeviceKey(key="illuminance", device_id=None)
49 KEY_LIGHT = DeviceKey(key="light", device_id=None)
50 KEY_MASS = DeviceKey(key="mass", device_id=None)
51 KEY_MOISTURE = DeviceKey(key="moisture", device_id=None)
52 KEY_PACKET_ID = DeviceKey(key="packet_id", device_id=None)
53 KEY_PM25 = DeviceKey(key="pm25", device_id=None)
54 KEY_PM10 = DeviceKey(key="pm10", device_id=None)
55 KEY_POWER = DeviceKey(key="power", device_id=None)
56 KEY_PRESSURE = DeviceKey(key="pressure", device_id=None)
57 KEY_RAW = DeviceKey(key="raw", device_id=None)
58 KEY_ROTATION = DeviceKey(key="rotation", device_id=None)
59 KEY_SIGNAL_STRENGTH = DeviceKey(key="signal_strength", device_id=None)
60 KEY_SPEED = DeviceKey(key="speed", device_id=None)
61 KEY_TEMPERATURE = DeviceKey(key="temperature", device_id=None)
62 KEY_TEXT = DeviceKey(key="text", device_id=None)
63 KEY_TIMESTAMP = DeviceKey(key="timestamp", device_id=None)
64 KEY_UV_INDEX = DeviceKey(key="uv_index", device_id=None)
65 KEY_VOC = DeviceKey(key="volatile_organic_compounds", device_id=None)
66 KEY_VOLTAGE = DeviceKey(key="voltage", device_id=None)
67 KEY_VOLUME = DeviceKey(key="volume", device_id=None)
68 KEY_VOLUME_FLOW_RATE = DeviceKey(key="volume_flow_rate", device_id=None)
69 KEY_VOLUME_STORAGE = DeviceKey(key="volume_storage", device_id=None)
70 KEY_WATER = DeviceKey(key="water", device_id=None)
71 KEY_CONDUCTIVITY = DeviceKey(key="conductivity", device_id=None)
72 KEY_LIGHT_LEVEL = DeviceKey(key="light_level", device_id=None)
73 KEY_SETTINGS_REVISION = DeviceKey(key="settings_revision", device_id=None)
74
75
76 @pytest.fixture(autouse=True)
77 def logging_config(caplog):
78 caplog.set_level(logging.DEBUG)
79
80
81 @pytest.fixture(autouse=True)
82 def mock_platform():
83 with patch("sys.platform") as p:
84 p.return_value = "linux"
85 yield p
86
87
88 def bytes_to_service_info(
89 payload: bytes,
90 local_name: str,
91 address: str = "00:00:00:00:00:00",
92 time: float = ADVERTISEMENT_TIME,
93 ) -> BluetoothServiceInfoBleak:
94 """Convert bytes to service info"""
95 return BluetoothServiceInfoBleak(
96 name=local_name,
97 address=address,
98 rssi=-60,
99 manufacturer_data={},
100 service_data={"0000fcd2-0000-1000-8000-00805f9b34fb": payload},
101 service_uuids=[],
102 source="",
103 device=None,
104 advertisement=None,
105 connectable=False,
106 time=time,
107 tx_power=None,
108 )
109
110
111 def test_can_create():
112 BTHomeBluetoothDeviceData()
113
114
115 def test_encryption_key_needed():
116 """Test that we can detect that an encryption key is needed."""
117 data_string = (
118 b"\x41\xd2\xfc\xef\xe5\x2e\xb2\x12\xd6\x00\x11\x22\x33\xbc\x38\xc9\x66"
119 )
120 advertisement = bytes_to_service_info(
121 payload=data_string,
122 local_name="ATC_8D18B2",
123 address="A4:C1:38:8D:18:B2",
124 )
125
126 device = BTHomeBluetoothDeviceData()
127
128 assert device.supported(advertisement)
129 assert device.encryption_scheme == EncryptionScheme.BTHOME_BINDKEY
130 assert not device.bindkey_verified
131
132
133 def test_encryption_no_key_needed():
134 """Test that we can detect that no encryption key is needed."""
135 data_string = b"\x40\x04\x13\x8a\x01"
136 advertisement = bytes_to_service_info(
137 payload=data_string,
138 local_name="ATC_8D18B2",
139 address="A4:C1:38:8D:18:B2",
140 )
141
142 device = BTHomeBluetoothDeviceData()
143
144 assert device.supported(advertisement)
145 assert device.encryption_scheme == EncryptionScheme.NONE
146 assert not device.bindkey_verified
147
148
149 def test_encryption_downgrade_fails():
150 """Test that unencrypted data gets rejected when a bindkey is provided."""
151 bindkey = "814aac74c4f17b6c1581e1ab87816b99"
152 data_string = b"\x40\x04\x13\x8a\x01"
153 advertisement = bytes_to_service_info(
154 payload=data_string,
155 local_name="ATC_8D18B2",
156 address="A4:C1:38:8D:18:B2",
157 )
158
159 device = BTHomeBluetoothDeviceData(bindkey=bytes.fromhex(bindkey))
160
161 assert not device.supported(advertisement)
162 assert not device.bindkey_verified
163
164
165 def test_downgrade_detected_flag_v2():
166 """Test that downgrade_detected flag is set correctly for v2."""
167 bindkey = "231d39c1d7cc1ab1aee224cd096db932"
168
169 # Encrypted v2 advertisement
170 encrypted_data = b"\x41\xc8i\xd3~\xa5\xf8\x8e'\x16)\xec\xf2z\x85"
171 encrypted_adv = bytes_to_service_info(
172 payload=encrypted_data,
173 local_name="TEST DEVICE",
174 address="54:48:E6:8F:80:A5",
175 )
176
177 # Unencrypted v2 advertisement
178 unencrypted_data = b"\x40\x04\x13\x8a\x01"
179 unencrypted_adv = bytes_to_service_info(
180 payload=unencrypted_data,
181 local_name="TEST DEVICE",
182 address="54:48:E6:8F:80:A5",
183 )
184
185 device = BTHomeBluetoothDeviceData(bindkey=bytes.fromhex(bindkey))
186
187 # Encrypted → should not set flag
188 assert device.supported(encrypted_adv)
189 device.update(encrypted_adv)
190 assert not device.downgrade_detected
191
192 # Unencrypted → should set flag
193 # Note: supported() still returns True (it's valid BTHome format)
194 # but update() blocks the data and sets the flag
195 device.update(unencrypted_adv)
196 assert device.downgrade_detected
197
198 # Encrypted resumes → should clear flag
199 device.update(encrypted_adv) # type: ignore[unreachable] # update() changes flag
200 assert not device.downgrade_detected
201
202
203 def test_sleepy_device():
204 """Test that we can detect that a device that doesn't update regularly."""
205 data_string = b"\x44\x04\x13\x8a\x01"
206 advertisement = bytes_to_service_info(
207 payload=data_string,
208 local_name="ATC_8D18B2",
209 address="A4:C1:38:8D:18:B2",
210 )
211
212 device = BTHomeBluetoothDeviceData()
213
214 assert device.supported(advertisement)
215 assert device.sleepy_device
216
217
218 def test_no_sleepy_device():
219 """Test that we can detect that a device that updates regularly."""
220 data_string = b"\x40\x04\x13\x8a\x01"
221 advertisement = bytes_to_service_info(
222 payload=data_string,
223 local_name="ATC_8D18B2",
224 address="A4:C1:38:8D:18:B2",
225 )
226
227 device = BTHomeBluetoothDeviceData()
228
229 assert device.supported(advertisement)
230 assert not device.sleepy_device
231
232
233 def test_mac_as_name():
234 """
235 A sensor without a name gets its MAC address as name from BluetoothServiceInfo.
236 Test that this sensor has BTHome sensor + identifier as its name.
237 """
238 data_string = b"\x40\x04\x13\x8a\x01"
239 advertisement = bytes_to_service_info(
240 payload=data_string,
241 local_name="A4:C1:38:8D:18:B2",
242 address="A4:C1:38:8D:18:B2",
243 )
244
245 device = BTHomeBluetoothDeviceData()
246
247 assert device.supported(advertisement)
248 assert device.update(advertisement) == SensorUpdate(
249 title="BTHome sensor 18B2",
250 devices={
251 None: SensorDeviceInfo(
252 name="BTHome sensor 18B2",
253 manufacturer=None,
254 model="BTHome sensor",
255 sw_version="BTHome BLE v2",
256 hw_version=None,
257 )
258 },
259 entity_descriptions={
260 KEY_PRESSURE: SensorDescription(
261 device_key=KEY_PRESSURE,
262 device_class=SensorDeviceClass.PRESSURE,
263 native_unit_of_measurement=Units.PRESSURE_MBAR,
264 ),
265 KEY_SIGNAL_STRENGTH: SensorDescription(
266 device_key=KEY_SIGNAL_STRENGTH,
267 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
268 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
269 ),
270 },
271 entity_values={
272 KEY_PRESSURE: SensorValue(
273 device_key=KEY_PRESSURE, name="Pressure", native_value=1008.83
274 ),
275 KEY_SIGNAL_STRENGTH: SensorValue(
276 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
277 ),
278 },
279 )
280
281
282 def test_has_incorrect_version():
283 """Test that we can detect a non-existing version v7."""
284 data_string = b"\xe1\x02\x00\x0c\x04\x04\x13\x8a\x01"
285 advertisement = bytes_to_service_info(
286 payload=data_string,
287 local_name="A4:C1:38:8D:18:B2",
288 address="A4:C1:38:8D:18:B2",
289 )
290
291 device = BTHomeBluetoothDeviceData()
292
293 assert not device.supported(advertisement)
294
295
296 def test_bindkey_wrong():
297 """Test BTHome parser with wrong encryption key."""
298 bindkey = "814aac74c4f17b6c1581e1ab87816b99"
299 data_string = (
300 b"\x41\xd2\xfc\xef\xe5\x2e\xb2\x12\xd6\x00\x11\x22\x33\xbc\x38\xc9\x66"
301 )
302 advertisement = bytes_to_service_info(
303 data_string,
304 local_name="Test Sensor",
305 address="A4:C1:38:8D:18:B2",
306 )
307
308 device = BTHomeBluetoothDeviceData(bindkey=bytes.fromhex(bindkey))
309 assert device.supported(advertisement)
310 assert not device.bindkey_verified
311 assert device.decryption_failed
312 assert device.update(advertisement) == SensorUpdate(
313 title="Test Sensor 18B2",
314 devices={
315 None: SensorDeviceInfo(
316 name="Test Sensor 18B2",
317 manufacturer=None,
318 model="BTHome sensor",
319 sw_version="BTHome BLE v2 (encrypted)",
320 hw_version=None,
321 )
322 },
323 entity_descriptions={
324 KEY_SIGNAL_STRENGTH: SensorDescription(
325 device_key=KEY_SIGNAL_STRENGTH,
326 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
327 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
328 ),
329 },
330 entity_values={
331 KEY_SIGNAL_STRENGTH: SensorValue(
332 name="Signal Strength", device_key=KEY_SIGNAL_STRENGTH, native_value=-60
333 ),
334 },
335 )
336
337
338 def test_bindkey_correct():
339 """Test BTHome parser with correct encryption key."""
340 bindkey = "231d39c1d7cc1ab1aee224cd096db932"
341 data_string = b"\x41\xa4\x72\x66\xc9\x5f\x73\x00\x11\x22\x33\x78\x23\x72\x14"
342 advertisement = bytes_to_service_info(
343 data_string,
344 local_name="SBHT-003C",
345 address="54:48:E6:8F:80:A5",
346 )
347
348 device = BTHomeBluetoothDeviceData(bindkey=bytes.fromhex(bindkey))
349 assert device.supported(advertisement)
350 assert device.bindkey_verified
351 assert not device.decryption_failed
352 assert device.update(advertisement) == SensorUpdate(
353 title="Shelly BLU H&T 80A5",
354 devices={
355 None: SensorDeviceInfo(
356 name="Shelly BLU H&T 80A5",
357 manufacturer="Shelly",
358 model="BLU H&T",
359 sw_version="BTHome BLE v2 (encrypted)",
360 hw_version=None,
361 )
362 },
363 entity_descriptions={
364 KEY_TEMPERATURE: SensorDescription(
365 device_key=KEY_TEMPERATURE,
366 device_class=SensorDeviceClass.TEMPERATURE,
367 native_unit_of_measurement=Units.TEMP_CELSIUS,
368 ),
369 KEY_HUMIDITY: SensorDescription(
370 device_key=KEY_HUMIDITY,
371 device_class=SensorDeviceClass.HUMIDITY,
372 native_unit_of_measurement=Units.PERCENTAGE,
373 ),
374 KEY_SIGNAL_STRENGTH: SensorDescription(
375 device_key=KEY_SIGNAL_STRENGTH,
376 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
377 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
378 ),
379 },
380 entity_values={
381 KEY_TEMPERATURE: SensorValue(
382 device_key=KEY_TEMPERATURE, name="Temperature", native_value=25.06
383 ),
384 KEY_HUMIDITY: SensorValue(
385 device_key=KEY_HUMIDITY, name="Humidity", native_value=50.55
386 ),
387 KEY_SIGNAL_STRENGTH: SensorValue(
388 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
389 ),
390 },
391 )
392
393
394 def test_shelly_blu_ht_display_zb():
395 """Test BTHome parser for Shelly BLU H&T Display ZB."""
396 data_string = b"\x40\x00\x2e\x01\x64\x1e\x01\x2e\x34\x45\x16\x01"
397 advertisement = bytes_to_service_info(
398 data_string,
399 local_name="SBHT-103C",
400 address="54:48:E6:8F:80:A5",
401 )
402
403 device = BTHomeBluetoothDeviceData()
404 assert device.supported(advertisement)
405 assert device.update(advertisement) == SensorUpdate(
406 title="Shelly BLU H&T Display ZB 80A5",
407 devices={
408 None: SensorDeviceInfo(
409 name="Shelly BLU H&T Display ZB 80A5",
410 manufacturer="Shelly",
411 model="BLU H&T Display ZB",
412 sw_version="BTHome BLE v2",
413 hw_version=None,
414 )
415 },
416 entity_descriptions={
417 KEY_PACKET_ID: SensorDescription(
418 device_key=KEY_PACKET_ID,
419 device_class=SensorDeviceClass.PACKET_ID,
420 native_unit_of_measurement=None,
421 ),
422 KEY_BATTERY: SensorDescription(
423 device_key=KEY_BATTERY,
424 device_class=SensorDeviceClass.BATTERY,
425 native_unit_of_measurement=Units.PERCENTAGE,
426 ),
427 KEY_TEMPERATURE: SensorDescription(
428 device_key=KEY_TEMPERATURE,
429 device_class=SensorDeviceClass.TEMPERATURE,
430 native_unit_of_measurement=Units.TEMP_CELSIUS,
431 ),
432 KEY_HUMIDITY: SensorDescription(
433 device_key=KEY_HUMIDITY,
434 device_class=SensorDeviceClass.HUMIDITY,
435 native_unit_of_measurement=Units.PERCENTAGE,
436 ),
437 KEY_SIGNAL_STRENGTH: SensorDescription(
438 device_key=KEY_SIGNAL_STRENGTH,
439 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
440 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
441 ),
442 },
443 entity_values={
444 KEY_PACKET_ID: SensorValue(
445 device_key=KEY_PACKET_ID, name="Packet Id", native_value=46
446 ),
447 KEY_BATTERY: SensorValue(
448 device_key=KEY_BATTERY, name="Battery", native_value=100
449 ),
450 KEY_TEMPERATURE: SensorValue(
451 device_key=KEY_TEMPERATURE, name="Temperature", native_value=27.8
452 ),
453 KEY_HUMIDITY: SensorValue(
454 device_key=KEY_HUMIDITY, name="Humidity", native_value=52
455 ),
456 KEY_SIGNAL_STRENGTH: SensorValue(
457 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
458 ),
459 },
460 binary_entity_descriptions={
461 KEY_LIGHT: BinarySensorDescription(
462 device_key=KEY_LIGHT,
463 device_class=BinarySensorDeviceClass.LIGHT,
464 )
465 },
466 binary_entity_values={
467 KEY_LIGHT: BinarySensorValue(
468 device_key=KEY_LIGHT, name="Light", native_value=True
469 ),
470 },
471 )
472
473
474 def test_incorrect_bindkey_length(caplog):
475 """Test BTHome parser with incorrect encryption key length."""
476 bindkey = (
477 "231d39c1d7cc1ab1aee224cd096db932231d39c1d7cc1ab1" # 24 bytes instead of 16
478 )
479 data_string = b"\x41\xa4\x72\x66\xc9\x5f\x73\x00\x11\x22\x33\x78\x23\x72\x14"
480 advertisement = bytes_to_service_info(
481 data_string,
482 local_name="TEST DEVICE",
483 address="54:48:E6:8F:80:A5",
484 )
485
486 device = BTHomeBluetoothDeviceData(bindkey=bytes.fromhex(bindkey))
487 assert device.supported(advertisement)
488 assert not device.bindkey_verified
489 assert device.decryption_failed
490 assert (
491 "TEST DEVICE 80A5: Encryption key should be 16 bytes (32 characters) long"
492 in caplog.text
493 )
494
495
496 def test_bindkey_set_late():
497 """Test BTHome parser with correct encryption key set after the device is created."""
498 bindkey = "231d39c1d7cc1ab1aee224cd096db932"
499 data_string = b"\x41\xa4\x72\x66\xc9\x5f\x73\x00\x11\x22\x33\x78\x23\x72\x14"
500 advertisement = bytes_to_service_info(
501 data_string,
502 local_name="TEST DEVICE",
503 address="54:48:E6:8F:80:A5",
504 )
505
506 device = BTHomeBluetoothDeviceData()
507 device.set_bindkey(bytes.fromhex(bindkey))
508 assert device.supported(advertisement)
509 assert device.bindkey_verified
510 assert device.update(advertisement) == SensorUpdate(
511 title="TEST DEVICE 80A5",
512 devices={
513 None: SensorDeviceInfo(
514 name="TEST DEVICE 80A5",
515 manufacturer=None,
516 model="BTHome sensor",
517 sw_version="BTHome BLE v2 (encrypted)",
518 hw_version=None,
519 )
520 },
521 entity_descriptions={
522 KEY_TEMPERATURE: SensorDescription(
523 device_key=KEY_TEMPERATURE,
524 device_class=SensorDeviceClass.TEMPERATURE,
525 native_unit_of_measurement=Units.TEMP_CELSIUS,
526 ),
527 KEY_HUMIDITY: SensorDescription(
528 device_key=KEY_HUMIDITY,
529 device_class=SensorDeviceClass.HUMIDITY,
530 native_unit_of_measurement=Units.PERCENTAGE,
531 ),
532 KEY_SIGNAL_STRENGTH: SensorDescription(
533 device_key=KEY_SIGNAL_STRENGTH,
534 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
535 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
536 ),
537 },
538 entity_values={
539 KEY_TEMPERATURE: SensorValue(
540 device_key=KEY_TEMPERATURE, name="Temperature", native_value=25.06
541 ),
542 KEY_HUMIDITY: SensorValue(
543 device_key=KEY_HUMIDITY, name="Humidity", native_value=50.55
544 ),
545 KEY_SIGNAL_STRENGTH: SensorValue(
546 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
547 ),
548 },
549 )
550
551
552 def test_bindkey_verified_can_be_unset():
553 """Test BTHome parser with wrong encryption key."""
554 bindkey = "814aac74c4f17b6c1581e1ab87816b99"
555 data_string = b"\x41\xa4\x72\x66\xc9\x5f\x73\x00\x11\x22\x33\x78\x23\x72\x14"
556 advertisement = bytes_to_service_info(
557 data_string,
558 local_name="ATC_8D18B2",
559 address="A4:C1:38:8D:18:B2",
560 )
561
562 device = BTHomeBluetoothDeviceData(bindkey=bytes.fromhex(bindkey))
563 device.bindkey_verified = True
564 device.decryption_failed = False
565
566 assert device.supported(advertisement)
567 # the first advertisement will fail decryption, but we don't ask to reauth yet
568 assert device.bindkey_verified
569 assert device.decryption_failed
570
571 data_string = b"\x41\xa4\x72\x66\xc9\x5f\x73\x01\x11\x22\x33\x78\x23\x72\x14"
572 advertisement = bytes_to_service_info(
573 data_string,
574 local_name="ATC_8D18B2",
575 address="A4:C1:38:8D:18:B2",
576 )
577 assert device.supported(advertisement)
578 # the second advertisement will fail decryption again, but now we ask to reauth
579 assert device.decryption_failed
580 assert not device.bindkey_verified
581
582
583 def test_same_service_data(caplog):
584 """Test BTHome parser with the same encryption counter."""
585 bindkey = "231d39c1d7cc1ab1aee224cd096db932"
586 data_string = b"\x41\xe4\x45\xf3\xc9\x96\x2b\x33\x22\x11\x00\x6c\x7c\x45\x19"
587 advertisement = bytes_to_service_info(
588 data_string,
589 local_name="TEST DEVICE",
590 address="54:48:E6:8F:80:A5",
591 )
592
593 device = BTHomeBluetoothDeviceData(bindkey=bytes.fromhex(bindkey))
594 assert device.supported(advertisement)
595 assert device.bindkey_verified
596 assert device.encryption_counter == 1122867
597
598 data_string = b"\x41\xe4\x45\xf3\xc9\x96\x2b\x33\x22\x11\x00\x6c\x7c\x45\x19"
599 advertisement = bytes_to_service_info(
600 data_string,
601 local_name="TEST DEVICE",
602 address="54:48:E6:8F:80:A5",
603 time=ADVERTISEMENT_TIME + 5,
604 )
605 assert device.supported(advertisement)
606 assert device.bindkey_verified
607 assert device.encryption_counter == 1122867
608 assert (
609 "TEST DEVICE 80A5: The encrypted service data is the same as the previous "
610 "service data. Skipping this BLE advertisement." in caplog.text
611 )
612
613
614 def test_increasing_encryption_counter(caplog):
615 """Test BTHome parser with increasing encryption counter."""
616 bindkey = "231d39c1d7cc1ab1aee224cd096db932"
617 data_string = b"\x41\xe4\x45\xf3\xc9\x96\x2b\x33\x22\x11\x00\x6c\x7c\x45\x19"
618 advertisement = bytes_to_service_info(
619 data_string,
620 local_name="TEST DEVICE",
621 address="54:48:E6:8F:80:A5",
622 )
623
624 device = BTHomeBluetoothDeviceData(bindkey=bytes.fromhex(bindkey))
625 assert device.supported(advertisement)
626 assert device.bindkey_verified
627 assert device.encryption_counter == 1122867
628
629 data_string = b"\x41\x3e\x93\x2c\xc7\x17\x5f\x34\x22\x11\x00\x55\x38\x76\xaf"
630 advertisement = bytes_to_service_info(
631 data_string,
632 local_name="TEST DEVICE",
633 address="54:48:E6:8F:80:A5",
634 )
635 assert device.supported(advertisement)
636 assert device.bindkey_verified
637 assert device.encryption_counter == 1122868
638
639
640 def test_decreasing_encryption_counter(caplog):
641 """Test BTHome parser with decreasing encryption counter."""
642 bindkey = "231d39c1d7cc1ab1aee224cd096db932"
643 data_string = b"\x41\xe4\x45\xf3\xc9\x96\x2b\x33\x22\x11\x00\x6c\x7c\x45\x19"
644 advertisement = bytes_to_service_info(
645 data_string,
646 local_name="TEST DEVICE",
647 address="54:48:E6:8F:80:A5",
648 )
649
650 device = BTHomeBluetoothDeviceData(bindkey=bytes.fromhex(bindkey))
651 assert device.supported(advertisement)
652 assert device.bindkey_verified
653 assert device.encryption_counter == 1122867
654
655 data_string = b"\x41\x72\x3d\x30\x35\xfb\x88\x32\x22\x11\x00\x9e\x74\x14\xc0"
656 advertisement = bytes_to_service_info(
657 data_string,
658 local_name="TEST DEVICE",
659 address="54:48:E6:8F:80:A5",
660 )
661 assert device.supported(advertisement)
662 assert device.bindkey_verified
663 # encryption counter should not be updated as it is lower
664 assert device.encryption_counter == 1122867
665 assert (
666 "TEST DEVICE 80A5: The new encryption counter (1122866) is smaller than the "
667 "previous value (1122867). The data might be compromised. BLE advertisement will "
668 "be skipped." in caplog.text
669 )
670
671
672 def test_manual_reset_encryption_counter(caplog):
673 """Test that reset_encryption_counter() accepts an otherwise lower counter.
674
675 Simulates a battery replacement on a device that seeds the AES-CCM
676 counter with a random value: the new counter is lower than the tracked
677 one, so the next advertisement is normally dropped as a replay. After
678 calling reset_encryption_counter(), the advertisement is accepted.
679 """
680 bindkey = "231d39c1d7cc1ab1aee224cd096db932"
681 high_counter_adv = b"\x41\xe4\x45\xf3\xc9\x96\x2b\x33\x22\x11\x00\x6c\x7c\x45\x19"
682 advertisement = bytes_to_service_info(
683 high_counter_adv,
684 local_name="TEST DEVICE",
685 address="54:48:E6:8F:80:A5",
686 )
687
688 device = BTHomeBluetoothDeviceData(bindkey=bytes.fromhex(bindkey))
689 assert device.supported(advertisement)
690 assert device.bindkey_verified
691 assert device.encryption_counter == 1122867
692
693 lower_counter_adv = b"\x41\x72\x3d\x30\x35\xfb\x88\x32\x22\x11\x00\x9e\x74\x14\xc0"
694 advertisement = bytes_to_service_info(
695 lower_counter_adv,
696 local_name="TEST DEVICE",
697 address="54:48:E6:8F:80:A5",
698 )
699 assert device.supported(advertisement)
700 # rejected — counter dropped from 1122867 to 1122866
701 assert device.encryption_counter == 1122867
702
703 device.reset_encryption_counter()
704 assert device.encryption_counter == 0
705 # bindkey is still valid — only the replay-protection counter was reset
706 assert device.bindkey_verified
707
708 advertisement = bytes_to_service_info(
709 lower_counter_adv,
710 local_name="TEST DEVICE",
711 address="54:48:E6:8F:80:A5",
712 )
713 assert device.supported(advertisement)
714 assert device.bindkey_verified
715 assert device.encryption_counter == 1122866
716
717
718 def test_reset_encryption_counter(caplog):
719 """Test BTHome parser during reset of the encryption counter."""
720 bindkey = "231d39c1d7cc1ab1aee224cd096db932"
721 data_string = b"\x41\xba\x0d\xb0\x7a\xee\xf6\xff\xff\xff\xff\x21\xfd\x46\x00"
722 advertisement = bytes_to_service_info(
723 data_string,
724 local_name="TEST DEVICE",
725 address="54:48:E6:8F:80:A5",
726 )
727
728 device = BTHomeBluetoothDeviceData(bindkey=bytes.fromhex(bindkey))
729 assert device.supported(advertisement)
730 assert device.bindkey_verified
731 assert device.encryption_counter == 4294967295
732
733 data_string = b"\x41\xde\x40\xb5\x0e\x67\x66\x00\x00\x00\x00\xd7\xcb\x95\xde"
734 advertisement = bytes_to_service_info(
735 data_string,
736 local_name="TEST DEVICE",
737 address="54:48:E6:8F:80:A5",
738 )
739 assert device.supported(advertisement)
740 assert device.bindkey_verified
741 assert device.encryption_counter == 0
742
743
744 def test_too_short_encryption_advertisement(caplog):
745 """Test BTHome parser with a too short encrypted advertisement."""
746 bindkey = "231d39c1d7cc1ab1aee224cd096db932"
747 data_string = b"\x41\xba\x0d\xb0\x7a\xee\xf6\x00"
748 advertisement = bytes_to_service_info(
749 data_string,
750 local_name="TEST DEVICE",
751 address="54:48:E6:8F:80:A5",
752 )
753
754 device = BTHomeBluetoothDeviceData(bindkey=bytes.fromhex(bindkey))
755 assert device.supported(advertisement)
756 assert "TEST DEVICE 80A5: Invalid data length (for decryption), adv:" in caplog.text
757
758
759 def test_identical_packet_id(caplog):
760 """Test BTHome parser for skipping BLE advertisement with identical or older packet id."""
761 # 1st advertisement
762 data_string = b"\x40\x00\x09"
763 advertisement = bytes_to_service_info(
764 data_string, local_name="ATC_8D18B2", address="A4:C1:38:8D:18:B2"
765 )
766
767 device = BTHomeBluetoothDeviceData()
768 assert device.update(advertisement) == SensorUpdate(
769 title="ATC 18B2",
770 devices={
771 None: SensorDeviceInfo(
772 name="ATC 18B2",
773 manufacturer="Xiaomi",
774 model="Temperature/Humidity sensor",
775 sw_version="BTHome BLE v2",
776 hw_version=None,
777 )
778 },
779 entity_descriptions={
780 KEY_PACKET_ID: SensorDescription(
781 device_key=KEY_PACKET_ID,
782 device_class=SensorDeviceClass.PACKET_ID,
783 native_unit_of_measurement=None,
784 ),
785 KEY_SIGNAL_STRENGTH: SensorDescription(
786 device_key=KEY_SIGNAL_STRENGTH,
787 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
788 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
789 ),
790 },
791 entity_values={
792 KEY_PACKET_ID: SensorValue(
793 device_key=KEY_PACKET_ID, name="Packet Id", native_value=9
794 ),
795 KEY_SIGNAL_STRENGTH: SensorValue(
796 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
797 ),
798 },
799 )
800 assert device.packet_id == 9
801 assert "ATC 18B2: First packet, not filtering packet_id 9" in caplog.text
802
803 # advertisement with the same packet id
804 device.update(advertisement)
805 assert device.packet_id == 9
806 assert (
807 "ATC 18B2: New packet_id 9 indicates an older packet (previous packet_id 9). "
808 "BLE advertisement will be skipped" in caplog.text
809 )
810
811 # advertisement packet id decreased by one, should be dropped
812 data_string_2 = b"\x40\x00\x08"
813 advertisement_2 = bytes_to_service_info(
814 data_string_2, local_name="ATC_8D18B2", address="A4:C1:38:8D:18:B2"
815 )
816
817 assert device.update(advertisement_2)
818 assert device.packet_id == 9
819 assert (
820 "ATC 18B2: New packet_id 8 indicates an older packet (previous packet_id 9). "
821 "BLE advertisement will be skipped" in caplog.text
822 )
823
824 # advertisement more than 4 seconds from last advertisement
825 advertisement_3 = bytes_to_service_info(
826 data_string,
827 local_name="ATC_8D18B2",
828 address="A4:C1:38:8D:18:B2",
829 time=ADVERTISEMENT_TIME + 5,
830 )
831
832 assert device.update(advertisement_3)
833 assert device.packet_id == 9
834 assert (
835 "ATC 18B2: Not filtering packet_id, more than 4 seconds since last packet."
836 in caplog.text
837 )
838
839
840 def test_increasing_packet_id(caplog):
841 """Test BTHome parser for BLE advertisement with increasing packet id."""
842 # start with 189
843 data_string = b"\x40\x00\xbd"
844 advertisement = bytes_to_service_info(
845 data_string, local_name="ATC_8D18B2", address="A4:C1:38:8D:18:B2"
846 )
847
848 device = BTHomeBluetoothDeviceData()
849 device.update(advertisement)
850 assert device.packet_id == 189
851
852 # increase by 1 to 190
853 data_string_2 = b"\x40\x00\xbe"
854 advertisement_2 = bytes_to_service_info(
855 data_string_2, local_name="ATC_8D18B2", address="A4:C1:38:8D:18:B2"
856 )
857
858 assert device.update(advertisement_2) == SensorUpdate(
859 title="ATC 18B2",
860 devices={
861 None: SensorDeviceInfo(
862 name="ATC 18B2",
863 manufacturer="Xiaomi",
864 model="Temperature/Humidity sensor",
865 sw_version="BTHome BLE v2",
866 hw_version=None,
867 )
868 },
869 entity_descriptions={
870 KEY_PACKET_ID: SensorDescription(
871 device_key=KEY_PACKET_ID,
872 device_class=SensorDeviceClass.PACKET_ID,
873 native_unit_of_measurement=None,
874 ),
875 KEY_SIGNAL_STRENGTH: SensorDescription(
876 device_key=KEY_SIGNAL_STRENGTH,
877 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
878 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
879 ),
880 },
881 entity_values={
882 KEY_PACKET_ID: SensorValue(
883 device_key=KEY_PACKET_ID, name="Packet Id", native_value=190
884 ),
885 KEY_SIGNAL_STRENGTH: SensorValue(
886 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
887 ),
888 },
889 )
890 assert device.packet_id == 190
891
892 # increast by 63 to 253
893 data_string_3 = b"\x40\x00\xfd"
894 advertisement_3 = bytes_to_service_info(
895 data_string_3, local_name="ATC_8D18B2", address="A4:C1:38:8D:18:B2"
896 )
897
898 device.update(advertisement_3)
899 assert device.packet_id == 253
900
901 # increast by 50, rollover to 47
902 data_string_4 = b"\x40\x00\x2f"
903 advertisement_4 = bytes_to_service_info(
904 data_string_4, local_name="ATC_8D18B2", address="A4:C1:38:8D:18:B2"
905 )
906
907 device.update(advertisement_4)
908 assert device.packet_id == 47
909
910
911 def test_bthome_wrong_object_id(caplog):
912 """Test BTHome parser for a non-existing Object ID xFE."""
913 data_string = b"\x40\xfe\xca\x09"
914 advertisement = bytes_to_service_info(
915 data_string, local_name="ATC_8D18B2", address="A4:C1:38:8D:18:B2"
916 )
917
918 device = BTHomeBluetoothDeviceData()
919 assert device.update(advertisement) == SensorUpdate(
920 title="ATC 18B2",
921 devices={
922 None: SensorDeviceInfo(
923 name="ATC 18B2",
924 manufacturer="Xiaomi",
925 model="Temperature/Humidity sensor",
926 sw_version="BTHome BLE v2",
927 hw_version=None,
928 )
929 },
930 entity_descriptions={
931 KEY_SIGNAL_STRENGTH: SensorDescription(
932 device_key=KEY_SIGNAL_STRENGTH,
933 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
934 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
935 ),
936 },
937 entity_values={
938 KEY_SIGNAL_STRENGTH: SensorValue(
939 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
940 ),
941 },
942 )
943
944
945 def test_bthome_battery_wrong_object_id_humidity(caplog):
946 """
947 Test BTHome parser for battery, wrong object id and humidity reading.
948 Should only return the battery reading, as humidity is after wrong object id.
949 """
950 data_string = b"\x40\x01\x5d\xfe\x5d\x09\x03\xb7\x18"
951 advertisement = bytes_to_service_info(
952 data_string, local_name="ATC_8D18B2", address="A4:C1:38:8D:18:B2"
953 )
954
955 device = BTHomeBluetoothDeviceData()
956 assert device.update(advertisement) == SensorUpdate(
957 title="ATC 18B2",
958 devices={
959 None: SensorDeviceInfo(
960 name="ATC 18B2",
961 manufacturer="Xiaomi",
962 model="Temperature/Humidity sensor",
963 sw_version="BTHome BLE v2",
964 hw_version=None,
965 )
966 },
967 entity_descriptions={
968 KEY_BATTERY: SensorDescription(
969 device_key=KEY_BATTERY,
970 device_class=SensorDeviceClass.BATTERY,
971 native_unit_of_measurement=Units.PERCENTAGE,
972 ),
973 KEY_SIGNAL_STRENGTH: SensorDescription(
974 device_key=KEY_SIGNAL_STRENGTH,
975 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
976 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
977 ),
978 },
979 entity_values={
980 KEY_BATTERY: SensorValue(
981 device_key=KEY_BATTERY, name="Battery", native_value=93
982 ),
983 KEY_SIGNAL_STRENGTH: SensorValue(
984 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
985 ),
986 },
987 )
988
989
990 def test_bthome_with_mac(caplog):
991 """Test BTHome parser for pressure reading mac address in payload."""
992 data_string = b"\x42\xb2\x18\x8d\x38\xc1\xa4\x04\x13\x8a\x01"
993 advertisement = bytes_to_service_info(
994 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
995 )
996
997 device = BTHomeBluetoothDeviceData()
998 assert device.update(advertisement) == SensorUpdate(
999 title="TEST DEVICE 18B2",
1000 devices={
1001 None: SensorDeviceInfo(
1002 name="TEST DEVICE 18B2",
1003 manufacturer=None,
1004 model="BTHome sensor",
1005 sw_version="BTHome BLE v2",
1006 hw_version=None,
1007 )
1008 },
1009 entity_descriptions={
1010 KEY_PRESSURE: SensorDescription(
1011 device_key=KEY_PRESSURE,
1012 device_class=SensorDeviceClass.PRESSURE,
1013 native_unit_of_measurement=Units.PRESSURE_MBAR,
1014 ),
1015 KEY_SIGNAL_STRENGTH: SensorDescription(
1016 device_key=KEY_SIGNAL_STRENGTH,
1017 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
1018 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
1019 ),
1020 },
1021 entity_values={
1022 KEY_PRESSURE: SensorValue(
1023 device_key=KEY_PRESSURE, name="Pressure", native_value=1008.83
1024 ),
1025 KEY_SIGNAL_STRENGTH: SensorValue(
1026 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
1027 ),
1028 },
1029 )
1030
1031
1032 def test_bthome_with_mac_encrypted():
1033 """Test BTHome parser with mac address in payload plus encryption."""
1034 bindkey = "231d39c1d7cc1ab1aee224cd096db932"
1035 data = b"\x43\xa5\x80\x8f\xe6\x48\x54\xf1\x96\xc0\x6f\xfb\x49\x00\x11\x22\x33\xf0\x8e\xcb\xde"
1036 advertisement = bytes_to_service_info(
1037 data,
1038 local_name="TEST DEVICE",
1039 address="54:48:E6:8F:80:A5",
1040 )
1041
1042 device = BTHomeBluetoothDeviceData(bindkey=bytes.fromhex(bindkey))
1043 assert device.supported(advertisement)
1044 assert device.bindkey_verified
1045 assert device.update(advertisement) == SensorUpdate(
1046 title="TEST DEVICE 80A5",
1047 devices={
1048 None: SensorDeviceInfo(
1049 name="TEST DEVICE 80A5",
1050 manufacturer=None,
1051 model="BTHome sensor",
1052 sw_version="BTHome BLE v2 (encrypted)",
1053 hw_version=None,
1054 )
1055 },
1056 entity_descriptions={
1057 KEY_TEMPERATURE: SensorDescription(
1058 device_key=KEY_TEMPERATURE,
1059 device_class=SensorDeviceClass.TEMPERATURE,
1060 native_unit_of_measurement=Units.TEMP_CELSIUS,
1061 ),
1062 KEY_HUMIDITY: SensorDescription(
1063 device_key=KEY_HUMIDITY,
1064 device_class=SensorDeviceClass.HUMIDITY,
1065 native_unit_of_measurement=Units.PERCENTAGE,
1066 ),
1067 KEY_SIGNAL_STRENGTH: SensorDescription(
1068 device_key=KEY_SIGNAL_STRENGTH,
1069 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
1070 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
1071 ),
1072 },
1073 entity_values={
1074 KEY_TEMPERATURE: SensorValue(
1075 device_key=KEY_TEMPERATURE, name="Temperature", native_value=25.06
1076 ),
1077 KEY_HUMIDITY: SensorValue(
1078 device_key=KEY_HUMIDITY, name="Humidity", native_value=50.55
1079 ),
1080 KEY_SIGNAL_STRENGTH: SensorValue(
1081 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
1082 ),
1083 },
1084 )
1085
1086
1087 def test_bthome_temperature_humidity(caplog):
1088 """Test BTHome parser for temperature humidity reading without encryption."""
1089 data_string = b"\x40\x02\xca\x09\x03\xbf\x13"
1090 advertisement = bytes_to_service_info(
1091 data_string, local_name="ATC_8D18B2", address="A4:C1:38:8D:18:B2"
1092 )
1093
1094 device = BTHomeBluetoothDeviceData()
1095 assert device.update(advertisement) == SensorUpdate(
1096 title="ATC 18B2",
1097 devices={
1098 None: SensorDeviceInfo(
1099 name="ATC 18B2",
1100 manufacturer="Xiaomi",
1101 model="Temperature/Humidity sensor",
1102 sw_version="BTHome BLE v2",
1103 hw_version=None,
1104 )
1105 },
1106 entity_descriptions={
1107 KEY_TEMPERATURE: SensorDescription(
1108 device_key=KEY_TEMPERATURE,
1109 device_class=SensorDeviceClass.TEMPERATURE,
1110 native_unit_of_measurement=Units.TEMP_CELSIUS,
1111 ),
1112 KEY_HUMIDITY: SensorDescription(
1113 device_key=KEY_HUMIDITY,
1114 device_class=SensorDeviceClass.HUMIDITY,
1115 native_unit_of_measurement=Units.PERCENTAGE,
1116 ),
1117 KEY_SIGNAL_STRENGTH: SensorDescription(
1118 device_key=KEY_SIGNAL_STRENGTH,
1119 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
1120 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
1121 ),
1122 },
1123 entity_values={
1124 KEY_TEMPERATURE: SensorValue(
1125 device_key=KEY_TEMPERATURE, name="Temperature", native_value=25.06
1126 ),
1127 KEY_HUMIDITY: SensorValue(
1128 device_key=KEY_HUMIDITY, name="Humidity", native_value=50.55
1129 ),
1130 KEY_SIGNAL_STRENGTH: SensorValue(
1131 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
1132 ),
1133 },
1134 )
1135
1136
1137 def test_bthome_packet_id_temperature_humidity_battery(caplog):
1138 """Test BTHome parser for packet_id, temperature, humidity and battery reading."""
1139 data_string = b"\x40\x00\x09\x01\x5d\x02\x5d\x09\x03\xb7\x18"
1140 advertisement = bytes_to_service_info(
1141 data_string, local_name="ATC_8D18B2", address="A4:C1:38:8D:18:B2"
1142 )
1143
1144 device = BTHomeBluetoothDeviceData()
1145 assert device.update(advertisement) == SensorUpdate(
1146 title="ATC 18B2",
1147 devices={
1148 None: SensorDeviceInfo(
1149 name="ATC 18B2",
1150 manufacturer="Xiaomi",
1151 model="Temperature/Humidity sensor",
1152 sw_version="BTHome BLE v2",
1153 hw_version=None,
1154 )
1155 },
1156 entity_descriptions={
1157 KEY_PACKET_ID: SensorDescription(
1158 device_key=KEY_PACKET_ID,
1159 device_class=SensorDeviceClass.PACKET_ID,
1160 native_unit_of_measurement=None,
1161 ),
1162 KEY_TEMPERATURE: SensorDescription(
1163 device_key=KEY_TEMPERATURE,
1164 device_class=SensorDeviceClass.TEMPERATURE,
1165 native_unit_of_measurement=Units.TEMP_CELSIUS,
1166 ),
1167 KEY_HUMIDITY: SensorDescription(
1168 device_key=KEY_HUMIDITY,
1169 device_class=SensorDeviceClass.HUMIDITY,
1170 native_unit_of_measurement=Units.PERCENTAGE,
1171 ),
1172 KEY_BATTERY: SensorDescription(
1173 device_key=KEY_BATTERY,
1174 device_class=SensorDeviceClass.BATTERY,
1175 native_unit_of_measurement=Units.PERCENTAGE,
1176 ),
1177 KEY_SIGNAL_STRENGTH: SensorDescription(
1178 device_key=KEY_SIGNAL_STRENGTH,
1179 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
1180 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
1181 ),
1182 },
1183 entity_values={
1184 KEY_PACKET_ID: SensorValue(
1185 device_key=KEY_PACKET_ID, name="Packet Id", native_value=9
1186 ),
1187 KEY_TEMPERATURE: SensorValue(
1188 device_key=KEY_TEMPERATURE, name="Temperature", native_value=23.97
1189 ),
1190 KEY_HUMIDITY: SensorValue(
1191 device_key=KEY_HUMIDITY, name="Humidity", native_value=63.27
1192 ),
1193 KEY_BATTERY: SensorValue(
1194 device_key=KEY_BATTERY, name="Battery", native_value=93
1195 ),
1196 KEY_SIGNAL_STRENGTH: SensorValue(
1197 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
1198 ),
1199 },
1200 )
1201
1202
1203 def test_bthome_pressure(caplog):
1204 """Test BTHome parser for pressure reading without encryption."""
1205 data_string = b"\x40\x04\x13\x8a\x01"
1206 advertisement = bytes_to_service_info(
1207 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
1208 )
1209
1210 device = BTHomeBluetoothDeviceData()
1211 assert device.update(advertisement) == SensorUpdate(
1212 title="TEST DEVICE 18B2",
1213 devices={
1214 None: SensorDeviceInfo(
1215 name="TEST DEVICE 18B2",
1216 manufacturer=None,
1217 model="BTHome sensor",
1218 sw_version="BTHome BLE v2",
1219 hw_version=None,
1220 )
1221 },
1222 entity_descriptions={
1223 KEY_PRESSURE: SensorDescription(
1224 device_key=KEY_PRESSURE,
1225 device_class=SensorDeviceClass.PRESSURE,
1226 native_unit_of_measurement=Units.PRESSURE_MBAR,
1227 ),
1228 KEY_SIGNAL_STRENGTH: SensorDescription(
1229 device_key=KEY_SIGNAL_STRENGTH,
1230 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
1231 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
1232 ),
1233 },
1234 entity_values={
1235 KEY_PRESSURE: SensorValue(
1236 device_key=KEY_PRESSURE, name="Pressure", native_value=1008.83
1237 ),
1238 KEY_SIGNAL_STRENGTH: SensorValue(
1239 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
1240 ),
1241 },
1242 )
1243
1244
1245 def test_bthome_illuminance(caplog):
1246 """Test BTHome parser for illuminance reading without encryption."""
1247 data_string = b"\x40\x05\x13\x8a\x14"
1248 advertisement = bytes_to_service_info(
1249 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
1250 )
1251
1252 device = BTHomeBluetoothDeviceData()
1253 assert device.update(advertisement) == SensorUpdate(
1254 title="TEST DEVICE 18B2",
1255 devices={
1256 None: SensorDeviceInfo(
1257 name="TEST DEVICE 18B2",
1258 manufacturer=None,
1259 model="BTHome sensor",
1260 sw_version="BTHome BLE v2",
1261 hw_version=None,
1262 )
1263 },
1264 entity_descriptions={
1265 KEY_ILLUMINANCE: SensorDescription(
1266 device_key=KEY_ILLUMINANCE,
1267 device_class=SensorDeviceClass.ILLUMINANCE,
1268 native_unit_of_measurement=Units.LIGHT_LUX,
1269 ),
1270 KEY_SIGNAL_STRENGTH: SensorDescription(
1271 device_key=KEY_SIGNAL_STRENGTH,
1272 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
1273 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
1274 ),
1275 },
1276 entity_values={
1277 KEY_ILLUMINANCE: SensorValue(
1278 device_key=KEY_ILLUMINANCE, name="Illuminance", native_value=13460.67
1279 ),
1280 KEY_SIGNAL_STRENGTH: SensorValue(
1281 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
1282 ),
1283 },
1284 )
1285
1286
1287 def test_bthome_mass_kilograms(caplog):
1288 """Test BTHome parser for mass reading in kilograms without encryption."""
1289 data_string = b"\x40\x06\x5e\x1f"
1290 advertisement = bytes_to_service_info(
1291 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
1292 )
1293
1294 device = BTHomeBluetoothDeviceData()
1295 assert device.update(advertisement) == SensorUpdate(
1296 title="TEST DEVICE 18B2",
1297 devices={
1298 None: SensorDeviceInfo(
1299 name="TEST DEVICE 18B2",
1300 manufacturer=None,
1301 model="BTHome sensor",
1302 sw_version="BTHome BLE v2",
1303 hw_version=None,
1304 )
1305 },
1306 entity_descriptions={
1307 KEY_MASS: SensorDescription(
1308 device_key=KEY_MASS,
1309 device_class=SensorDeviceClass.MASS,
1310 native_unit_of_measurement=Units.MASS_KILOGRAMS,
1311 ),
1312 KEY_SIGNAL_STRENGTH: SensorDescription(
1313 device_key=KEY_SIGNAL_STRENGTH,
1314 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
1315 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
1316 ),
1317 },
1318 entity_values={
1319 KEY_MASS: SensorValue(device_key=KEY_MASS, name="Mass", native_value=80.3),
1320 KEY_SIGNAL_STRENGTH: SensorValue(
1321 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
1322 ),
1323 },
1324 )
1325
1326
1327 def test_bthome_mass_pounds(caplog):
1328 """Test BTHome parser for mass reading in pounds without encryption."""
1329 data_string = b"\x40\x07\x3e\x1d"
1330 advertisement = bytes_to_service_info(
1331 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
1332 )
1333
1334 device = BTHomeBluetoothDeviceData()
1335 assert device.update(advertisement) == SensorUpdate(
1336 title="TEST DEVICE 18B2",
1337 devices={
1338 None: SensorDeviceInfo(
1339 name="TEST DEVICE 18B2",
1340 manufacturer=None,
1341 model="BTHome sensor",
1342 sw_version="BTHome BLE v2",
1343 hw_version=None,
1344 )
1345 },
1346 entity_descriptions={
1347 KEY_MASS: SensorDescription(
1348 device_key=KEY_MASS,
1349 device_class=SensorDeviceClass.MASS,
1350 native_unit_of_measurement=Units.MASS_POUNDS,
1351 ),
1352 KEY_SIGNAL_STRENGTH: SensorDescription(
1353 device_key=KEY_SIGNAL_STRENGTH,
1354 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
1355 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
1356 ),
1357 },
1358 entity_values={
1359 KEY_MASS: SensorValue(device_key=KEY_MASS, name="Mass", native_value=74.86),
1360 KEY_SIGNAL_STRENGTH: SensorValue(
1361 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
1362 ),
1363 },
1364 )
1365
1366
1367 def test_bthome_dew_point(caplog):
1368 """Test BTHome parser for dew point reading without encryption."""
1369 data_string = b"\x40\x08\xca\x06"
1370 advertisement = bytes_to_service_info(
1371 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
1372 )
1373
1374 device = BTHomeBluetoothDeviceData()
1375 assert device.update(advertisement) == SensorUpdate(
1376 title="TEST DEVICE 18B2",
1377 devices={
1378 None: SensorDeviceInfo(
1379 name="TEST DEVICE 18B2",
1380 manufacturer=None,
1381 model="BTHome sensor",
1382 sw_version="BTHome BLE v2",
1383 hw_version=None,
1384 )
1385 },
1386 entity_descriptions={
1387 KEY_DEW_POINT: SensorDescription(
1388 device_key=KEY_DEW_POINT,
1389 device_class=SensorDeviceClass.DEW_POINT,
1390 native_unit_of_measurement=Units.TEMP_CELSIUS,
1391 ),
1392 KEY_SIGNAL_STRENGTH: SensorDescription(
1393 device_key=KEY_SIGNAL_STRENGTH,
1394 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
1395 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
1396 ),
1397 },
1398 entity_values={
1399 KEY_DEW_POINT: SensorValue(
1400 device_key=KEY_DEW_POINT, name="Dew Point", native_value=17.38
1401 ),
1402 KEY_SIGNAL_STRENGTH: SensorValue(
1403 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
1404 ),
1405 },
1406 )
1407
1408
1409 def test_bthome_count(caplog):
1410 """Test BTHome parser for counter reading without encryption."""
1411 data_string = b"\x40\x09\x60"
1412 advertisement = bytes_to_service_info(
1413 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
1414 )
1415
1416 device = BTHomeBluetoothDeviceData()
1417 assert device.update(advertisement) == SensorUpdate(
1418 title="TEST DEVICE 18B2",
1419 devices={
1420 None: SensorDeviceInfo(
1421 name="TEST DEVICE 18B2",
1422 manufacturer=None,
1423 model="BTHome sensor",
1424 sw_version="BTHome BLE v2",
1425 hw_version=None,
1426 )
1427 },
1428 entity_descriptions={
1429 KEY_COUNT: SensorDescription(
1430 device_key=KEY_COUNT,
1431 device_class=SensorDeviceClass.COUNT,
1432 native_unit_of_measurement=None,
1433 ),
1434 KEY_SIGNAL_STRENGTH: SensorDescription(
1435 device_key=KEY_SIGNAL_STRENGTH,
1436 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
1437 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
1438 ),
1439 },
1440 entity_values={
1441 KEY_COUNT: SensorValue(device_key=KEY_COUNT, name="Count", native_value=96),
1442 KEY_SIGNAL_STRENGTH: SensorValue(
1443 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
1444 ),
1445 },
1446 )
1447
1448
1449 def test_bthome_energy(caplog):
1450 """Test BTHome parser for energy reading without encryption."""
1451 data_string = b"\x40\x0a\x13\x8a\x14"
1452 advertisement = bytes_to_service_info(
1453 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
1454 )
1455
1456 device = BTHomeBluetoothDeviceData()
1457 assert device.update(advertisement) == SensorUpdate(
1458 title="TEST DEVICE 18B2",
1459 devices={
1460 None: SensorDeviceInfo(
1461 name="TEST DEVICE 18B2",
1462 manufacturer=None,
1463 model="BTHome sensor",
1464 sw_version="BTHome BLE v2",
1465 hw_version=None,
1466 )
1467 },
1468 entity_descriptions={
1469 KEY_ENERGY: SensorDescription(
1470 device_key=KEY_ENERGY,
1471 device_class=SensorDeviceClass.ENERGY,
1472 native_unit_of_measurement=Units.ENERGY_KILO_WATT_HOUR,
1473 ),
1474 KEY_SIGNAL_STRENGTH: SensorDescription(
1475 device_key=KEY_SIGNAL_STRENGTH,
1476 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
1477 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
1478 ),
1479 },
1480 entity_values={
1481 KEY_ENERGY: SensorValue(
1482 device_key=KEY_ENERGY, name="Energy", native_value=1346.067
1483 ),
1484 KEY_SIGNAL_STRENGTH: SensorValue(
1485 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
1486 ),
1487 },
1488 )
1489
1490
1491 def test_bthome_power(caplog):
1492 """Test BTHome parser for power reading without encryption."""
1493 data_string = b"\x40\x0b\x02\x1b\x00"
1494 advertisement = bytes_to_service_info(
1495 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
1496 )
1497
1498 device = BTHomeBluetoothDeviceData()
1499 assert device.update(advertisement) == SensorUpdate(
1500 title="TEST DEVICE 18B2",
1501 devices={
1502 None: SensorDeviceInfo(
1503 name="TEST DEVICE 18B2",
1504 manufacturer=None,
1505 model="BTHome sensor",
1506 sw_version="BTHome BLE v2",
1507 hw_version=None,
1508 )
1509 },
1510 entity_descriptions={
1511 KEY_POWER: SensorDescription(
1512 device_key=KEY_POWER,
1513 device_class=SensorDeviceClass.POWER,
1514 native_unit_of_measurement=Units.POWER_WATT,
1515 ),
1516 KEY_SIGNAL_STRENGTH: SensorDescription(
1517 device_key=KEY_SIGNAL_STRENGTH,
1518 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
1519 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
1520 ),
1521 },
1522 entity_values={
1523 KEY_POWER: SensorValue(
1524 device_key=KEY_POWER, name="Power", native_value=69.14
1525 ),
1526 KEY_SIGNAL_STRENGTH: SensorValue(
1527 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
1528 ),
1529 },
1530 )
1531
1532
1533 def test_bthome_voltage(caplog):
1534 """Test BThome parser for voltage reading without encryption."""
1535 data_string = b"\x40\x0c\x02\x0c"
1536 advertisement = bytes_to_service_info(
1537 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
1538 )
1539
1540 device = BTHomeBluetoothDeviceData()
1541 assert device.update(advertisement) == SensorUpdate(
1542 title="TEST DEVICE 18B2",
1543 devices={
1544 None: SensorDeviceInfo(
1545 name="TEST DEVICE 18B2",
1546 manufacturer=None,
1547 model="BTHome sensor",
1548 sw_version="BTHome BLE v2",
1549 hw_version=None,
1550 )
1551 },
1552 entity_descriptions={
1553 KEY_VOLTAGE: SensorDescription(
1554 device_key=KEY_VOLTAGE,
1555 device_class=SensorDeviceClass.VOLTAGE,
1556 native_unit_of_measurement=Units.ELECTRIC_POTENTIAL_VOLT,
1557 ),
1558 KEY_SIGNAL_STRENGTH: SensorDescription(
1559 device_key=KEY_SIGNAL_STRENGTH,
1560 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
1561 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
1562 ),
1563 },
1564 entity_values={
1565 KEY_VOLTAGE: SensorValue(
1566 device_key=KEY_VOLTAGE, name="Voltage", native_value=3.074
1567 ),
1568 KEY_SIGNAL_STRENGTH: SensorValue(
1569 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
1570 ),
1571 },
1572 )
1573
1574
1575 def test_bthome_binary_sensor(caplog):
1576 """Test BTHome parser for binary sensor without device class, without encryption."""
1577 data_string = b"\x40\x0f\x01"
1578 advertisement = bytes_to_service_info(
1579 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
1580 )
1581
1582 device = BTHomeBluetoothDeviceData()
1583
1584 assert device.update(advertisement) == SensorUpdate(
1585 title="TEST DEVICE 18B2",
1586 devices={
1587 None: SensorDeviceInfo(
1588 name="TEST DEVICE 18B2",
1589 manufacturer=None,
1590 model="BTHome sensor",
1591 sw_version="BTHome BLE v2",
1592 hw_version=None,
1593 )
1594 },
1595 entity_descriptions={
1596 KEY_SIGNAL_STRENGTH: SensorDescription(
1597 device_key=KEY_SIGNAL_STRENGTH,
1598 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
1599 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
1600 ),
1601 },
1602 entity_values={
1603 KEY_SIGNAL_STRENGTH: SensorValue(
1604 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
1605 ),
1606 },
1607 binary_entity_descriptions={
1608 KEY_BINARY_GENERIC: BinarySensorDescription(
1609 device_key=KEY_BINARY_GENERIC,
1610 device_class=BinarySensorDeviceClass.GENERIC,
1611 ),
1612 },
1613 binary_entity_values={
1614 KEY_BINARY_GENERIC: BinarySensorValue(
1615 device_key=KEY_BINARY_GENERIC, name="Generic", native_value=True
1616 ),
1617 },
1618 )
1619
1620
1621 def test_bthome_binary_sensor_power(caplog):
1622 """Test BTHome parser for binary sensor power without encryption."""
1623 data_string = b"\x40\x10\x01"
1624 advertisement = bytes_to_service_info(
1625 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
1626 )
1627
1628 device = BTHomeBluetoothDeviceData()
1629
1630 assert device.update(advertisement) == SensorUpdate(
1631 title="TEST DEVICE 18B2",
1632 devices={
1633 None: SensorDeviceInfo(
1634 name="TEST DEVICE 18B2",
1635 manufacturer=None,
1636 model="BTHome sensor",
1637 sw_version="BTHome BLE v2",
1638 hw_version=None,
1639 )
1640 },
1641 entity_descriptions={
1642 KEY_SIGNAL_STRENGTH: SensorDescription(
1643 device_key=KEY_SIGNAL_STRENGTH,
1644 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
1645 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
1646 ),
1647 },
1648 entity_values={
1649 KEY_SIGNAL_STRENGTH: SensorValue(
1650 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
1651 ),
1652 },
1653 binary_entity_descriptions={
1654 KEY_BINARY_POWER: BinarySensorDescription(
1655 device_key=KEY_BINARY_POWER,
1656 device_class=BinarySensorDeviceClass.POWER,
1657 ),
1658 },
1659 binary_entity_values={
1660 KEY_BINARY_POWER: BinarySensorValue(
1661 device_key=KEY_BINARY_POWER, name="Power", native_value=True
1662 ),
1663 },
1664 )
1665
1666
1667 def test_bthome_binary_sensor_opening(caplog):
1668 """Test BTHome parser for binary sensor opening without encryption."""
1669 data_string = b"\x40\x11\x00"
1670 advertisement = bytes_to_service_info(
1671 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
1672 )
1673
1674 device = BTHomeBluetoothDeviceData()
1675
1676 assert device.update(advertisement) == SensorUpdate(
1677 title="TEST DEVICE 18B2",
1678 devices={
1679 None: SensorDeviceInfo(
1680 name="TEST DEVICE 18B2",
1681 manufacturer=None,
1682 model="BTHome sensor",
1683 sw_version="BTHome BLE v2",
1684 hw_version=None,
1685 )
1686 },
1687 entity_descriptions={
1688 KEY_SIGNAL_STRENGTH: SensorDescription(
1689 device_key=KEY_SIGNAL_STRENGTH,
1690 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
1691 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
1692 ),
1693 },
1694 entity_values={
1695 KEY_SIGNAL_STRENGTH: SensorValue(
1696 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
1697 ),
1698 },
1699 binary_entity_descriptions={
1700 KEY_BINARY_OPENING: BinarySensorDescription(
1701 device_key=KEY_BINARY_OPENING,
1702 device_class=BinarySensorDeviceClass.OPENING,
1703 ),
1704 },
1705 binary_entity_values={
1706 KEY_BINARY_OPENING: BinarySensorValue(
1707 device_key=KEY_BINARY_OPENING, name="Opening", native_value=False
1708 ),
1709 },
1710 )
1711
1712
1713 def test_bthome_binary_sensor_window(caplog):
1714 """Test BTHome parser for binary sensor window without encryption."""
1715 data_string = b"\x40\x2d\x01"
1716 advertisement = bytes_to_service_info(
1717 data_string, local_name="SBDW-002C", address="A4:C1:38:8D:18:B2"
1718 )
1719
1720 device = BTHomeBluetoothDeviceData()
1721
1722 assert device.update(advertisement) == SensorUpdate(
1723 title="Shelly BLU Door/Window 18B2",
1724 devices={
1725 None: SensorDeviceInfo(
1726 name="Shelly BLU Door/Window 18B2",
1727 manufacturer="Shelly",
1728 model="BLU Door/Window",
1729 sw_version="BTHome BLE v2",
1730 hw_version=None,
1731 )
1732 },
1733 entity_descriptions={
1734 KEY_SIGNAL_STRENGTH: SensorDescription(
1735 device_key=KEY_SIGNAL_STRENGTH,
1736 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
1737 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
1738 ),
1739 },
1740 entity_values={
1741 KEY_SIGNAL_STRENGTH: SensorValue(
1742 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
1743 ),
1744 },
1745 binary_entity_descriptions={
1746 KEY_BINARY_WINDOW: BinarySensorDescription(
1747 device_key=KEY_BINARY_WINDOW,
1748 device_class=BinarySensorDeviceClass.WINDOW,
1749 ),
1750 },
1751 binary_entity_values={
1752 KEY_BINARY_WINDOW: BinarySensorValue(
1753 device_key=KEY_BINARY_WINDOW, name="Window", native_value=True
1754 ),
1755 },
1756 )
1757
1758
1759 def test_bthome_pm(caplog):
1760 """Test BTHome parser for PM2.5 and PM10 reading without encryption."""
1761 data_string = b"\x40\x0d\x12\x0c\x0e\x02\x1c"
1762 advertisement = bytes_to_service_info(
1763 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
1764 )
1765
1766 device = BTHomeBluetoothDeviceData()
1767 assert device.update(advertisement) == SensorUpdate(
1768 title="TEST DEVICE 18B2",
1769 devices={
1770 None: SensorDeviceInfo(
1771 name="TEST DEVICE 18B2",
1772 manufacturer=None,
1773 model="BTHome sensor",
1774 sw_version="BTHome BLE v2",
1775 hw_version=None,
1776 )
1777 },
1778 entity_descriptions={
1779 KEY_PM25: SensorDescription(
1780 device_key=KEY_PM25,
1781 device_class=SensorDeviceClass.PM25,
1782 native_unit_of_measurement=(
1783 Units.CONCENTRATION_MICROGRAMS_PER_CUBIC_METER
1784 ),
1785 ),
1786 KEY_PM10: SensorDescription(
1787 device_key=KEY_PM10,
1788 device_class=SensorDeviceClass.PM10,
1789 native_unit_of_measurement=(
1790 Units.CONCENTRATION_MICROGRAMS_PER_CUBIC_METER
1791 ),
1792 ),
1793 KEY_SIGNAL_STRENGTH: SensorDescription(
1794 device_key=KEY_SIGNAL_STRENGTH,
1795 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
1796 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
1797 ),
1798 },
1799 entity_values={
1800 KEY_PM25: SensorValue(device_key=KEY_PM25, name="Pm25", native_value=3090),
1801 KEY_PM10: SensorValue(device_key=KEY_PM10, name="Pm10", native_value=7170),
1802 KEY_SIGNAL_STRENGTH: SensorValue(
1803 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
1804 ),
1805 },
1806 )
1807
1808
1809 def test_bthome_co2(caplog):
1810 """Test BTHome parser for CO2 reading without encryption."""
1811 data_string = b"\x40\x12\xe2\x04"
1812 advertisement = bytes_to_service_info(
1813 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
1814 )
1815
1816 device = BTHomeBluetoothDeviceData()
1817 assert device.update(advertisement) == SensorUpdate(
1818 title="TEST DEVICE 18B2",
1819 devices={
1820 None: SensorDeviceInfo(
1821 name="TEST DEVICE 18B2",
1822 manufacturer=None,
1823 model="BTHome sensor",
1824 sw_version="BTHome BLE v2",
1825 hw_version=None,
1826 )
1827 },
1828 entity_descriptions={
1829 KEY_CO2: SensorDescription(
1830 device_key=KEY_CO2,
1831 device_class=SensorDeviceClass.CO2,
1832 native_unit_of_measurement=Units.CONCENTRATION_PARTS_PER_MILLION,
1833 ),
1834 KEY_SIGNAL_STRENGTH: SensorDescription(
1835 device_key=KEY_SIGNAL_STRENGTH,
1836 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
1837 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
1838 ),
1839 },
1840 entity_values={
1841 KEY_CO2: SensorValue(
1842 device_key=KEY_CO2, name="Carbon Dioxide", native_value=1250
1843 ),
1844 KEY_SIGNAL_STRENGTH: SensorValue(
1845 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
1846 ),
1847 },
1848 )
1849
1850
1851 def test_bthome_voc(caplog):
1852 """Test BTHome parser for VOC reading without encryption."""
1853 data_string = b"\x40\x133\x01"
1854 advertisement = bytes_to_service_info(
1855 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
1856 )
1857
1858 device = BTHomeBluetoothDeviceData()
1859 assert device.update(advertisement) == SensorUpdate(
1860 title="TEST DEVICE 18B2",
1861 devices={
1862 None: SensorDeviceInfo(
1863 name="TEST DEVICE 18B2",
1864 manufacturer=None,
1865 model="BTHome sensor",
1866 sw_version="BTHome BLE v2",
1867 hw_version=None,
1868 )
1869 },
1870 entity_descriptions={
1871 KEY_VOC: SensorDescription(
1872 device_key=KEY_VOC,
1873 device_class=SensorDeviceClass.VOLATILE_ORGANIC_COMPOUNDS,
1874 native_unit_of_measurement=Units.CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
1875 ),
1876 KEY_SIGNAL_STRENGTH: SensorDescription(
1877 device_key=KEY_SIGNAL_STRENGTH,
1878 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
1879 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
1880 ),
1881 },
1882 entity_values={
1883 KEY_VOC: SensorValue(
1884 device_key=KEY_VOC, name="Volatile Organic Compounds", native_value=307
1885 ),
1886 KEY_SIGNAL_STRENGTH: SensorValue(
1887 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
1888 ),
1889 },
1890 )
1891
1892
1893 def test_bthome_moisture(caplog):
1894 """Test BTHome parser for moisture reading from b-parasite sensor."""
1895 data_string = b"\x40\x14\x02\x0c"
1896 advertisement = bytes_to_service_info(
1897 data_string, local_name="prst", address="A4:C1:38:8D:18:B2"
1898 )
1899
1900 device = BTHomeBluetoothDeviceData()
1901 assert device.update(advertisement) == SensorUpdate(
1902 title="b-parasite 18B2",
1903 devices={
1904 None: SensorDeviceInfo(
1905 name="b-parasite 18B2",
1906 manufacturer="b-parasite",
1907 model="Plant sensor",
1908 sw_version="BTHome BLE v2",
1909 hw_version=None,
1910 )
1911 },
1912 entity_descriptions={
1913 KEY_MOISTURE: SensorDescription(
1914 device_key=KEY_MOISTURE,
1915 device_class=SensorDeviceClass.MOISTURE,
1916 native_unit_of_measurement=Units.PERCENTAGE,
1917 ),
1918 KEY_SIGNAL_STRENGTH: SensorDescription(
1919 device_key=KEY_SIGNAL_STRENGTH,
1920 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
1921 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
1922 ),
1923 },
1924 entity_values={
1925 KEY_MOISTURE: SensorValue(
1926 device_key=KEY_MOISTURE, name="Moisture", native_value=30.74
1927 ),
1928 KEY_SIGNAL_STRENGTH: SensorValue(
1929 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
1930 ),
1931 },
1932 )
1933
1934
1935 def test_bthome_motion(caplog):
1936 """Test BTHome parser for motion reading from sensor."""
1937 bindkey = "231d39c1d7cc1ab1aee224cd096db932"
1938 data_string = b"\x41\x87\xb9\x00\x11\x22\x33\x72\xb0\x23\x1f"
1939 advertisement = bytes_to_service_info(
1940 data_string, local_name="SBMO-003Z", address="54:48:E6:8F:80:A5"
1941 )
1942
1943 device = BTHomeBluetoothDeviceData(bindkey=bytes.fromhex(bindkey))
1944
1945 assert device.update(advertisement) == SensorUpdate(
1946 title="Shelly BLU Motion 80A5",
1947 devices={
1948 None: SensorDeviceInfo(
1949 name="Shelly BLU Motion 80A5",
1950 manufacturer="Shelly",
1951 model="BLU Motion",
1952 sw_version="BTHome BLE v2 (encrypted)",
1953 hw_version=None,
1954 )
1955 },
1956 entity_descriptions={
1957 KEY_SIGNAL_STRENGTH: SensorDescription(
1958 device_key=KEY_SIGNAL_STRENGTH,
1959 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
1960 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
1961 ),
1962 },
1963 entity_values={
1964 KEY_SIGNAL_STRENGTH: SensorValue(
1965 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
1966 ),
1967 },
1968 binary_entity_descriptions={
1969 KEY_BINARY_MOTION: BinarySensorDescription(
1970 device_key=KEY_BINARY_MOTION,
1971 device_class=BinarySensorDeviceClass.MOTION,
1972 ),
1973 },
1974 binary_entity_values={
1975 KEY_BINARY_MOTION: BinarySensorValue(
1976 device_key=KEY_BINARY_MOTION, name="Motion", native_value=True
1977 ),
1978 },
1979 )
1980
1981
1982 def test_bthome_event_button_long_press(caplog):
1983 """Test BTHome parser for an event of a long press on a button without encryption."""
1984 data_string = b"\x40\x3a\x04"
1985 advertisement = bytes_to_service_info(
1986 data_string, local_name="SBBT-002C", address="A4:C1:38:8D:18:B2"
1987 )
1988
1989 device = BTHomeBluetoothDeviceData()
1990
1991 assert device.update(advertisement) == SensorUpdate(
1992 title="Shelly BLU Button1 18B2",
1993 devices={
1994 None: SensorDeviceInfo(
1995 name="Shelly BLU Button1 18B2",
1996 manufacturer="Shelly",
1997 model="BLU Button1",
1998 sw_version="BTHome BLE v2",
1999 hw_version=None,
2000 )
2001 },
2002 entity_descriptions={
2003 KEY_SIGNAL_STRENGTH: SensorDescription(
2004 device_key=KEY_SIGNAL_STRENGTH,
2005 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
2006 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
2007 ),
2008 },
2009 entity_values={
2010 KEY_SIGNAL_STRENGTH: SensorValue(
2011 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
2012 ),
2013 },
2014 events={
2015 KEY_BUTTON: Event(
2016 device_key=KEY_BUTTON,
2017 name="Button",
2018 event_type="long_press",
2019 event_properties=None,
2020 ),
2021 },
2022 )
2023
2024
2025 def test_shelly_blu_button_zb(caplog):
2026 """Test BTHome parser for an event of a long press on a button without encryption."""
2027 data_string = b"\x40\x3a\x04"
2028 advertisement = bytes_to_service_info(
2029 data_string, local_name="SBBT-102C", address="A4:C1:38:8D:18:B2"
2030 )
2031
2032 device = BTHomeBluetoothDeviceData()
2033
2034 assert device.update(advertisement) == SensorUpdate(
2035 title="Shelly BLU Button1 ZB 18B2",
2036 devices={
2037 None: SensorDeviceInfo(
2038 name="Shelly BLU Button1 ZB 18B2",
2039 manufacturer="Shelly",
2040 model="BLU Button1 ZB",
2041 sw_version="BTHome BLE v2",
2042 hw_version=None,
2043 )
2044 },
2045 entity_descriptions={
2046 KEY_SIGNAL_STRENGTH: SensorDescription(
2047 device_key=KEY_SIGNAL_STRENGTH,
2048 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
2049 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
2050 ),
2051 },
2052 entity_values={
2053 KEY_SIGNAL_STRENGTH: SensorValue(
2054 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
2055 ),
2056 },
2057 events={
2058 KEY_BUTTON: Event(
2059 device_key=KEY_BUTTON,
2060 name="Button",
2061 event_type="long_press",
2062 event_properties=None,
2063 ),
2064 },
2065 )
2066
2067
2068 def test_bthome_event_triple_button_device(caplog):
2069 """
2070 Test BTHome parser for an event of a triple button device where
2071 the 2nd button is pressed and the 3rd button is triple pressed.
2072 """
2073 data_string = b"\x40\x3a\x00\x3a\x01\x3a\x03"
2074 advertisement = bytes_to_service_info(
2075 data_string, local_name="SBBT-004CEU", address="A4:C1:38:8D:18:B2"
2076 )
2077
2078 device = BTHomeBluetoothDeviceData()
2079
2080 assert device.update(advertisement) == SensorUpdate(
2081 title="Shelly BLU Wall Switch 4 18B2",
2082 devices={
2083 None: SensorDeviceInfo(
2084 name="Shelly BLU Wall Switch 4 18B2",
2085 manufacturer="Shelly",
2086 model="BLU Wall Switch 4",
2087 sw_version="BTHome BLE v2",
2088 hw_version=None,
2089 )
2090 },
2091 entity_descriptions={
2092 KEY_SIGNAL_STRENGTH: SensorDescription(
2093 device_key=KEY_SIGNAL_STRENGTH,
2094 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
2095 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
2096 ),
2097 },
2098 entity_values={
2099 KEY_SIGNAL_STRENGTH: SensorValue(
2100 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
2101 ),
2102 },
2103 events={
2104 DeviceKey(key="button_2", device_id=None): Event(
2105 device_key=DeviceKey(key="button_2", device_id=None),
2106 name="Button 2",
2107 event_type="press",
2108 event_properties=None,
2109 ),
2110 DeviceKey(key="button_3", device_id=None): Event(
2111 device_key=DeviceKey(key="button_3", device_id=None),
2112 name="Button 3",
2113 event_type="triple_press",
2114 event_properties=None,
2115 ),
2116 },
2117 )
2118
2119
2120 def test_bthome_event_button_hold_press(caplog):
2121 """Test BTHome parser for an event of holding press on a button without encryption."""
2122 data_string = b"\x40\x3a\x80"
2123 advertisement = bytes_to_service_info(
2124 data_string, local_name="SBBT-004CUS", address="A4:C1:38:8D:18:B2"
2125 )
2126
2127 device = BTHomeBluetoothDeviceData()
2128
2129 assert device.update(advertisement) == SensorUpdate(
2130 title="Shelly BLU RC Button 4 18B2",
2131 devices={
2132 None: SensorDeviceInfo(
2133 name="Shelly BLU RC Button 4 18B2",
2134 manufacturer="Shelly",
2135 model="BLU RC Button 4",
2136 sw_version="BTHome BLE v2",
2137 hw_version=None,
2138 )
2139 },
2140 entity_descriptions={
2141 KEY_SIGNAL_STRENGTH: SensorDescription(
2142 device_key=KEY_SIGNAL_STRENGTH,
2143 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
2144 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
2145 ),
2146 },
2147 entity_values={
2148 KEY_SIGNAL_STRENGTH: SensorValue(
2149 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
2150 ),
2151 },
2152 events={
2153 KEY_BUTTON: Event(
2154 device_key=KEY_BUTTON,
2155 name="Button",
2156 event_type="hold_press",
2157 event_properties=None,
2158 ),
2159 },
2160 )
2161
2162
2163 def test_bthome_event_dimmer_rotate_left_3_steps(caplog):
2164 """Test BTHome parser for an event rotating a dimmer 3 steps left."""
2165 data_string = b"\x40\x3c\x01\x03"
2166 advertisement = bytes_to_service_info(
2167 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
2168 )
2169
2170 device = BTHomeBluetoothDeviceData()
2171
2172 assert device.update(advertisement) == SensorUpdate(
2173 title="TEST DEVICE 18B2",
2174 devices={
2175 None: SensorDeviceInfo(
2176 name="TEST DEVICE 18B2",
2177 manufacturer=None,
2178 model="BTHome sensor",
2179 sw_version="BTHome BLE v2",
2180 hw_version=None,
2181 )
2182 },
2183 entity_descriptions={
2184 KEY_SIGNAL_STRENGTH: SensorDescription(
2185 device_key=KEY_SIGNAL_STRENGTH,
2186 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
2187 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
2188 ),
2189 },
2190 entity_values={
2191 KEY_SIGNAL_STRENGTH: SensorValue(
2192 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
2193 ),
2194 },
2195 events={
2196 KEY_DIMMER: Event(
2197 device_key=KEY_DIMMER,
2198 name="Dimmer",
2199 event_type="rotate_left",
2200 event_properties={"steps": 3},
2201 ),
2202 },
2203 )
2204
2205
2206 def test_bthome_event_dimmer_rotate_right_high_steps(caplog):
2207 """Steps is an unsigned byte; values >127 must not wrap to negative."""
2208 # 0x3C dimmer object: event_type=0x02 (rotate_right), steps=0xC8 (=200)
2209 data_string = b"\x40\x3c\x02\xc8"
2210 advertisement = bytes_to_service_info(
2211 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
2212 )
2213
2214 device = BTHomeBluetoothDeviceData()
2215
2216 assert device.update(advertisement) == SensorUpdate(
2217 title="TEST DEVICE 18B2",
2218 devices={
2219 None: SensorDeviceInfo(
2220 name="TEST DEVICE 18B2",
2221 manufacturer=None,
2222 model="BTHome sensor",
2223 sw_version="BTHome BLE v2",
2224 hw_version=None,
2225 )
2226 },
2227 entity_descriptions={
2228 KEY_SIGNAL_STRENGTH: SensorDescription(
2229 device_key=KEY_SIGNAL_STRENGTH,
2230 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
2231 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
2232 ),
2233 },
2234 entity_values={
2235 KEY_SIGNAL_STRENGTH: SensorValue(
2236 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
2237 ),
2238 },
2239 events={
2240 KEY_DIMMER: Event(
2241 device_key=KEY_DIMMER,
2242 name="Dimmer",
2243 event_type="rotate_right",
2244 event_properties={"steps": 200},
2245 ),
2246 },
2247 )
2248
2249
2250 def test_bthome_rotation(caplog):
2251 """Test BTHome parser for rotation."""
2252 data_string = b"\x40\x3f\x02\x0c"
2253 advertisement = bytes_to_service_info(
2254 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
2255 )
2256
2257 device = BTHomeBluetoothDeviceData()
2258
2259 assert device.update(advertisement) == SensorUpdate(
2260 title="TEST DEVICE 18B2",
2261 devices={
2262 None: SensorDeviceInfo(
2263 name="TEST DEVICE 18B2",
2264 manufacturer=None,
2265 model="BTHome sensor",
2266 sw_version="BTHome BLE v2",
2267 hw_version=None,
2268 )
2269 },
2270 entity_descriptions={
2271 KEY_ROTATION: SensorDescription(
2272 device_key=KEY_ROTATION,
2273 device_class=SensorDeviceClass.ROTATION,
2274 native_unit_of_measurement=Units.DEGREE,
2275 ),
2276 KEY_SIGNAL_STRENGTH: SensorDescription(
2277 device_key=KEY_SIGNAL_STRENGTH,
2278 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
2279 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
2280 ),
2281 },
2282 entity_values={
2283 KEY_ROTATION: SensorValue(
2284 device_key=KEY_ROTATION, name="Rotation", native_value=307.4
2285 ),
2286 KEY_SIGNAL_STRENGTH: SensorValue(
2287 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
2288 ),
2289 },
2290 )
2291
2292
2293 def test_bthome_invalid_button_event(caplog):
2294 """Test BTHome parser for an invalid button event."""
2295 data_string = b"\x40\x3a\xfe"
2296 advertisement = bytes_to_service_info(
2297 data_string, local_name="SBBT-002C", address="A4:C1:38:8D:18:B2"
2298 )
2299
2300 device = BTHomeBluetoothDeviceData()
2301
2302 assert device.update(advertisement) == SensorUpdate(
2303 title="Shelly BLU Button1 18B2",
2304 devices={
2305 None: SensorDeviceInfo(
2306 name="Shelly BLU Button1 18B2",
2307 manufacturer="Shelly",
2308 model="BLU Button1",
2309 sw_version="BTHome BLE v2",
2310 hw_version=None,
2311 )
2312 },
2313 entity_descriptions={
2314 KEY_SIGNAL_STRENGTH: SensorDescription(
2315 device_key=KEY_SIGNAL_STRENGTH,
2316 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
2317 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
2318 ),
2319 },
2320 entity_values={
2321 KEY_SIGNAL_STRENGTH: SensorValue(
2322 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
2323 ),
2324 },
2325 events={},
2326 )
2327
2328
2329 def test_encrypted_shelly_blu_button_event(caplog):
2330 """Test BTHome parser for an encrypted shelly blu button event."""
2331 bindkey = "90bffd73cb6b26ef58a7a8eba9232036"
2332 data_string = b"\x45\x0a\xeb\x84\x46\x7f\x85\xba\x01\x00\x00\x31\x88\x78\x8c"
2333
2334 advertisement = bytes_to_service_info(
2335 data_string,
2336 local_name="SBBT-002C",
2337 address="B4:35:22:F5:3D:A4",
2338 )
2339
2340 device = BTHomeBluetoothDeviceData(bindkey=bytes.fromhex(bindkey))
2341 assert device.supported(advertisement)
2342 assert device.bindkey_verified
2343
2344
2345 def test_bthome_distance_millimeters(caplog):
2346 """Test BTHome parser for distance in millimeters."""
2347 data_string = b"\x40\x40\x0c\x00"
2348 advertisement = bytes_to_service_info(
2349 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
2350 )
2351
2352 device = BTHomeBluetoothDeviceData()
2353
2354 assert device.update(advertisement) == SensorUpdate(
2355 title="TEST DEVICE 18B2",
2356 devices={
2357 None: SensorDeviceInfo(
2358 name="TEST DEVICE 18B2",
2359 manufacturer=None,
2360 model="BTHome sensor",
2361 sw_version="BTHome BLE v2",
2362 hw_version=None,
2363 )
2364 },
2365 entity_descriptions={
2366 KEY_DISTANCE: SensorDescription(
2367 device_key=KEY_DISTANCE,
2368 device_class=SensorDeviceClass.DISTANCE,
2369 native_unit_of_measurement=Units.LENGTH_MILLIMETERS,
2370 ),
2371 KEY_SIGNAL_STRENGTH: SensorDescription(
2372 device_key=KEY_SIGNAL_STRENGTH,
2373 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
2374 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
2375 ),
2376 },
2377 entity_values={
2378 KEY_DISTANCE: SensorValue(
2379 device_key=KEY_DISTANCE, name="Distance", native_value=12
2380 ),
2381 KEY_SIGNAL_STRENGTH: SensorValue(
2382 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
2383 ),
2384 },
2385 )
2386
2387
2388 def test_bthome_distance_meters(caplog):
2389 """Test BTHome parser for distance in meters."""
2390 data_string = b"\x40\x41\x4e\x00"
2391 advertisement = bytes_to_service_info(
2392 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
2393 )
2394
2395 device = BTHomeBluetoothDeviceData()
2396
2397 assert device.update(advertisement) == SensorUpdate(
2398 title="TEST DEVICE 18B2",
2399 devices={
2400 None: SensorDeviceInfo(
2401 name="TEST DEVICE 18B2",
2402 manufacturer=None,
2403 model="BTHome sensor",
2404 sw_version="BTHome BLE v2",
2405 hw_version=None,
2406 )
2407 },
2408 entity_descriptions={
2409 KEY_DISTANCE: SensorDescription(
2410 device_key=KEY_DISTANCE,
2411 device_class=SensorDeviceClass.DISTANCE,
2412 native_unit_of_measurement=Units.LENGTH_METERS,
2413 ),
2414 KEY_SIGNAL_STRENGTH: SensorDescription(
2415 device_key=KEY_SIGNAL_STRENGTH,
2416 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
2417 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
2418 ),
2419 },
2420 entity_values={
2421 KEY_DISTANCE: SensorValue(
2422 device_key=KEY_DISTANCE, name="Distance", native_value=7.8
2423 ),
2424 KEY_SIGNAL_STRENGTH: SensorValue(
2425 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
2426 ),
2427 },
2428 )
2429
2430
2431 def test_bthome_duration(caplog):
2432 """Test BTHome parser for duration in seconds."""
2433 data_string = b"\x40\x42\x4e\x34\x00"
2434 advertisement = bytes_to_service_info(
2435 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
2436 )
2437
2438 device = BTHomeBluetoothDeviceData()
2439
2440 assert device.update(advertisement) == SensorUpdate(
2441 title="TEST DEVICE 18B2",
2442 devices={
2443 None: SensorDeviceInfo(
2444 name="TEST DEVICE 18B2",
2445 manufacturer=None,
2446 model="BTHome sensor",
2447 sw_version="BTHome BLE v2",
2448 hw_version=None,
2449 )
2450 },
2451 entity_descriptions={
2452 KEY_DURATION: SensorDescription(
2453 device_key=KEY_DURATION,
2454 device_class=SensorDeviceClass.DURATION,
2455 native_unit_of_measurement=Units.TIME_SECONDS,
2456 ),
2457 KEY_SIGNAL_STRENGTH: SensorDescription(
2458 device_key=KEY_SIGNAL_STRENGTH,
2459 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
2460 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
2461 ),
2462 },
2463 entity_values={
2464 KEY_DURATION: SensorValue(
2465 device_key=KEY_DURATION, name="Duration", native_value=13.39
2466 ),
2467 KEY_SIGNAL_STRENGTH: SensorValue(
2468 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
2469 ),
2470 },
2471 )
2472
2473
2474 def test_bthome_current(caplog):
2475 """Test BTHome parser for current in VA."""
2476 data_string = b"\x40\x43\x4e\x34"
2477 advertisement = bytes_to_service_info(
2478 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
2479 )
2480
2481 device = BTHomeBluetoothDeviceData()
2482
2483 assert device.update(advertisement) == SensorUpdate(
2484 title="TEST DEVICE 18B2",
2485 devices={
2486 None: SensorDeviceInfo(
2487 name="TEST DEVICE 18B2",
2488 manufacturer=None,
2489 model="BTHome sensor",
2490 sw_version="BTHome BLE v2",
2491 hw_version=None,
2492 )
2493 },
2494 entity_descriptions={
2495 KEY_CURRENT: SensorDescription(
2496 device_key=KEY_CURRENT,
2497 device_class=SensorDeviceClass.CURRENT,
2498 native_unit_of_measurement=Units.ELECTRIC_CURRENT_AMPERE,
2499 ),
2500 KEY_SIGNAL_STRENGTH: SensorDescription(
2501 device_key=KEY_SIGNAL_STRENGTH,
2502 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
2503 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
2504 ),
2505 },
2506 entity_values={
2507 KEY_CURRENT: SensorValue(
2508 device_key=KEY_CURRENT, name="Current", native_value=13.39
2509 ),
2510 KEY_SIGNAL_STRENGTH: SensorValue(
2511 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
2512 ),
2513 },
2514 )
2515
2516
2517 def test_bthome_speed(caplog):
2518 """Test BTHome parser for speed in m/s."""
2519 data_string = b"\x40\x44\x4e\x34"
2520 advertisement = bytes_to_service_info(
2521 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
2522 )
2523
2524 device = BTHomeBluetoothDeviceData()
2525
2526 assert device.update(advertisement) == SensorUpdate(
2527 title="TEST DEVICE 18B2",
2528 devices={
2529 None: SensorDeviceInfo(
2530 name="TEST DEVICE 18B2",
2531 manufacturer=None,
2532 model="BTHome sensor",
2533 sw_version="BTHome BLE v2",
2534 hw_version=None,
2535 )
2536 },
2537 entity_descriptions={
2538 KEY_SPEED: SensorDescription(
2539 device_key=KEY_SPEED,
2540 device_class=SensorDeviceClass.SPEED,
2541 native_unit_of_measurement=Units.SPEED_METERS_PER_SECOND,
2542 ),
2543 KEY_SIGNAL_STRENGTH: SensorDescription(
2544 device_key=KEY_SIGNAL_STRENGTH,
2545 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
2546 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
2547 ),
2548 },
2549 entity_values={
2550 KEY_SPEED: SensorValue(
2551 device_key=KEY_SPEED, name="Speed", native_value=133.9
2552 ),
2553 KEY_SIGNAL_STRENGTH: SensorValue(
2554 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
2555 ),
2556 },
2557 )
2558
2559
2560 def test_bthome_speed_signed(caplog):
2561 """Test BTHome parser for signed, hi-res speed in m/s."""
2562 data_string = b"\x40\x62\x40\x99\xdf\xff"
2563 advertisement = bytes_to_service_info(
2564 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
2565 )
2566
2567 device = BTHomeBluetoothDeviceData()
2568
2569 assert device.update(advertisement) == SensorUpdate(
2570 title="TEST DEVICE 18B2",
2571 devices={
2572 None: SensorDeviceInfo(
2573 name="TEST DEVICE 18B2",
2574 manufacturer=None,
2575 model="BTHome sensor",
2576 sw_version="BTHome BLE v2",
2577 hw_version=None,
2578 )
2579 },
2580 entity_descriptions={
2581 KEY_SPEED: SensorDescription(
2582 device_key=KEY_SPEED,
2583 device_class=SensorDeviceClass.SPEED,
2584 native_unit_of_measurement=Units.SPEED_METERS_PER_SECOND,
2585 ),
2586 KEY_SIGNAL_STRENGTH: SensorDescription(
2587 device_key=KEY_SIGNAL_STRENGTH,
2588 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
2589 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
2590 ),
2591 },
2592 entity_values={
2593 KEY_SPEED: SensorValue(
2594 device_key=KEY_SPEED, name="Speed", native_value=-2.123456
2595 ),
2596 KEY_SIGNAL_STRENGTH: SensorValue(
2597 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
2598 ),
2599 },
2600 )
2601
2602
2603 def test_bthome_temperature_2(caplog):
2604 """Test BTHome parser for temperature with one digit."""
2605 data_string = b"\x40\x45\x11\x01"
2606 advertisement = bytes_to_service_info(
2607 data_string, local_name="SBTR-001AEU", address="A4:C1:38:8D:18:B2"
2608 )
2609
2610 device = BTHomeBluetoothDeviceData()
2611
2612 assert device.update(advertisement) == SensorUpdate(
2613 title="Shelly BLU TRV 18B2",
2614 devices={
2615 None: SensorDeviceInfo(
2616 name="Shelly BLU TRV 18B2",
2617 manufacturer="Shelly",
2618 model="BLU TRV",
2619 sw_version="BTHome BLE v2",
2620 hw_version=None,
2621 )
2622 },
2623 entity_descriptions={
2624 KEY_TEMPERATURE: SensorDescription(
2625 device_key=KEY_TEMPERATURE,
2626 device_class=SensorDeviceClass.TEMPERATURE,
2627 native_unit_of_measurement=Units.TEMP_CELSIUS,
2628 ),
2629 KEY_SIGNAL_STRENGTH: SensorDescription(
2630 device_key=KEY_SIGNAL_STRENGTH,
2631 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
2632 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
2633 ),
2634 },
2635 entity_values={
2636 KEY_TEMPERATURE: SensorValue(
2637 device_key=KEY_TEMPERATURE, name="Temperature", native_value=27.3
2638 ),
2639 KEY_SIGNAL_STRENGTH: SensorValue(
2640 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
2641 ),
2642 },
2643 )
2644
2645
2646 def test_bthome_uv_index(caplog):
2647 """Test BTHome parser for UV index."""
2648 data_string = b"\x40\x46\x32"
2649 advertisement = bytes_to_service_info(
2650 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
2651 )
2652
2653 device = BTHomeBluetoothDeviceData()
2654
2655 assert device.update(advertisement) == SensorUpdate(
2656 title="TEST DEVICE 18B2",
2657 devices={
2658 None: SensorDeviceInfo(
2659 name="TEST DEVICE 18B2",
2660 manufacturer=None,
2661 model="BTHome sensor",
2662 sw_version="BTHome BLE v2",
2663 hw_version=None,
2664 )
2665 },
2666 entity_descriptions={
2667 KEY_UV_INDEX: SensorDescription(
2668 device_key=KEY_UV_INDEX,
2669 device_class=SensorDeviceClass.UV_INDEX,
2670 native_unit_of_measurement=None,
2671 ),
2672 KEY_SIGNAL_STRENGTH: SensorDescription(
2673 device_key=KEY_SIGNAL_STRENGTH,
2674 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
2675 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
2676 ),
2677 },
2678 entity_values={
2679 KEY_UV_INDEX: SensorValue(
2680 device_key=KEY_UV_INDEX, name="Uv Index", native_value=5.0
2681 ),
2682 KEY_SIGNAL_STRENGTH: SensorValue(
2683 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
2684 ),
2685 },
2686 )
2687
2688
2689 def test_bthome_volume_liters(caplog):
2690 """Test BTHome parser for Volume in Liters."""
2691 data_string = b"\x40\x47\x87\x56"
2692 advertisement = bytes_to_service_info(
2693 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
2694 )
2695
2696 device = BTHomeBluetoothDeviceData()
2697
2698 assert device.update(advertisement) == SensorUpdate(
2699 title="TEST DEVICE 18B2",
2700 devices={
2701 None: SensorDeviceInfo(
2702 name="TEST DEVICE 18B2",
2703 manufacturer=None,
2704 model="BTHome sensor",
2705 sw_version="BTHome BLE v2",
2706 hw_version=None,
2707 )
2708 },
2709 entity_descriptions={
2710 KEY_VOLUME: SensorDescription(
2711 device_key=KEY_VOLUME,
2712 device_class=SensorDeviceClass.VOLUME,
2713 native_unit_of_measurement=Units.VOLUME_LITERS,
2714 ),
2715 KEY_SIGNAL_STRENGTH: SensorDescription(
2716 device_key=KEY_SIGNAL_STRENGTH,
2717 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
2718 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
2719 ),
2720 },
2721 entity_values={
2722 KEY_VOLUME: SensorValue(
2723 device_key=KEY_VOLUME, name="Volume", native_value=2215.1
2724 ),
2725 KEY_SIGNAL_STRENGTH: SensorValue(
2726 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
2727 ),
2728 },
2729 )
2730
2731
2732 def test_bthome_volume_milliliters(caplog):
2733 """Test BTHome parser for Volume in milliliters."""
2734 data_string = b"\x40\x48\xdc\x87"
2735 advertisement = bytes_to_service_info(
2736 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
2737 )
2738
2739 device = BTHomeBluetoothDeviceData()
2740
2741 assert device.update(advertisement) == SensorUpdate(
2742 title="TEST DEVICE 18B2",
2743 devices={
2744 None: SensorDeviceInfo(
2745 name="TEST DEVICE 18B2",
2746 manufacturer=None,
2747 model="BTHome sensor",
2748 sw_version="BTHome BLE v2",
2749 hw_version=None,
2750 )
2751 },
2752 entity_descriptions={
2753 KEY_VOLUME: SensorDescription(
2754 device_key=KEY_VOLUME,
2755 device_class=SensorDeviceClass.VOLUME,
2756 native_unit_of_measurement=Units.VOLUME_MILLILITERS,
2757 ),
2758 KEY_SIGNAL_STRENGTH: SensorDescription(
2759 device_key=KEY_SIGNAL_STRENGTH,
2760 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
2761 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
2762 ),
2763 },
2764 entity_values={
2765 KEY_VOLUME: SensorValue(
2766 device_key=KEY_VOLUME, name="Volume", native_value=34780
2767 ),
2768 KEY_SIGNAL_STRENGTH: SensorValue(
2769 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
2770 ),
2771 },
2772 )
2773
2774
2775 def test_bthome_volume_flow_rate(caplog):
2776 """Test BTHome parser for Volume flow rate in m3 per hour."""
2777 data_string = b"\x40\x49\xdc\x87"
2778 advertisement = bytes_to_service_info(
2779 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
2780 )
2781
2782 device = BTHomeBluetoothDeviceData()
2783
2784 assert device.update(advertisement) == SensorUpdate(
2785 title="TEST DEVICE 18B2",
2786 devices={
2787 None: SensorDeviceInfo(
2788 name="TEST DEVICE 18B2",
2789 manufacturer=None,
2790 model="BTHome sensor",
2791 sw_version="BTHome BLE v2",
2792 hw_version=None,
2793 )
2794 },
2795 entity_descriptions={
2796 KEY_VOLUME_FLOW_RATE: SensorDescription(
2797 device_key=KEY_VOLUME_FLOW_RATE,
2798 device_class=SensorDeviceClass.VOLUME_FLOW_RATE,
2799 native_unit_of_measurement=Units.VOLUME_FLOW_RATE_CUBIC_METERS_PER_HOUR,
2800 ),
2801 KEY_SIGNAL_STRENGTH: SensorDescription(
2802 device_key=KEY_SIGNAL_STRENGTH,
2803 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
2804 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
2805 ),
2806 },
2807 entity_values={
2808 KEY_VOLUME_FLOW_RATE: SensorValue(
2809 device_key=KEY_VOLUME_FLOW_RATE,
2810 name="Volume Flow Rate",
2811 native_value=34.780,
2812 ),
2813 KEY_SIGNAL_STRENGTH: SensorValue(
2814 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
2815 ),
2816 },
2817 )
2818
2819
2820 def test_bthome_voltage_2(caplog):
2821 """Test BThome parser for voltage reading without encryption."""
2822 data_string = b"\x40\x4a\x02\x0c"
2823 advertisement = bytes_to_service_info(
2824 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
2825 )
2826
2827 device = BTHomeBluetoothDeviceData()
2828 assert device.update(advertisement) == SensorUpdate(
2829 title="TEST DEVICE 18B2",
2830 devices={
2831 None: SensorDeviceInfo(
2832 name="TEST DEVICE 18B2",
2833 manufacturer=None,
2834 model="BTHome sensor",
2835 sw_version="BTHome BLE v2",
2836 hw_version=None,
2837 )
2838 },
2839 entity_descriptions={
2840 KEY_VOLTAGE: SensorDescription(
2841 device_key=KEY_VOLTAGE,
2842 device_class=SensorDeviceClass.VOLTAGE,
2843 native_unit_of_measurement=Units.ELECTRIC_POTENTIAL_VOLT,
2844 ),
2845 KEY_SIGNAL_STRENGTH: SensorDescription(
2846 device_key=KEY_SIGNAL_STRENGTH,
2847 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
2848 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
2849 ),
2850 },
2851 entity_values={
2852 KEY_VOLTAGE: SensorValue(
2853 device_key=KEY_VOLTAGE, name="Voltage", native_value=307.4
2854 ),
2855 KEY_SIGNAL_STRENGTH: SensorValue(
2856 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
2857 ),
2858 },
2859 )
2860
2861
2862 def test_bthome_gas(caplog):
2863 """Test BTHome parser for gas reading without encryption."""
2864 data_string = b"\x40\x4b\x13\x8a\x14"
2865 advertisement = bytes_to_service_info(
2866 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
2867 )
2868
2869 device = BTHomeBluetoothDeviceData()
2870 assert device.update(advertisement) == SensorUpdate(
2871 title="TEST DEVICE 18B2",
2872 devices={
2873 None: SensorDeviceInfo(
2874 name="TEST DEVICE 18B2",
2875 manufacturer=None,
2876 model="BTHome sensor",
2877 sw_version="BTHome BLE v2",
2878 hw_version=None,
2879 )
2880 },
2881 entity_descriptions={
2882 KEY_GAS: SensorDescription(
2883 device_key=KEY_GAS,
2884 device_class=SensorDeviceClass.GAS,
2885 native_unit_of_measurement=Units.VOLUME_CUBIC_METERS,
2886 ),
2887 KEY_SIGNAL_STRENGTH: SensorDescription(
2888 device_key=KEY_SIGNAL_STRENGTH,
2889 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
2890 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
2891 ),
2892 },
2893 entity_values={
2894 KEY_GAS: SensorValue(device_key=KEY_GAS, name="Gas", native_value=1346.067),
2895 KEY_SIGNAL_STRENGTH: SensorValue(
2896 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
2897 ),
2898 },
2899 )
2900
2901
2902 def test_bthome_gas_2(caplog):
2903 """Test BTHome parser for gas reading without encryption."""
2904 data_string = b"\x40\x4c\x41\x01\x8a\x01"
2905 advertisement = bytes_to_service_info(
2906 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
2907 )
2908
2909 device = BTHomeBluetoothDeviceData()
2910 assert device.update(advertisement) == SensorUpdate(
2911 title="TEST DEVICE 18B2",
2912 devices={
2913 None: SensorDeviceInfo(
2914 name="TEST DEVICE 18B2",
2915 manufacturer=None,
2916 model="BTHome sensor",
2917 sw_version="BTHome BLE v2",
2918 hw_version=None,
2919 )
2920 },
2921 entity_descriptions={
2922 KEY_GAS: SensorDescription(
2923 device_key=KEY_GAS,
2924 device_class=SensorDeviceClass.GAS,
2925 native_unit_of_measurement=Units.VOLUME_CUBIC_METERS,
2926 ),
2927 KEY_SIGNAL_STRENGTH: SensorDescription(
2928 device_key=KEY_SIGNAL_STRENGTH,
2929 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
2930 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
2931 ),
2932 },
2933 entity_values={
2934 KEY_GAS: SensorValue(
2935 device_key=KEY_GAS, name="Gas", native_value=25821.505
2936 ),
2937 KEY_SIGNAL_STRENGTH: SensorValue(
2938 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
2939 ),
2940 },
2941 )
2942
2943
2944 def test_bthome_energy_2(caplog):
2945 """Test BTHome parser for energy reading without encryption."""
2946 data_string = b"\x40\x4d\x12\x13\x8a\x14"
2947 advertisement = bytes_to_service_info(
2948 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
2949 )
2950
2951 device = BTHomeBluetoothDeviceData()
2952 assert device.update(advertisement) == SensorUpdate(
2953 title="TEST DEVICE 18B2",
2954 devices={
2955 None: SensorDeviceInfo(
2956 name="TEST DEVICE 18B2",
2957 manufacturer=None,
2958 model="BTHome sensor",
2959 sw_version="BTHome BLE v2",
2960 hw_version=None,
2961 )
2962 },
2963 entity_descriptions={
2964 KEY_ENERGY: SensorDescription(
2965 device_key=KEY_ENERGY,
2966 device_class=SensorDeviceClass.ENERGY,
2967 native_unit_of_measurement=Units.ENERGY_KILO_WATT_HOUR,
2968 ),
2969 KEY_SIGNAL_STRENGTH: SensorDescription(
2970 device_key=KEY_SIGNAL_STRENGTH,
2971 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
2972 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
2973 ),
2974 },
2975 entity_values={
2976 KEY_ENERGY: SensorValue(
2977 device_key=KEY_ENERGY, name="Energy", native_value=344593.17
2978 ),
2979 KEY_SIGNAL_STRENGTH: SensorValue(
2980 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
2981 ),
2982 },
2983 )
2984
2985
2986 def test_bthome_volume_liters_2(caplog):
2987 """Test BTHome parser for Volume in Liters."""
2988 data_string = b"\x40\x4e\x87\x56\x2a\x01"
2989 advertisement = bytes_to_service_info(
2990 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
2991 )
2992
2993 device = BTHomeBluetoothDeviceData()
2994
2995 assert device.update(advertisement) == SensorUpdate(
2996 title="TEST DEVICE 18B2",
2997 devices={
2998 None: SensorDeviceInfo(
2999 name="TEST DEVICE 18B2",
3000 manufacturer=None,
3001 model="BTHome sensor",
3002 sw_version="BTHome BLE v2",
3003 hw_version=None,
3004 )
3005 },
3006 entity_descriptions={
3007 KEY_VOLUME: SensorDescription(
3008 device_key=KEY_VOLUME,
3009 device_class=SensorDeviceClass.VOLUME,
3010 native_unit_of_measurement=Units.VOLUME_LITERS,
3011 ),
3012 KEY_SIGNAL_STRENGTH: SensorDescription(
3013 device_key=KEY_SIGNAL_STRENGTH,
3014 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
3015 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
3016 ),
3017 },
3018 entity_values={
3019 KEY_VOLUME: SensorValue(
3020 device_key=KEY_VOLUME, name="Volume", native_value=19551.879
3021 ),
3022 KEY_SIGNAL_STRENGTH: SensorValue(
3023 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
3024 ),
3025 },
3026 )
3027
3028
3029 def test_bthome_volume_water(caplog):
3030 """Test BTHome parser for water in Liters."""
3031 data_string = b"\x40\x4f\x87\x56\x2a\x01"
3032 advertisement = bytes_to_service_info(
3033 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
3034 )
3035
3036 device = BTHomeBluetoothDeviceData()
3037
3038 assert device.update(advertisement) == SensorUpdate(
3039 title="TEST DEVICE 18B2",
3040 devices={
3041 None: SensorDeviceInfo(
3042 name="TEST DEVICE 18B2",
3043 manufacturer=None,
3044 model="BTHome sensor",
3045 sw_version="BTHome BLE v2",
3046 hw_version=None,
3047 )
3048 },
3049 entity_descriptions={
3050 KEY_WATER: SensorDescription(
3051 device_key=KEY_WATER,
3052 device_class=SensorDeviceClass.WATER,
3053 native_unit_of_measurement=Units.VOLUME_LITERS,
3054 ),
3055 KEY_SIGNAL_STRENGTH: SensorDescription(
3056 device_key=KEY_SIGNAL_STRENGTH,
3057 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
3058 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
3059 ),
3060 },
3061 entity_values={
3062 KEY_WATER: SensorValue(
3063 device_key=KEY_WATER, name="Water", native_value=19551.879
3064 ),
3065 KEY_SIGNAL_STRENGTH: SensorValue(
3066 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
3067 ),
3068 },
3069 )
3070
3071
3072 def test_bthome_timestamp(caplog):
3073 """Test BTHome parser for Unix timestamp (seconds from 1-1-1970)."""
3074 data_string = b"\x44\x50\x5d\x39\x61\x64"
3075 advertisement = bytes_to_service_info(
3076 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
3077 )
3078
3079 device = BTHomeBluetoothDeviceData()
3080
3081 assert device.update(advertisement) == SensorUpdate(
3082 title="TEST DEVICE 18B2",
3083 devices={
3084 None: SensorDeviceInfo(
3085 name="TEST DEVICE 18B2",
3086 manufacturer=None,
3087 model="BTHome sensor",
3088 sw_version="BTHome BLE v2",
3089 hw_version=None,
3090 )
3091 },
3092 entity_descriptions={
3093 KEY_TIMESTAMP: SensorDescription(
3094 device_key=KEY_TIMESTAMP,
3095 device_class=SensorDeviceClass.TIMESTAMP,
3096 native_unit_of_measurement=None,
3097 ),
3098 KEY_SIGNAL_STRENGTH: SensorDescription(
3099 device_key=KEY_SIGNAL_STRENGTH,
3100 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
3101 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
3102 ),
3103 },
3104 entity_values={
3105 KEY_TIMESTAMP: SensorValue(
3106 device_key=KEY_TIMESTAMP,
3107 name="Timestamp",
3108 native_value=datetime(2023, 5, 14, 19, 41, 17, tzinfo=UTC),
3109 ),
3110 KEY_SIGNAL_STRENGTH: SensorValue(
3111 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
3112 ),
3113 },
3114 )
3115
3116
3117 def test_bthome_acceleration(caplog):
3118 """Test BTHome parser for acceleration in m/s°."""
3119 data_string = b"\x44\x51\x87\x56"
3120 advertisement = bytes_to_service_info(
3121 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
3122 )
3123
3124 device = BTHomeBluetoothDeviceData()
3125
3126 assert device.update(advertisement) == SensorUpdate(
3127 title="TEST DEVICE 18B2",
3128 devices={
3129 None: SensorDeviceInfo(
3130 name="TEST DEVICE 18B2",
3131 manufacturer=None,
3132 model="BTHome sensor",
3133 sw_version="BTHome BLE v2",
3134 hw_version=None,
3135 )
3136 },
3137 entity_descriptions={
3138 KEY_ACCELERATION: SensorDescription(
3139 device_key=KEY_ACCELERATION,
3140 device_class=SensorDeviceClass.ACCELERATION,
3141 native_unit_of_measurement=Units.ACCELERATION_METERS_PER_SQUARE_SECOND,
3142 ),
3143 KEY_SIGNAL_STRENGTH: SensorDescription(
3144 device_key=KEY_SIGNAL_STRENGTH,
3145 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
3146 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
3147 ),
3148 },
3149 entity_values={
3150 KEY_ACCELERATION: SensorValue(
3151 device_key=KEY_ACCELERATION, name="Acceleration", native_value=22.151
3152 ),
3153 KEY_SIGNAL_STRENGTH: SensorValue(
3154 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
3155 ),
3156 },
3157 )
3158
3159
3160 def test_bthome_acceleration_signed(caplog):
3161 """Test BTHome parser for signed, hi-res acceleration in m/s°."""
3162 data_string = b"\x40\x63\x00\x57\xd0\xff"
3163 advertisement = bytes_to_service_info(
3164 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
3165 )
3166
3167 device = BTHomeBluetoothDeviceData()
3168
3169 assert device.update(advertisement) == SensorUpdate(
3170 title="TEST DEVICE 18B2",
3171 devices={
3172 None: SensorDeviceInfo(
3173 name="TEST DEVICE 18B2",
3174 manufacturer=None,
3175 model="BTHome sensor",
3176 sw_version="BTHome BLE v2",
3177 hw_version=None,
3178 )
3179 },
3180 entity_descriptions={
3181 KEY_ACCELERATION: SensorDescription(
3182 device_key=KEY_ACCELERATION,
3183 device_class=SensorDeviceClass.ACCELERATION,
3184 native_unit_of_measurement=Units.ACCELERATION_METERS_PER_SQUARE_SECOND,
3185 ),
3186 KEY_SIGNAL_STRENGTH: SensorDescription(
3187 device_key=KEY_SIGNAL_STRENGTH,
3188 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
3189 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
3190 ),
3191 },
3192 entity_values={
3193 KEY_ACCELERATION: SensorValue(
3194 device_key=KEY_ACCELERATION, name="Acceleration", native_value=-3.123456
3195 ),
3196 KEY_SIGNAL_STRENGTH: SensorValue(
3197 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
3198 ),
3199 },
3200 )
3201
3202
3203 def test_bthome_gyroscope(caplog):
3204 """Test BTHome parser for gyroscope in °/s."""
3205 data_string = b"\x44\x52\x87\x56"
3206 advertisement = bytes_to_service_info(
3207 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
3208 )
3209
3210 device = BTHomeBluetoothDeviceData()
3211
3212 assert device.update(advertisement) == SensorUpdate(
3213 title="TEST DEVICE 18B2",
3214 devices={
3215 None: SensorDeviceInfo(
3216 name="TEST DEVICE 18B2",
3217 manufacturer=None,
3218 model="BTHome sensor",
3219 sw_version="BTHome BLE v2",
3220 hw_version=None,
3221 )
3222 },
3223 entity_descriptions={
3224 KEY_GYROSCOPE: SensorDescription(
3225 device_key=KEY_GYROSCOPE,
3226 device_class=SensorDeviceClass.GYROSCOPE,
3227 native_unit_of_measurement=Units.GYROSCOPE_DEGREES_PER_SECOND,
3228 ),
3229 KEY_SIGNAL_STRENGTH: SensorDescription(
3230 device_key=KEY_SIGNAL_STRENGTH,
3231 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
3232 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
3233 ),
3234 },
3235 entity_values={
3236 KEY_GYROSCOPE: SensorValue(
3237 device_key=KEY_GYROSCOPE, name="Gyroscope", native_value=22.151
3238 ),
3239 KEY_SIGNAL_STRENGTH: SensorValue(
3240 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
3241 ),
3242 },
3243 )
3244
3245
3246 def test_bthome_text(caplog):
3247 """Test BTHome parser for text."""
3248 data_string = b"\x44\x53\x0c\x48\x65\x6c\x6c\x6f\x20\x57\x6f\x72\x6c\x64\x21"
3249 advertisement = bytes_to_service_info(
3250 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
3251 )
3252
3253 device = BTHomeBluetoothDeviceData()
3254
3255 assert device.update(advertisement) == SensorUpdate(
3256 title="TEST DEVICE 18B2",
3257 devices={
3258 None: SensorDeviceInfo(
3259 name="TEST DEVICE 18B2",
3260 manufacturer=None,
3261 model="BTHome sensor",
3262 sw_version="BTHome BLE v2",
3263 hw_version=None,
3264 )
3265 },
3266 entity_descriptions={
3267 KEY_TEXT: SensorDescription(
3268 device_key=KEY_TEXT,
3269 device_class=ExtendedSensorDeviceClass.TEXT,
3270 native_unit_of_measurement=None,
3271 ),
3272 KEY_SIGNAL_STRENGTH: SensorDescription(
3273 device_key=KEY_SIGNAL_STRENGTH,
3274 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
3275 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
3276 ),
3277 },
3278 entity_values={
3279 KEY_TEXT: SensorValue(
3280 device_key=KEY_TEXT, name="Text", native_value="Hello World!"
3281 ),
3282 KEY_SIGNAL_STRENGTH: SensorValue(
3283 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
3284 ),
3285 },
3286 )
3287
3288
3289 def test_bthome_raw(caplog):
3290 """Test BTHome parser for raw hex data."""
3291 data_string = b"\x44\x54\x0c\x48\x65\x6c\x6c\x6f\x20\x57\x6f\x72\x6c\x64\x21"
3292 advertisement = bytes_to_service_info(
3293 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
3294 )
3295
3296 device = BTHomeBluetoothDeviceData()
3297
3298 assert device.update(advertisement) == SensorUpdate(
3299 title="TEST DEVICE 18B2",
3300 devices={
3301 None: SensorDeviceInfo(
3302 name="TEST DEVICE 18B2",
3303 manufacturer=None,
3304 model="BTHome sensor",
3305 sw_version="BTHome BLE v2",
3306 hw_version=None,
3307 )
3308 },
3309 entity_descriptions={
3310 KEY_RAW: SensorDescription(
3311 device_key=KEY_RAW,
3312 device_class=ExtendedSensorDeviceClass.RAW,
3313 native_unit_of_measurement=None,
3314 ),
3315 KEY_SIGNAL_STRENGTH: SensorDescription(
3316 device_key=KEY_SIGNAL_STRENGTH,
3317 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
3318 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
3319 ),
3320 },
3321 entity_values={
3322 KEY_RAW: SensorValue(
3323 device_key=KEY_RAW, name="Raw", native_value="48656c6c6f20576f726c6421"
3324 ),
3325 KEY_SIGNAL_STRENGTH: SensorValue(
3326 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
3327 ),
3328 },
3329 )
3330
3331
3332 def test_bthome_text_invalid(caplog):
3333 """Test BTHome parser for text sensor with invalid format."""
3334 data_string = b"\x44\x53\x01\x87"
3335 advertisement = bytes_to_service_info(
3336 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
3337 )
3338
3339 device = BTHomeBluetoothDeviceData()
3340
3341 assert device.update(advertisement) == SensorUpdate(
3342 title="TEST DEVICE 18B2",
3343 devices={
3344 None: SensorDeviceInfo(
3345 name="TEST DEVICE 18B2",
3346 manufacturer=None,
3347 model="BTHome sensor",
3348 sw_version="BTHome BLE v2",
3349 hw_version=None,
3350 )
3351 },
3352 entity_descriptions={
3353 KEY_SIGNAL_STRENGTH: SensorDescription(
3354 device_key=KEY_SIGNAL_STRENGTH,
3355 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
3356 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
3357 ),
3358 },
3359 entity_values={
3360 KEY_SIGNAL_STRENGTH: SensorValue(
3361 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
3362 ),
3363 },
3364 )
3365
3366
3367 def test_bthome_volume_storage(caplog):
3368 """Test BTHome parser for volume storage in Liters."""
3369 data_string = b"\x40\x55\x87\x56\x2a\x01"
3370 advertisement = bytes_to_service_info(
3371 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
3372 )
3373
3374 device = BTHomeBluetoothDeviceData()
3375
3376 assert device.update(advertisement) == SensorUpdate(
3377 title="TEST DEVICE 18B2",
3378 devices={
3379 None: SensorDeviceInfo(
3380 name="TEST DEVICE 18B2",
3381 manufacturer=None,
3382 model="BTHome sensor",
3383 sw_version="BTHome BLE v2",
3384 hw_version=None,
3385 )
3386 },
3387 entity_descriptions={
3388 KEY_VOLUME_STORAGE: SensorDescription(
3389 device_key=KEY_VOLUME_STORAGE,
3390 device_class=ExtendedSensorDeviceClass.VOLUME_STORAGE,
3391 native_unit_of_measurement=Units.VOLUME_LITERS,
3392 ),
3393 KEY_SIGNAL_STRENGTH: SensorDescription(
3394 device_key=KEY_SIGNAL_STRENGTH,
3395 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
3396 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
3397 ),
3398 },
3399 entity_values={
3400 KEY_VOLUME_STORAGE: SensorValue(
3401 device_key=KEY_VOLUME_STORAGE,
3402 name="Volume Storage",
3403 native_value=19551.879,
3404 ),
3405 KEY_SIGNAL_STRENGTH: SensorValue(
3406 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
3407 ),
3408 },
3409 )
3410
3411
3412 def test_bthome_conductivity(caplog):
3413 """Test BTHome parser for conductivity reading from SenseNL sensor."""
3414 data_string = b"\x40\x56\xe8\x03"
3415 advertisement = bytes_to_service_info(
3416 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
3417 )
3418
3419 device = BTHomeBluetoothDeviceData()
3420 assert device.update(advertisement) == SensorUpdate(
3421 title="TEST DEVICE 18B2",
3422 devices={
3423 None: SensorDeviceInfo(
3424 name="TEST DEVICE 18B2",
3425 manufacturer=None,
3426 model="BTHome sensor",
3427 sw_version="BTHome BLE v2",
3428 hw_version=None,
3429 )
3430 },
3431 entity_descriptions={
3432 KEY_CONDUCTIVITY: SensorDescription(
3433 device_key=KEY_CONDUCTIVITY,
3434 device_class=SensorDeviceClass.CONDUCTIVITY,
3435 native_unit_of_measurement=Units.CONDUCTIVITY,
3436 ),
3437 KEY_SIGNAL_STRENGTH: SensorDescription(
3438 device_key=KEY_SIGNAL_STRENGTH,
3439 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
3440 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
3441 ),
3442 },
3443 entity_values={
3444 KEY_CONDUCTIVITY: SensorValue(
3445 device_key=KEY_CONDUCTIVITY, name="Conductivity", native_value=1000
3446 ),
3447 KEY_SIGNAL_STRENGTH: SensorValue(
3448 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
3449 ),
3450 },
3451 )
3452
3453
3454 def test_shelly_blu_ht(caplog):
3455 """Test BTHome parser shelly blu h&t."""
3456 data_string = b"\x40\x57\xea"
3457 advertisement = bytes_to_service_info(
3458 data_string, local_name="SBHT-003C", address="A4:C1:38:8D:18:B2"
3459 )
3460
3461 device = BTHomeBluetoothDeviceData()
3462 assert device.update(advertisement) == SensorUpdate(
3463 title="Shelly BLU H&T 18B2",
3464 devices={
3465 None: SensorDeviceInfo(
3466 name="Shelly BLU H&T 18B2",
3467 manufacturer="Shelly",
3468 model="BLU H&T",
3469 sw_version="BTHome BLE v2",
3470 hw_version=None,
3471 )
3472 },
3473 entity_descriptions={
3474 KEY_TEMPERATURE: SensorDescription(
3475 device_key=KEY_TEMPERATURE,
3476 device_class=SensorDeviceClass.TEMPERATURE,
3477 native_unit_of_measurement=Units.TEMP_CELSIUS,
3478 ),
3479 KEY_SIGNAL_STRENGTH: SensorDescription(
3480 device_key=KEY_SIGNAL_STRENGTH,
3481 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
3482 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
3483 ),
3484 },
3485 entity_values={
3486 KEY_TEMPERATURE: SensorValue(
3487 device_key=KEY_TEMPERATURE, name="Temperature", native_value=-22
3488 ),
3489 KEY_SIGNAL_STRENGTH: SensorValue(
3490 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
3491 ),
3492 },
3493 )
3494
3495
3496 def test_shelly_blu_ht_zb(caplog):
3497 """Test BTHome parser shelly blu h&t."""
3498 data_string = b"\x40\x57\xea"
3499 advertisement = bytes_to_service_info(
3500 data_string, local_name="SBHT-203C", address="A4:C1:38:8D:18:B2"
3501 )
3502
3503 device = BTHomeBluetoothDeviceData()
3504 assert device.update(advertisement) == SensorUpdate(
3505 title="Shelly BLU H&T ZB 18B2",
3506 devices={
3507 None: SensorDeviceInfo(
3508 name="Shelly BLU H&T ZB 18B2",
3509 manufacturer="Shelly",
3510 model="BLU H&T ZB",
3511 sw_version="BTHome BLE v2",
3512 hw_version=None,
3513 )
3514 },
3515 entity_descriptions={
3516 KEY_TEMPERATURE: SensorDescription(
3517 device_key=KEY_TEMPERATURE,
3518 device_class=SensorDeviceClass.TEMPERATURE,
3519 native_unit_of_measurement=Units.TEMP_CELSIUS,
3520 ),
3521 KEY_SIGNAL_STRENGTH: SensorDescription(
3522 device_key=KEY_SIGNAL_STRENGTH,
3523 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
3524 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
3525 ),
3526 },
3527 entity_values={
3528 KEY_TEMPERATURE: SensorValue(
3529 device_key=KEY_TEMPERATURE, name="Temperature", native_value=-22
3530 ),
3531 KEY_SIGNAL_STRENGTH: SensorValue(
3532 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
3533 ),
3534 },
3535 )
3536
3537
3538 def test_bthome_temperature_1_byte(caplog):
3539 """Test BTHome parser for 1 byte temperature reading without encryption."""
3540 data_string = b"\x40\x57\xea"
3541 advertisement = bytes_to_service_info(
3542 data_string, local_name="ATC_8D18B2", address="A4:C1:38:8D:18:B2"
3543 )
3544
3545 device = BTHomeBluetoothDeviceData()
3546 assert device.update(advertisement) == SensorUpdate(
3547 title="ATC 18B2",
3548 devices={
3549 None: SensorDeviceInfo(
3550 name="ATC 18B2",
3551 manufacturer="Xiaomi",
3552 model="Temperature/Humidity sensor",
3553 sw_version="BTHome BLE v2",
3554 hw_version=None,
3555 )
3556 },
3557 entity_descriptions={
3558 KEY_TEMPERATURE: SensorDescription(
3559 device_key=KEY_TEMPERATURE,
3560 device_class=SensorDeviceClass.TEMPERATURE,
3561 native_unit_of_measurement=Units.TEMP_CELSIUS,
3562 ),
3563 KEY_SIGNAL_STRENGTH: SensorDescription(
3564 device_key=KEY_SIGNAL_STRENGTH,
3565 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
3566 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
3567 ),
3568 },
3569 entity_values={
3570 KEY_TEMPERATURE: SensorValue(
3571 device_key=KEY_TEMPERATURE, name="Temperature", native_value=-22
3572 ),
3573 KEY_SIGNAL_STRENGTH: SensorValue(
3574 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
3575 ),
3576 },
3577 )
3578
3579
3580 def test_bthome_temperature_1_byte_2(caplog):
3581 """Test BTHome parser for 1 byte temperature reading without encryption."""
3582 data_string = b"\x40\x58\xea"
3583 advertisement = bytes_to_service_info(
3584 data_string, local_name="ATC_8D18B2", address="A4:C1:38:8D:18:B2"
3585 )
3586
3587 device = BTHomeBluetoothDeviceData()
3588 assert device.update(advertisement) == SensorUpdate(
3589 title="ATC 18B2",
3590 devices={
3591 None: SensorDeviceInfo(
3592 name="ATC 18B2",
3593 manufacturer="Xiaomi",
3594 model="Temperature/Humidity sensor",
3595 sw_version="BTHome BLE v2",
3596 hw_version=None,
3597 )
3598 },
3599 entity_descriptions={
3600 KEY_TEMPERATURE: SensorDescription(
3601 device_key=KEY_TEMPERATURE,
3602 device_class=SensorDeviceClass.TEMPERATURE,
3603 native_unit_of_measurement=Units.TEMP_CELSIUS,
3604 ),
3605 KEY_SIGNAL_STRENGTH: SensorDescription(
3606 device_key=KEY_SIGNAL_STRENGTH,
3607 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
3608 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
3609 ),
3610 },
3611 entity_values={
3612 KEY_TEMPERATURE: SensorValue(
3613 device_key=KEY_TEMPERATURE, name="Temperature", native_value=-7.7
3614 ),
3615 KEY_SIGNAL_STRENGTH: SensorValue(
3616 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
3617 ),
3618 },
3619 )
3620
3621
3622 def test_bthome_count_signed(caplog):
3623 """Test BTHome parser for signed counter reading without encryption."""
3624 data_string = b"\x40\x59\xea"
3625 advertisement = bytes_to_service_info(
3626 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
3627 )
3628
3629 device = BTHomeBluetoothDeviceData()
3630 assert device.update(advertisement) == SensorUpdate(
3631 title="TEST DEVICE 18B2",
3632 devices={
3633 None: SensorDeviceInfo(
3634 name="TEST DEVICE 18B2",
3635 manufacturer=None,
3636 model="BTHome sensor",
3637 sw_version="BTHome BLE v2",
3638 hw_version=None,
3639 )
3640 },
3641 entity_descriptions={
3642 KEY_COUNT: SensorDescription(
3643 device_key=KEY_COUNT,
3644 device_class=SensorDeviceClass.COUNT,
3645 native_unit_of_measurement=None,
3646 ),
3647 KEY_SIGNAL_STRENGTH: SensorDescription(
3648 device_key=KEY_SIGNAL_STRENGTH,
3649 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
3650 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
3651 ),
3652 },
3653 entity_values={
3654 KEY_COUNT: SensorValue(
3655 device_key=KEY_COUNT, name="Count", native_value=-22
3656 ),
3657 KEY_SIGNAL_STRENGTH: SensorValue(
3658 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
3659 ),
3660 },
3661 )
3662
3663
3664 def test_bthome_count_signed_2_bytes(caplog):
3665 """Test BTHome parser for signed counter reading without encryption."""
3666 data_string = b"\x40\x5a\xea\xea"
3667 advertisement = bytes_to_service_info(
3668 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
3669 )
3670
3671 device = BTHomeBluetoothDeviceData()
3672 assert device.update(advertisement) == SensorUpdate(
3673 title="TEST DEVICE 18B2",
3674 devices={
3675 None: SensorDeviceInfo(
3676 name="TEST DEVICE 18B2",
3677 manufacturer=None,
3678 model="BTHome sensor",
3679 sw_version="BTHome BLE v2",
3680 hw_version=None,
3681 )
3682 },
3683 entity_descriptions={
3684 KEY_COUNT: SensorDescription(
3685 device_key=KEY_COUNT,
3686 device_class=SensorDeviceClass.COUNT,
3687 native_unit_of_measurement=None,
3688 ),
3689 KEY_SIGNAL_STRENGTH: SensorDescription(
3690 device_key=KEY_SIGNAL_STRENGTH,
3691 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
3692 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
3693 ),
3694 },
3695 entity_values={
3696 KEY_COUNT: SensorValue(
3697 device_key=KEY_COUNT, name="Count", native_value=-5398
3698 ),
3699 KEY_SIGNAL_STRENGTH: SensorValue(
3700 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
3701 ),
3702 },
3703 )
3704
3705
3706 def test_bthome_count_signed_4_bytes(caplog):
3707 """Test BTHome parser for signed counter reading without encryption."""
3708 data_string = b"\x40\x5b\xea\x02\x34\xea"
3709 advertisement = bytes_to_service_info(
3710 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
3711 )
3712
3713 device = BTHomeBluetoothDeviceData()
3714 assert device.update(advertisement) == SensorUpdate(
3715 title="TEST DEVICE 18B2",
3716 devices={
3717 None: SensorDeviceInfo(
3718 name="TEST DEVICE 18B2",
3719 manufacturer=None,
3720 model="BTHome sensor",
3721 sw_version="BTHome BLE v2",
3722 hw_version=None,
3723 )
3724 },
3725 entity_descriptions={
3726 KEY_COUNT: SensorDescription(
3727 device_key=KEY_COUNT,
3728 device_class=SensorDeviceClass.COUNT,
3729 native_unit_of_measurement=None,
3730 ),
3731 KEY_SIGNAL_STRENGTH: SensorDescription(
3732 device_key=KEY_SIGNAL_STRENGTH,
3733 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
3734 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
3735 ),
3736 },
3737 entity_values={
3738 KEY_COUNT: SensorValue(
3739 device_key=KEY_COUNT, name="Count", native_value=-365690134
3740 ),
3741 KEY_SIGNAL_STRENGTH: SensorValue(
3742 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
3743 ),
3744 },
3745 )
3746
3747
3748 def test_bthome_power_signed(caplog):
3749 """Test BTHome parser for signed power reading without encryption."""
3750 data_string = b"\x40\x5c\x02\xfb\xff\xff"
3751 advertisement = bytes_to_service_info(
3752 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
3753 )
3754
3755 device = BTHomeBluetoothDeviceData()
3756 assert device.update(advertisement) == SensorUpdate(
3757 title="TEST DEVICE 18B2",
3758 devices={
3759 None: SensorDeviceInfo(
3760 name="TEST DEVICE 18B2",
3761 manufacturer=None,
3762 model="BTHome sensor",
3763 sw_version="BTHome BLE v2",
3764 hw_version=None,
3765 )
3766 },
3767 entity_descriptions={
3768 KEY_POWER: SensorDescription(
3769 device_key=KEY_POWER,
3770 device_class=SensorDeviceClass.POWER,
3771 native_unit_of_measurement=Units.POWER_WATT,
3772 ),
3773 KEY_SIGNAL_STRENGTH: SensorDescription(
3774 device_key=KEY_SIGNAL_STRENGTH,
3775 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
3776 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
3777 ),
3778 },
3779 entity_values={
3780 KEY_POWER: SensorValue(
3781 device_key=KEY_POWER, name="Power", native_value=-12.78
3782 ),
3783 KEY_SIGNAL_STRENGTH: SensorValue(
3784 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
3785 ),
3786 },
3787 )
3788
3789
3790 def test_bthome_current_signed(caplog):
3791 """Test BTHome parser for signed current in VA."""
3792 data_string = b"\x40\x5d\x02\xea"
3793 advertisement = bytes_to_service_info(
3794 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
3795 )
3796
3797 device = BTHomeBluetoothDeviceData()
3798
3799 assert device.update(advertisement) == SensorUpdate(
3800 title="TEST DEVICE 18B2",
3801 devices={
3802 None: SensorDeviceInfo(
3803 name="TEST DEVICE 18B2",
3804 manufacturer=None,
3805 model="BTHome sensor",
3806 sw_version="BTHome BLE v2",
3807 hw_version=None,
3808 )
3809 },
3810 entity_descriptions={
3811 KEY_CURRENT: SensorDescription(
3812 device_key=KEY_CURRENT,
3813 device_class=SensorDeviceClass.CURRENT,
3814 native_unit_of_measurement=Units.ELECTRIC_CURRENT_AMPERE,
3815 ),
3816 KEY_SIGNAL_STRENGTH: SensorDescription(
3817 device_key=KEY_SIGNAL_STRENGTH,
3818 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
3819 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
3820 ),
3821 },
3822 entity_values={
3823 KEY_CURRENT: SensorValue(
3824 device_key=KEY_CURRENT, name="Current", native_value=-5.63
3825 ),
3826 KEY_SIGNAL_STRENGTH: SensorValue(
3827 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
3828 ),
3829 },
3830 )
3831
3832
3833 def test_bthome_double_temperature(caplog):
3834 """Test BTHome parser for double temperature reading without encryption."""
3835 data_string = b"\x40\x02\xca\x09\x02\xcf\x09"
3836 advertisement = bytes_to_service_info(
3837 data_string, local_name="A4:C1:38:8D:18:B2", address="A4:C1:38:8D:18:B2"
3838 )
3839
3840 device = BTHomeBluetoothDeviceData()
3841 assert device.update(advertisement) == SensorUpdate(
3842 title="BTHome sensor 18B2",
3843 devices={
3844 None: SensorDeviceInfo(
3845 name="BTHome sensor 18B2",
3846 manufacturer=None,
3847 model="BTHome sensor",
3848 sw_version="BTHome BLE v2",
3849 hw_version=None,
3850 )
3851 },
3852 entity_descriptions={
3853 DeviceKey(key="temperature_1", device_id=None): SensorDescription(
3854 device_key=DeviceKey(key="temperature_1", device_id=None),
3855 device_class=SensorDeviceClass.TEMPERATURE,
3856 native_unit_of_measurement=Units.TEMP_CELSIUS,
3857 ),
3858 DeviceKey(key="temperature_2", device_id=None): SensorDescription(
3859 device_key=DeviceKey(key="temperature_2", device_id=None),
3860 device_class=SensorDeviceClass.TEMPERATURE,
3861 native_unit_of_measurement=Units.TEMP_CELSIUS,
3862 ),
3863 KEY_SIGNAL_STRENGTH: SensorDescription(
3864 device_key=KEY_SIGNAL_STRENGTH,
3865 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
3866 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
3867 ),
3868 },
3869 entity_values={
3870 DeviceKey(key="temperature_1", device_id=None): SensorValue(
3871 device_key=DeviceKey(key="temperature_1", device_id=None),
3872 name="Temperature 1",
3873 native_value=25.06,
3874 ),
3875 DeviceKey(key="temperature_2", device_id=None): SensorValue(
3876 device_key=DeviceKey(key="temperature_2", device_id=None),
3877 name="Temperature 2",
3878 native_value=25.11,
3879 ),
3880 KEY_SIGNAL_STRENGTH: SensorValue(
3881 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
3882 ),
3883 },
3884 )
3885
3886
3887 def test_bthome_triple_temperature_double_humidity_battery(caplog):
3888 """
3889 Test BTHome parser for triple temperature, double humidity and
3890 single battery reading without encryption.
3891 """
3892 data_string = (
3893 b"\x40\x02\xca\x09\x02\xcf\x09\x02\xcf\x08\x03\xb7\x18\x03\xb7\x17\x01\x5d"
3894 )
3895 advertisement = bytes_to_service_info(
3896 data_string, local_name="A4:C1:38:8D:18:B2", address="A4:C1:38:8D:18:B2"
3897 )
3898
3899 device = BTHomeBluetoothDeviceData()
3900 assert device.update(advertisement) == SensorUpdate(
3901 title="BTHome sensor 18B2",
3902 devices={
3903 None: SensorDeviceInfo(
3904 name="BTHome sensor 18B2",
3905 manufacturer=None,
3906 model="BTHome sensor",
3907 sw_version="BTHome BLE v2",
3908 hw_version=None,
3909 )
3910 },
3911 entity_descriptions={
3912 DeviceKey(key="temperature_1", device_id=None): SensorDescription(
3913 device_key=DeviceKey(key="temperature_1", device_id=None),
3914 device_class=SensorDeviceClass.TEMPERATURE,
3915 native_unit_of_measurement=Units.TEMP_CELSIUS,
3916 ),
3917 DeviceKey(key="temperature_2", device_id=None): SensorDescription(
3918 device_key=DeviceKey(key="temperature_2", device_id=None),
3919 device_class=SensorDeviceClass.TEMPERATURE,
3920 native_unit_of_measurement=Units.TEMP_CELSIUS,
3921 ),
3922 DeviceKey(key="temperature_3", device_id=None): SensorDescription(
3923 device_key=DeviceKey(key="temperature_3", device_id=None),
3924 device_class=SensorDeviceClass.TEMPERATURE,
3925 native_unit_of_measurement=Units.TEMP_CELSIUS,
3926 ),
3927 DeviceKey(key="humidity_1", device_id=None): SensorDescription(
3928 device_key=DeviceKey(key="humidity_1", device_id=None),
3929 device_class=SensorDeviceClass.HUMIDITY,
3930 native_unit_of_measurement=Units.PERCENTAGE,
3931 ),
3932 DeviceKey(key="humidity_2", device_id=None): SensorDescription(
3933 device_key=DeviceKey(key="humidity_2", device_id=None),
3934 device_class=SensorDeviceClass.HUMIDITY,
3935 native_unit_of_measurement=Units.PERCENTAGE,
3936 ),
3937 KEY_BATTERY: SensorDescription(
3938 device_key=KEY_BATTERY,
3939 device_class=SensorDeviceClass.BATTERY,
3940 native_unit_of_measurement=Units.PERCENTAGE,
3941 ),
3942 KEY_SIGNAL_STRENGTH: SensorDescription(
3943 device_key=KEY_SIGNAL_STRENGTH,
3944 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
3945 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
3946 ),
3947 },
3948 entity_values={
3949 DeviceKey(key="temperature_1", device_id=None): SensorValue(
3950 device_key=DeviceKey(key="temperature_1", device_id=None),
3951 name="Temperature 1",
3952 native_value=25.06,
3953 ),
3954 DeviceKey(key="temperature_2", device_id=None): SensorValue(
3955 device_key=DeviceKey(key="temperature_2", device_id=None),
3956 name="Temperature 2",
3957 native_value=25.11,
3958 ),
3959 DeviceKey(key="temperature_3", device_id=None): SensorValue(
3960 device_key=DeviceKey(key="temperature_3", device_id=None),
3961 name="Temperature 3",
3962 native_value=22.55,
3963 ),
3964 DeviceKey(key="humidity_1", device_id=None): SensorValue(
3965 device_key=DeviceKey(key="humidity_1", device_id=None),
3966 name="Humidity 1",
3967 native_value=63.27,
3968 ),
3969 DeviceKey(key="humidity_2", device_id=None): SensorValue(
3970 device_key=DeviceKey(key="humidity_2", device_id=None),
3971 name="Humidity 2",
3972 native_value=60.71,
3973 ),
3974 KEY_BATTERY: SensorValue(KEY_BATTERY, name="Battery", native_value=93),
3975 KEY_SIGNAL_STRENGTH: SensorValue(
3976 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
3977 ),
3978 },
3979 )
3980
3981
3982 def test_bthome_multiple_uuids(caplog):
3983 """Test BTHome parser for a device that broadcasts multiple uuids."""
3984 advertisement = BluetoothServiceInfoBleak(
3985 name="ATC_8D18B2",
3986 address="A4:C1:38:8D:18:B2",
3987 rssi=-60,
3988 manufacturer_data={},
3989 service_data={
3990 "0000181a-0000-1000-8000-00805f9b34fb": b"\xc4$\x818\xc1\xa4V\x08\x83\x18\xbf",
3991 "0000fe95-0000-1000-8000-00805f9b34fb": b"0X[\x05\x01\xc4$\x818\xc1\xa4(\x01\x00",
3992 "0000fcd2-0000-1000-8000-00805f9b34fb": b"\x40\x01\x5d\x02\x5d\x09\x03\xb7\x18",
3993 },
3994 service_uuids=[
3995 "0000181a-0000-1000-8000-00805f9b34fb",
3996 "0000fe95-0000-1000-8000-00805f9b34fb",
3997 "0000fcd2-0000-1000-8000-00805f9b34fb",
3998 ],
3999 source="",
4000 device=None,
4001 advertisement=None,
4002 connectable=False,
4003 time=ADVERTISEMENT_TIME,
4004 tx_power=None,
4005 )
4006
4007 device = BTHomeBluetoothDeviceData()
4008
4009 assert device.update(advertisement) == SensorUpdate(
4010 title="ATC 18B2",
4011 devices={
4012 None: SensorDeviceInfo(
4013 name="ATC 18B2",
4014 manufacturer="Xiaomi",
4015 model="Temperature/Humidity sensor",
4016 sw_version="BTHome BLE v2",
4017 hw_version=None,
4018 )
4019 },
4020 entity_descriptions={
4021 KEY_TEMPERATURE: SensorDescription(
4022 device_key=KEY_TEMPERATURE,
4023 device_class=SensorDeviceClass.TEMPERATURE,
4024 native_unit_of_measurement=Units.TEMP_CELSIUS,
4025 ),
4026 KEY_HUMIDITY: SensorDescription(
4027 device_key=KEY_HUMIDITY,
4028 device_class=SensorDeviceClass.HUMIDITY,
4029 native_unit_of_measurement=Units.PERCENTAGE,
4030 ),
4031 KEY_BATTERY: SensorDescription(
4032 device_key=KEY_BATTERY,
4033 device_class=SensorDeviceClass.BATTERY,
4034 native_unit_of_measurement=Units.PERCENTAGE,
4035 ),
4036 KEY_SIGNAL_STRENGTH: SensorDescription(
4037 device_key=KEY_SIGNAL_STRENGTH,
4038 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
4039 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
4040 ),
4041 },
4042 entity_values={
4043 KEY_TEMPERATURE: SensorValue(
4044 device_key=KEY_TEMPERATURE, name="Temperature", native_value=23.97
4045 ),
4046 KEY_HUMIDITY: SensorValue(
4047 device_key=KEY_HUMIDITY, name="Humidity", native_value=63.27
4048 ),
4049 KEY_BATTERY: SensorValue(
4050 device_key=KEY_BATTERY, name="Battery", native_value=93
4051 ),
4052 KEY_SIGNAL_STRENGTH: SensorValue(
4053 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
4054 ),
4055 },
4056 )
4057
4058
4059 def test_bthome_double_voltage_different_object_id(caplog):
4060 """
4061 Test BTHome parser for double voltage with different object id.
4062 """
4063 data_string = b"@\x00\x01\x0b\x00\x00\x00J\r\t\x013\x0c\xe9\x0c"
4064 advertisement = bytes_to_service_info(
4065 data_string, local_name="A4:C1:38:8D:18:B2", address="A4:C1:38:8D:18:B2"
4066 )
4067
4068 device = BTHomeBluetoothDeviceData()
4069 assert device.update(advertisement) == SensorUpdate(
4070 title="BTHome sensor 18B2",
4071 devices={
4072 None: SensorDeviceInfo(
4073 name="BTHome sensor 18B2",
4074 manufacturer=None,
4075 model="BTHome sensor",
4076 sw_version="BTHome BLE v2",
4077 hw_version=None,
4078 )
4079 },
4080 entity_descriptions={
4081 KEY_PACKET_ID: SensorDescription(
4082 device_key=KEY_PACKET_ID,
4083 device_class=SensorDeviceClass.PACKET_ID,
4084 native_unit_of_measurement=None,
4085 ),
4086 DeviceKey(key="power", device_id=None): SensorDescription(
4087 device_key=DeviceKey(key="power", device_id=None),
4088 device_class=SensorDeviceClass.POWER,
4089 native_unit_of_measurement=Units.POWER_WATT,
4090 ),
4091 DeviceKey(key="voltage_1", device_id=None): SensorDescription(
4092 device_key=DeviceKey(key="voltage_1", device_id=None),
4093 device_class=SensorDeviceClass.VOLTAGE,
4094 native_unit_of_measurement=Units.ELECTRIC_POTENTIAL_VOLT,
4095 ),
4096 KEY_BATTERY: SensorDescription(
4097 device_key=KEY_BATTERY,
4098 device_class=SensorDeviceClass.BATTERY,
4099 native_unit_of_measurement=Units.PERCENTAGE,
4100 ),
4101 DeviceKey(key="voltage_2", device_id=None): SensorDescription(
4102 device_key=DeviceKey(key="voltage_2", device_id=None),
4103 device_class=SensorDeviceClass.VOLTAGE,
4104 native_unit_of_measurement=Units.ELECTRIC_POTENTIAL_VOLT,
4105 ),
4106 KEY_SIGNAL_STRENGTH: SensorDescription(
4107 device_key=KEY_SIGNAL_STRENGTH,
4108 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
4109 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
4110 ),
4111 },
4112 entity_values={
4113 KEY_PACKET_ID: SensorValue(
4114 device_key=KEY_PACKET_ID, name="Packet Id", native_value=1
4115 ),
4116 DeviceKey(key="power", device_id=None): SensorValue(
4117 device_key=DeviceKey(key="power", device_id=None),
4118 name="Power",
4119 native_value=0.0,
4120 ),
4121 DeviceKey(key="voltage_1", device_id=None): SensorValue(
4122 device_key=DeviceKey(key="voltage_1", device_id=None),
4123 name="Voltage 1",
4124 native_value=231.7,
4125 ),
4126 KEY_BATTERY: SensorValue(KEY_BATTERY, name="Battery", native_value=51),
4127 DeviceKey(key="voltage_2", device_id=None): SensorValue(
4128 device_key=DeviceKey(key="voltage_2", device_id=None),
4129 name="Voltage 2",
4130 native_value=3.305,
4131 ),
4132 KEY_SIGNAL_STRENGTH: SensorValue(
4133 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
4134 ),
4135 },
4136 )
4137
4138
4139 def test_bthome_shelly_button(caplog):
4140 """
4141 Test BTHome parser with a shelly button.
4142 """
4143 data_string = b"@\x00R\x01d:\x01"
4144 advertisement = bytes_to_service_info(
4145 data_string, local_name="A4:C1:38:8D:18:B2", address="A4:C1:38:8D:18:B2"
4146 )
4147
4148 device = BTHomeBluetoothDeviceData()
4149 update = device.update(advertisement)
4150 assert device.supported(advertisement) is True
4151 assert update == SensorUpdate(
4152 title="BTHome sensor 18B2",
4153 devices={
4154 None: SensorDeviceInfo(
4155 name="BTHome sensor 18B2",
4156 model="BTHome sensor",
4157 manufacturer=None,
4158 sw_version="BTHome BLE v2",
4159 hw_version=None,
4160 )
4161 },
4162 entity_descriptions={
4163 DeviceKey(key="packet_id", device_id=None): SensorDescription(
4164 device_key=DeviceKey(key="packet_id", device_id=None),
4165 device_class=SensorDeviceClass.PACKET_ID,
4166 native_unit_of_measurement=None,
4167 ),
4168 DeviceKey(key="battery", device_id=None): SensorDescription(
4169 device_key=DeviceKey(key="battery", device_id=None),
4170 device_class=SensorDeviceClass.BATTERY,
4171 native_unit_of_measurement=Units.PERCENTAGE,
4172 ),
4173 DeviceKey(key="signal_strength", device_id=None): SensorDescription(
4174 device_key=DeviceKey(key="signal_strength", device_id=None),
4175 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
4176 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
4177 ),
4178 },
4179 entity_values={
4180 DeviceKey(key="packet_id", device_id=None): SensorValue(
4181 device_key=DeviceKey(key="packet_id", device_id=None),
4182 name="Packet Id",
4183 native_value=82,
4184 ),
4185 DeviceKey(key="battery", device_id=None): SensorValue(
4186 device_key=DeviceKey(key="battery", device_id=None),
4187 name="Battery",
4188 native_value=100,
4189 ),
4190 DeviceKey(key="signal_strength", device_id=None): SensorValue(
4191 device_key=DeviceKey(key="signal_strength", device_id=None),
4192 name="Signal Strength",
4193 native_value=-60,
4194 ),
4195 },
4196 binary_entity_descriptions={},
4197 binary_entity_values={},
4198 events={
4199 DeviceKey(key="button", device_id=None): Event(
4200 device_key=DeviceKey(key="button", device_id=None),
4201 name="Button",
4202 event_type="press",
4203 event_properties=None,
4204 )
4205 },
4206 )
4207
4208
4209 def test_bthome_shelly_button_no_press(caplog):
4210 """Test BTHome parser for button event followed by empty event."""
4211 data_string = b"\x44\x00\x21\x01\x5e\x3a\x01"
4212 advertisement = bytes_to_service_info(
4213 data_string, local_name="SBBT-002C", address="A4:C1:38:8D:18:B2"
4214 )
4215
4216 device = BTHomeBluetoothDeviceData()
4217
4218 assert device.update(advertisement) == SensorUpdate(
4219 title="Shelly BLU Button1 18B2",
4220 devices={
4221 None: SensorDeviceInfo(
4222 name="Shelly BLU Button1 18B2",
4223 manufacturer="Shelly",
4224 model="BLU Button1",
4225 sw_version="BTHome BLE v2",
4226 hw_version=None,
4227 )
4228 },
4229 entity_descriptions={
4230 KEY_PACKET_ID: SensorDescription(
4231 device_key=KEY_PACKET_ID,
4232 device_class=SensorDeviceClass.PACKET_ID,
4233 native_unit_of_measurement=None,
4234 ),
4235 KEY_BATTERY: SensorDescription(
4236 device_key=KEY_BATTERY,
4237 device_class=SensorDeviceClass.BATTERY,
4238 native_unit_of_measurement=Units.PERCENTAGE,
4239 ),
4240 KEY_SIGNAL_STRENGTH: SensorDescription(
4241 device_key=KEY_SIGNAL_STRENGTH,
4242 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
4243 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
4244 ),
4245 },
4246 entity_values={
4247 KEY_PACKET_ID: SensorValue(
4248 device_key=KEY_PACKET_ID, name="Packet Id", native_value=33
4249 ),
4250 KEY_BATTERY: SensorValue(KEY_BATTERY, name="Battery", native_value=94),
4251 KEY_SIGNAL_STRENGTH: SensorValue(
4252 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
4253 ),
4254 },
4255 events={
4256 DeviceKey(key="button", device_id=None): Event(
4257 device_key=DeviceKey(key="button", device_id=None),
4258 name="Button",
4259 event_type="press",
4260 event_properties=None,
4261 )
4262 },
4263 )
4264
4265 data_string = b"\x44\x00\x23\x01\x5e\x3a\x00"
4266 advertisement = bytes_to_service_info(
4267 data_string, local_name="SBBT-002C", address="A4:C1:38:8D:18:B2"
4268 )
4269
4270 assert device.update(advertisement) == SensorUpdate(
4271 title="Shelly BLU Button1 18B2",
4272 devices={
4273 None: SensorDeviceInfo(
4274 name="Shelly BLU Button1 18B2",
4275 manufacturer="Shelly",
4276 model="BLU Button1",
4277 sw_version="BTHome BLE v2",
4278 hw_version=None,
4279 )
4280 },
4281 entity_descriptions={
4282 KEY_PACKET_ID: SensorDescription(
4283 device_key=KEY_PACKET_ID,
4284 device_class=SensorDeviceClass.PACKET_ID,
4285 native_unit_of_measurement=None,
4286 ),
4287 KEY_BATTERY: SensorDescription(
4288 device_key=KEY_BATTERY,
4289 device_class=SensorDeviceClass.BATTERY,
4290 native_unit_of_measurement=Units.PERCENTAGE,
4291 ),
4292 KEY_SIGNAL_STRENGTH: SensorDescription(
4293 device_key=KEY_SIGNAL_STRENGTH,
4294 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
4295 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
4296 ),
4297 },
4298 entity_values={
4299 KEY_PACKET_ID: SensorValue(
4300 device_key=KEY_PACKET_ID, name="Packet Id", native_value=35
4301 ),
4302 KEY_BATTERY: SensorValue(KEY_BATTERY, name="Battery", native_value=94),
4303 KEY_SIGNAL_STRENGTH: SensorValue(
4304 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
4305 ),
4306 },
4307 events={},
4308 )
4309
4310
4311 def test_bthome_test(caplog):
4312 """Test BTHome parser for acceleration in m/s°."""
4313 data_string = b"@\x00\xa7\x0cx\x0b\x10\x00"
4314 advertisement = bytes_to_service_info(
4315 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
4316 )
4317
4318 device = BTHomeBluetoothDeviceData()
4319
4320 assert device.update(advertisement) == SensorUpdate(
4321 title="TEST DEVICE 18B2",
4322 devices={
4323 None: SensorDeviceInfo(
4324 name="TEST DEVICE 18B2",
4325 manufacturer=None,
4326 model="BTHome sensor",
4327 sw_version="BTHome BLE v2",
4328 hw_version=None,
4329 )
4330 },
4331 entity_descriptions={
4332 KEY_PACKET_ID: SensorDescription(
4333 device_key=KEY_PACKET_ID,
4334 device_class=SensorDeviceClass.PACKET_ID,
4335 native_unit_of_measurement=None,
4336 ),
4337 KEY_VOLTAGE: SensorDescription(
4338 device_key=KEY_VOLTAGE,
4339 device_class=SensorDeviceClass.VOLTAGE,
4340 native_unit_of_measurement=Units.ELECTRIC_POTENTIAL_VOLT,
4341 ),
4342 KEY_SIGNAL_STRENGTH: SensorDescription(
4343 device_key=KEY_SIGNAL_STRENGTH,
4344 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
4345 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
4346 ),
4347 },
4348 entity_values={
4349 KEY_PACKET_ID: SensorValue(
4350 device_key=KEY_PACKET_ID, name="Packet Id", native_value=167
4351 ),
4352 KEY_VOLTAGE: SensorValue(
4353 device_key=KEY_VOLTAGE, name="Voltage", native_value=2.936
4354 ),
4355 KEY_SIGNAL_STRENGTH: SensorValue(
4356 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
4357 ),
4358 },
4359 binary_entity_descriptions={
4360 KEY_BINARY_POWER: BinarySensorDescription(
4361 device_key=KEY_BINARY_POWER,
4362 device_class=BinarySensorDeviceClass.POWER,
4363 ),
4364 },
4365 binary_entity_values={
4366 KEY_BINARY_POWER: BinarySensorValue(
4367 device_key=KEY_BINARY_POWER, name="Power", native_value=False
4368 ),
4369 },
4370 )
4371
4372
4373 def test_incorrect_bthome_version(caplog):
4374 """Test BTHome parser with incorrect BTHome version."""
4375 data_string = b"\x00"
4376 advertisement = bytes_to_service_info(
4377 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
4378 )
4379
4380 device = BTHomeBluetoothDeviceData()
4381
4382 device.update(advertisement)
4383 assert device.supported(advertisement) is False
4384 assert (
4385 "18B2: Sensor is set to use BTHome version 0, which is not existing. "
4386 "Please modify the version in the first byte of the service data" in caplog.text
4387 )
4388
4389
4390 def test_bthome_not_sending_object_ids_in_numerical_order(caplog):
4391 """Test BTHome parser for not sending object ids in numerical order."""
4392 data_string = b"\x40\x03\xbf\x13\x02\xca\x09"
4393 advertisement = bytes_to_service_info(
4394 data_string, local_name="ATC_8D18B2", address="A4:C1:38:8D:18:B2"
4395 )
4396
4397 device = BTHomeBluetoothDeviceData()
4398 device.update(advertisement)
4399 assert (
4400 "ATC 18B2: BTHome device is not sending object ids in numerical order "
4401 "(from low to high object id). This can cause issues with your BTHome receiver, "
4402 "payload: 03bf1302ca09" in caplog.text
4403 )
4404
4405
4406 def test_bthome_invalid_object_payload_data_length(caplog):
4407 """Test BTHome parser for object with invalid payload data length."""
4408 data_string = b"\x40\x02\xca\x09\x03\xbf" # missing 1 byte
4409 advertisement = bytes_to_service_info(
4410 data_string, local_name="ATC_8D18B2", address="A4:C1:38:8D:18:B2"
4411 )
4412
4413 device = BTHomeBluetoothDeviceData()
4414 device.update(advertisement)
4415 assert "ATC 18B2: Invalid payload data length, payload: 02ca0903bf" in caplog.text
4416
4417
4418 def test_bthome_direction_precipitation(caplog):
4419 """Test BTHome parser for Direction(0x5E) and Precipitation(0x5F) without encryption."""
4420 data_string = b"\x40\x5e\x3f\x75\x5f\x11\xd5"
4421 advertisement = bytes_to_service_info(
4422 data_string, local_name="SBWS-90CM", address="A4:C1:38:55:AA:55"
4423 )
4424
4425 device = BTHomeBluetoothDeviceData()
4426 assert device.update(advertisement) == SensorUpdate(
4427 title="SBWS-90CM AA55",
4428 devices={
4429 None: SensorDeviceInfo(
4430 name="SBWS-90CM AA55",
4431 manufacturer=None,
4432 model="BTHome sensor",
4433 sw_version="BTHome BLE v2",
4434 hw_version=None,
4435 )
4436 },
4437 entity_descriptions={
4438 DeviceKey(key="direction", device_id=None): SensorDescription(
4439 device_key=DeviceKey(key="direction", device_id=None),
4440 device_class=ExtendedSensorDeviceClass.DIRECTION,
4441 native_unit_of_measurement=Units.DEGREE,
4442 ),
4443 DeviceKey(key="precipitation", device_id=None): SensorDescription(
4444 device_key=DeviceKey(key="precipitation", device_id=None),
4445 device_class=ExtendedSensorDeviceClass.PRECIPITATION,
4446 native_unit_of_measurement=Units.LENGTH_MILLIMETERS,
4447 ),
4448 KEY_SIGNAL_STRENGTH: SensorDescription(
4449 device_key=KEY_SIGNAL_STRENGTH,
4450 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
4451 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
4452 ),
4453 },
4454 entity_values={
4455 DeviceKey(key="direction", device_id=None): SensorValue(
4456 device_key=DeviceKey(key="direction", device_id=None),
4457 name="Direction",
4458 native_value=300.15,
4459 ),
4460 DeviceKey(key="precipitation", device_id=None): SensorValue(
4461 device_key=DeviceKey(key="precipitation", device_id=None),
4462 name="Precipitation",
4463 native_value=5454.5,
4464 ),
4465 KEY_SIGNAL_STRENGTH: SensorValue(
4466 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
4467 ),
4468 },
4469 )
4470
4471
4472 def test_bthome_channel(caplog):
4473 """Test BTHome parser for Channel(0x60) without encryption."""
4474 data_string = b"\x40\x60\x01"
4475 advertisement = bytes_to_service_info(
4476 data_string, local_name="SBRC-005B", address="28:DB:A7:B5:D4:03"
4477 )
4478
4479 device = BTHomeBluetoothDeviceData()
4480 assert device.update(advertisement) == SensorUpdate(
4481 title="SBRC-005B D403",
4482 devices={
4483 None: SensorDeviceInfo(
4484 name="SBRC-005B D403",
4485 manufacturer=None,
4486 model="BTHome sensor",
4487 sw_version="BTHome BLE v2",
4488 hw_version=None,
4489 )
4490 },
4491 entity_descriptions={
4492 DeviceKey(key="channel", device_id=None): SensorDescription(
4493 device_key=DeviceKey(key="channel", device_id=None),
4494 device_class=ExtendedSensorDeviceClass.CHANNEL,
4495 native_unit_of_measurement=None,
4496 ),
4497 KEY_SIGNAL_STRENGTH: SensorDescription(
4498 device_key=KEY_SIGNAL_STRENGTH,
4499 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
4500 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
4501 ),
4502 },
4503 entity_values={
4504 DeviceKey(key="channel", device_id=None): SensorValue(
4505 device_key=DeviceKey(key="channel", device_id=None),
4506 name="Channel",
4507 native_value=1,
4508 ),
4509 KEY_SIGNAL_STRENGTH: SensorValue(
4510 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
4511 ),
4512 },
4513 )
4514
4515
4516 def test_bthome_revolutions_per_minute(caplog):
4517 """Test BTHome parser for Revolutions Per Minute(0x61) without encryption."""
4518 data_string = b"\x40\x61\xac\x0d"
4519 advertisement = bytes_to_service_info(
4520 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
4521 )
4522
4523 device = BTHomeBluetoothDeviceData()
4524 assert device.update(advertisement) == SensorUpdate(
4525 title="TEST DEVICE 18B2",
4526 devices={
4527 None: SensorDeviceInfo(
4528 name="TEST DEVICE 18B2",
4529 manufacturer=None,
4530 model="BTHome sensor",
4531 sw_version="BTHome BLE v2",
4532 hw_version=None,
4533 )
4534 },
4535 entity_descriptions={
4536 DeviceKey(key="rotational_speed", device_id=None): SensorDescription(
4537 device_key=DeviceKey(key="rotational_speed", device_id=None),
4538 device_class=ExtendedSensorDeviceClass.ROTATIONAL_SPEED,
4539 native_unit_of_measurement=Units.REVOLUTIONS_PER_MINUTE,
4540 ),
4541 KEY_SIGNAL_STRENGTH: SensorDescription(
4542 device_key=KEY_SIGNAL_STRENGTH,
4543 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
4544 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
4545 ),
4546 },
4547 entity_values={
4548 DeviceKey(key="rotational_speed", device_id=None): SensorValue(
4549 device_key=DeviceKey(key="rotational_speed", device_id=None),
4550 name="Rotational Speed",
4551 native_value=3500,
4552 ),
4553 KEY_SIGNAL_STRENGTH: SensorValue(
4554 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
4555 ),
4556 },
4557 )
4558
4559
4560 def test_bthome_light_level(caplog):
4561 """Test BTHome parser for Light Level (0x64) without encryption."""
4562 data_string = b"\x40\x64\x02"
4563 advertisement = bytes_to_service_info(
4564 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
4565 )
4566
4567 device = BTHomeBluetoothDeviceData()
4568 assert device.update(advertisement) == SensorUpdate(
4569 title="TEST DEVICE 18B2",
4570 devices={
4571 None: SensorDeviceInfo(
4572 name="TEST DEVICE 18B2",
4573 manufacturer=None,
4574 model="BTHome sensor",
4575 sw_version="BTHome BLE v2",
4576 hw_version=None,
4577 )
4578 },
4579 entity_descriptions={
4580 KEY_LIGHT_LEVEL: SensorDescription(
4581 device_key=KEY_LIGHT_LEVEL,
4582 device_class=ExtendedSensorDeviceClass.LIGHT_LEVEL,
4583 native_unit_of_measurement=None,
4584 ),
4585 KEY_SIGNAL_STRENGTH: SensorDescription(
4586 device_key=KEY_SIGNAL_STRENGTH,
4587 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
4588 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
4589 ),
4590 },
4591 entity_values={
4592 KEY_LIGHT_LEVEL: SensorValue(
4593 device_key=KEY_LIGHT_LEVEL,
4594 name="Light Level",
4595 native_value=2,
4596 ),
4597 KEY_SIGNAL_STRENGTH: SensorValue(
4598 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
4599 ),
4600 },
4601 )
4602
4603
4604 def test_bthome_settings_revision(caplog):
4605 """Test BTHome parser for Settings Revision (0x65) without encryption."""
4606 data_string = b"\x40\x65\x03"
4607 advertisement = bytes_to_service_info(
4608 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
4609 )
4610
4611 device = BTHomeBluetoothDeviceData()
4612 assert device.update(advertisement) == SensorUpdate(
4613 title="TEST DEVICE 18B2",
4614 devices={
4615 None: SensorDeviceInfo(
4616 name="TEST DEVICE 18B2",
4617 manufacturer=None,
4618 model="BTHome sensor",
4619 sw_version="BTHome BLE v2",
4620 hw_version=None,
4621 )
4622 },
4623 entity_descriptions={
4624 KEY_SETTINGS_REVISION: SensorDescription(
4625 device_key=KEY_SETTINGS_REVISION,
4626 device_class=ExtendedSensorDeviceClass.SETTINGS_REVISION,
4627 native_unit_of_measurement=None,
4628 ),
4629 KEY_SIGNAL_STRENGTH: SensorDescription(
4630 device_key=KEY_SIGNAL_STRENGTH,
4631 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
4632 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
4633 ),
4634 },
4635 entity_values={
4636 KEY_SETTINGS_REVISION: SensorValue(
4637 device_key=KEY_SETTINGS_REVISION,
4638 name="Settings Revision",
4639 native_value=3,
4640 ),
4641 KEY_SIGNAL_STRENGTH: SensorValue(
4642 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
4643 ),
4644 },
4645 )
4646
4647
4648 def test_bthome_event_command_step_up_5(caplog):
4649 """Test BTHome parser for a command event (step_up with 1-byte arg)."""
4650 data_string = b"\x40\x3b\x01\x03\x05"
4651 advertisement = bytes_to_service_info(
4652 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
4653 )
4654
4655 device = BTHomeBluetoothDeviceData()
4656
4657 assert device.update(advertisement) == SensorUpdate(
4658 title="TEST DEVICE 18B2",
4659 devices={
4660 None: SensorDeviceInfo(
4661 name="TEST DEVICE 18B2",
4662 manufacturer=None,
4663 model="BTHome sensor",
4664 sw_version="BTHome BLE v2",
4665 hw_version=None,
4666 )
4667 },
4668 entity_descriptions={
4669 KEY_SIGNAL_STRENGTH: SensorDescription(
4670 device_key=KEY_SIGNAL_STRENGTH,
4671 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
4672 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
4673 ),
4674 },
4675 entity_values={
4676 KEY_SIGNAL_STRENGTH: SensorValue(
4677 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
4678 ),
4679 },
4680 events={
4681 KEY_COMMAND: Event(
4682 device_key=KEY_COMMAND,
4683 name="Command",
4684 event_type="step_up",
4685 event_properties={"args": "05"},
4686 ),
4687 },
4688 )
4689
4690
4691 def test_bthome_event_command_toggle_no_args(caplog):
4692 """Test BTHome parser for a command event with zero-length args (toggle)."""
4693 data_string = b"\x40\x3b\x00\x02"
4694 advertisement = bytes_to_service_info(
4695 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
4696 )
4697
4698 device = BTHomeBluetoothDeviceData()
4699
4700 assert device.update(advertisement) == SensorUpdate(
4701 title="TEST DEVICE 18B2",
4702 devices={
4703 None: SensorDeviceInfo(
4704 name="TEST DEVICE 18B2",
4705 manufacturer=None,
4706 model="BTHome sensor",
4707 sw_version="BTHome BLE v2",
4708 hw_version=None,
4709 )
4710 },
4711 entity_descriptions={
4712 KEY_SIGNAL_STRENGTH: SensorDescription(
4713 device_key=KEY_SIGNAL_STRENGTH,
4714 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
4715 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
4716 ),
4717 },
4718 entity_values={
4719 KEY_SIGNAL_STRENGTH: SensorValue(
4720 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
4721 ),
4722 },
4723 events={
4724 KEY_COMMAND: Event(
4725 device_key=KEY_COMMAND,
4726 name="Command",
4727 event_type="toggle",
4728 event_properties={"args": ""},
4729 ),
4730 },
4731 )
4732
4733
4734 def test_bthome_event_command_unknown_opcode(caplog):
4735 """Test BTHome parser for a command with unknown opcode (no event fired)."""
4736 # opcode 0x7F is manufacturer-specific / unknown; no standard event should fire.
4737 data_string = b"\x40\x3b\x00\x7f"
4738 advertisement = bytes_to_service_info(
4739 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
4740 )
4741
4742 device = BTHomeBluetoothDeviceData()
4743
4744 assert device.update(advertisement) == SensorUpdate(
4745 title="TEST DEVICE 18B2",
4746 devices={
4747 None: SensorDeviceInfo(
4748 name="TEST DEVICE 18B2",
4749 manufacturer=None,
4750 model="BTHome sensor",
4751 sw_version="BTHome BLE v2",
4752 hw_version=None,
4753 )
4754 },
4755 entity_descriptions={
4756 KEY_SIGNAL_STRENGTH: SensorDescription(
4757 device_key=KEY_SIGNAL_STRENGTH,
4758 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
4759 native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
4760 ),
4761 },
4762 entity_values={
4763 KEY_SIGNAL_STRENGTH: SensorValue(
4764 device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
4765 ),
4766 },
4767 )
4768
4769
4770 @pytest.mark.parametrize(
4771 "payload",
4772 [b"\x53", b"\x3b", b"\x54"],
4773 ids=["string", "command", "raw"],
4774 )
4775 def test_truncated_v2_object_length_byte(payload: bytes) -> None:
4776 """Regression: V2 object truncated to type byte only must not crash."""
4777 device = BTHomeBluetoothDeviceData()
4778 device.set_title("test")
4779 device.bthome_version = BTHomeVersion.V2
4780 assert device._parse_payload(payload, 0.0) is False
4781
4782
4783 def test_bthome_device_type_id() -> None:
4784 """Object 0xF0 stores a manufacturer device type id (uint16 LE)."""
4785 # adv_info + F0 0100 (device_type_id = 1)
4786 data_string = b"\x40\xf0\x01\x00"
4787 advertisement = bytes_to_service_info(
4788 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
4789 )
4790
4791 device = BTHomeBluetoothDeviceData()
4792 device.update(advertisement)
4793
4794 assert device.device_type_id == 1
4795
4796
4797 def test_bthome_firmware_version_4_bytes() -> None:
4798 """Object 0xF1 carries a 4-component firmware version, byte-reversed."""
4799 # adv_info + F1 00 01 02 04 → sw_version "4.2.1.0"
4800 data_string = b"\x40\xf1\x00\x01\x02\x04"
4801 advertisement = bytes_to_service_info(
4802 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
4803 )
4804
4805 device = BTHomeBluetoothDeviceData()
4806 result = device.update(advertisement)
4807
4808 assert result.devices[None].sw_version == "4.2.1.0"
4809
4810
4811 def test_bthome_firmware_version_3_bytes() -> None:
4812 """Object 0xF2 carries a 3-component firmware version, byte-reversed."""
4813 # adv_info + F2 00 01 06 → sw_version "6.1.0"
4814 data_string = b"\x40\xf2\x00\x01\x06"
4815 advertisement = bytes_to_service_info(
4816 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
4817 )
4818
4819 device = BTHomeBluetoothDeviceData()
4820 result = device.update(advertisement)
4821
4822 assert result.devices[None].sw_version == "6.1.0"
4823
4824
4825 def test_bthome_firmware_version_4_overrides_default_sw_version() -> None:
4826 """F1 should override the default 'BTHome BLE v2' sw_version."""
4827 # Temperature 0x02 + F1 firmware version
4828 data_string = b"\x40\x02\xca\x09\xf1\x00\x01\x02\x04"
4829 advertisement = bytes_to_service_info(
4830 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
4831 )
4832
4833 device = BTHomeBluetoothDeviceData()
4834 result = device.update(advertisement)
4835
4836 assert result.devices[None].sw_version == "4.2.1.0"
4837 assert result.entity_values[KEY_TEMPERATURE].native_value == 25.06
4838
4839
4840 def test_bthome_device_info_combined() -> None:
4841 """F0 + F1 in the same advertisement must both be consumed without errors."""
4842 # adv_info + F0 0100 + F1 00 01 02 04
4843 data_string = b"\x40\xf0\x01\x00\xf1\x00\x01\x02\x04"
4844 advertisement = bytes_to_service_info(
4845 data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
4846 )
4847
4848 device = BTHomeBluetoothDeviceData()
4849 result = device.update(advertisement)
4850
4851 assert device.device_type_id == 1
4852 assert result.devices[None].sw_version == "4.2.1.0"
4853
4854
4855 def test_bthome_firmware_version_3_overrides_default_sw_version_encrypted() -> None:
4856 """F2 takes precedence over the 'BTHome BLE v2 (encrypted)' default too."""
4857 device = BTHomeBluetoothDeviceData()
4858 device.set_title("test")
4859 device.encryption_scheme = EncryptionScheme.BTHOME_BINDKEY
4860 device.bthome_version = BTHomeVersion.V2
4861 # Seed the encrypted default that update() would have set.
4862 device.set_device_sw_version("BTHome BLE v2 (encrypted)")
4863 assert device._get_device_info(None).sw_version == "BTHome BLE v2 (encrypted)"
4864
4865 # F2 00 01 06 → 6.1.0 should overwrite the encrypted default in place.
4866 assert device._parse_payload(b"\xf2\x00\x01\x06", 0.0) is True
4867 assert device._get_device_info(None).sw_version == "6.1.0"