| 1 |
"""Tests for the parser of BLE advertisements in BTHome V1 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.parser import BTHomeBluetoothDeviceData, BTHomeVersion, EncryptionScheme |
| 24 |
|
| 25 |
ADVERTISEMENT_TIME = 1709331995.5181565 |
| 26 |
|
| 27 |
KEY_BATTERY = DeviceKey(key="battery", device_id=None) |
| 28 |
KEY_BINARY_GENERIC = DeviceKey(key="generic", device_id=None) |
| 29 |
KEY_BINARY_OPENING = DeviceKey(key="opening", device_id=None) |
| 30 |
KEY_BINARY_POWER = DeviceKey(key="power", device_id=None) |
| 31 |
KEY_BUTTON = DeviceKey(key="button", device_id=None) |
| 32 |
KEY_CO2 = DeviceKey(key="carbon_dioxide", device_id=None) |
| 33 |
KEY_DIMMER = DeviceKey(key="dimmer", device_id=None) |
| 34 |
KEY_COUNT = DeviceKey(key="count", device_id=None) |
| 35 |
KEY_DEW_POINT = DeviceKey(key="dew_point", device_id=None) |
| 36 |
KEY_ENERGY = DeviceKey(key="energy", device_id=None) |
| 37 |
KEY_HUMIDITY = DeviceKey(key="humidity", device_id=None) |
| 38 |
KEY_ILLUMINANCE = DeviceKey(key="illuminance", device_id=None) |
| 39 |
KEY_MASS = DeviceKey(key="mass", device_id=None) |
| 40 |
KEY_MOISTURE = DeviceKey(key="moisture", device_id=None) |
| 41 |
KEY_PACKET_ID = DeviceKey(key="packet_id", device_id=None) |
| 42 |
KEY_PM25 = DeviceKey(key="pm25", device_id=None) |
| 43 |
KEY_PM10 = DeviceKey(key="pm10", device_id=None) |
| 44 |
KEY_POWER = DeviceKey(key="power", device_id=None) |
| 45 |
KEY_PRESSURE = DeviceKey(key="pressure", device_id=None) |
| 46 |
KEY_SIGNAL_STRENGTH = DeviceKey(key="signal_strength", device_id=None) |
| 47 |
KEY_TEMPERATURE = DeviceKey(key="temperature", device_id=None) |
| 48 |
KEY_TIMESTAMP = DeviceKey(key="timestamp", device_id=None) |
| 49 |
KEY_VOC = DeviceKey(key="volatile_organic_compounds", device_id=None) |
| 50 |
KEY_VOLTAGE = DeviceKey(key="voltage", device_id=None) |
| 51 |
|
| 52 |
|
| 53 |
@pytest.fixture(autouse=True) |
| 54 |
def logging_config(caplog): |
| 55 |
caplog.set_level(logging.DEBUG) |
| 56 |
|
| 57 |
|
| 58 |
@pytest.fixture(autouse=True) |
| 59 |
def mock_platform(): |
| 60 |
with patch("sys.platform") as p: |
| 61 |
p.return_value = "linux" |
| 62 |
yield p |
| 63 |
|
| 64 |
|
| 65 |
def bytes_to_service_info( |
| 66 |
payload: bytes, local_name: str, address: str = "00:00:00:00:00:00" |
| 67 |
) -> BluetoothServiceInfoBleak: |
| 68 |
"""Convert bytes to service info""" |
| 69 |
return BluetoothServiceInfoBleak( |
| 70 |
name=local_name, |
| 71 |
address=address, |
| 72 |
rssi=-60, |
| 73 |
manufacturer_data={}, |
| 74 |
service_data={"0000181c-0000-1000-8000-00805f9b34fb": payload}, |
| 75 |
service_uuids=["0000181c-0000-1000-8000-00805f9b34fb"], |
| 76 |
source="", |
| 77 |
device=None, |
| 78 |
advertisement=None, |
| 79 |
connectable=False, |
| 80 |
time=ADVERTISEMENT_TIME, |
| 81 |
tx_power=None, |
| 82 |
) |
| 83 |
|
| 84 |
|
| 85 |
def bytes_to_encrypted_service_info( |
| 86 |
payload: bytes, local_name: str, address: str = "00:00:00:00:00:00" |
| 87 |
) -> BluetoothServiceInfoBleak: |
| 88 |
"""Convert bytes to service info""" |
| 89 |
return BluetoothServiceInfoBleak( |
| 90 |
name=local_name, |
| 91 |
address=address, |
| 92 |
rssi=-60, |
| 93 |
manufacturer_data={}, |
| 94 |
service_data={"0000181e-0000-1000-8000-00805f9b34fb": payload}, |
| 95 |
service_uuids=["0000181e-0000-1000-8000-00805f9b34fb"], |
| 96 |
source="", |
| 97 |
device=None, |
| 98 |
advertisement=None, |
| 99 |
connectable=False, |
| 100 |
time=ADVERTISEMENT_TIME, |
| 101 |
tx_power=None, |
| 102 |
) |
| 103 |
|
| 104 |
|
| 105 |
def test_can_create(): |
| 106 |
BTHomeBluetoothDeviceData() |
| 107 |
|
| 108 |
|
| 109 |
def test_encryption_key_needed(): |
| 110 |
"""Test that we can detect that an encryption key is needed.""" |
| 111 |
data_string = b'\xfb\xa45\xe4\xd3\xc3\x12\xfb\x00\x11"3W\xd9\n\x99' |
| 112 |
advertisement = bytes_to_encrypted_service_info( |
| 113 |
payload=data_string, |
| 114 |
local_name="ATC_8D18B2", |
| 115 |
address="A4:C1:38:8D:18:B2", |
| 116 |
) |
| 117 |
|
| 118 |
device = BTHomeBluetoothDeviceData() |
| 119 |
|
| 120 |
assert device.supported(advertisement) |
| 121 |
assert device.encryption_scheme == EncryptionScheme.BTHOME_BINDKEY |
| 122 |
assert not device.bindkey_verified |
| 123 |
|
| 124 |
|
| 125 |
def test_encryption_no_key_needed(): |
| 126 |
"""Test that we can detect that no encryption key is needed.""" |
| 127 |
data_string = b"\x02\x00\x0c\x04\x04\x13\x8a\x01" |
| 128 |
advertisement = bytes_to_service_info( |
| 129 |
payload=data_string, |
| 130 |
local_name="ATC_8D18B2", |
| 131 |
address="A4:C1:38:8D:18:B2", |
| 132 |
) |
| 133 |
|
| 134 |
device = BTHomeBluetoothDeviceData() |
| 135 |
|
| 136 |
assert device.supported(advertisement) |
| 137 |
assert device.encryption_scheme == EncryptionScheme.NONE |
| 138 |
assert not device.bindkey_verified |
| 139 |
|
| 140 |
|
| 141 |
def test_encryption_downgrade_fails(): |
| 142 |
"""Test that unencrypted data gets rejected when a bindkey is provided.""" |
| 143 |
bindkey = "814aac74c4f17b6c1581e1ab87816b99" |
| 144 |
data_string = b"\x02\x00\x0c\x04\x04\x13\x8a\x01" |
| 145 |
advertisement = bytes_to_service_info( |
| 146 |
payload=data_string, |
| 147 |
local_name="ATC_8D18B2", |
| 148 |
address="A4:C1:38:8D:18:B2", |
| 149 |
) |
| 150 |
|
| 151 |
device = BTHomeBluetoothDeviceData(bindkey=bytes.fromhex(bindkey)) |
| 152 |
|
| 153 |
assert not device.supported(advertisement) |
| 154 |
assert not device.bindkey_verified |
| 155 |
|
| 156 |
|
| 157 |
def test_downgrade_detected_flag_v1(): |
| 158 |
"""Test that downgrade_detected flag is set correctly.""" |
| 159 |
bindkey = "231d39c1d7cc1ab1aee224cd096db932" |
| 160 |
|
| 161 |
|
| 162 |
encrypted_data = b'\xfb\xa45\xe4\xd3\xc3\x12\xfb\x00\x11"3W\xd9\n\x99' |
| 163 |
encrypted_adv = bytes_to_encrypted_service_info( |
| 164 |
payload=encrypted_data, |
| 165 |
local_name="TEST DEVICE", |
| 166 |
address="54:48:E6:8F:80:A5", |
| 167 |
) |
| 168 |
|
| 169 |
|
| 170 |
unencrypted_data = b"\x02\x00\x0c\x04\x04\x13\x8a\x01" |
| 171 |
unencrypted_adv = bytes_to_service_info( |
| 172 |
payload=unencrypted_data, |
| 173 |
local_name="TEST DEVICE", |
| 174 |
address="54:48:E6:8F:80:A5", |
| 175 |
) |
| 176 |
|
| 177 |
device = BTHomeBluetoothDeviceData(bindkey=bytes.fromhex(bindkey)) |
| 178 |
|
| 179 |
|
| 180 |
assert device.supported(encrypted_adv) |
| 181 |
device.update(encrypted_adv) |
| 182 |
assert device.bindkey_verified |
| 183 |
assert not device.downgrade_detected |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
device.update(unencrypted_adv) |
| 189 |
assert device.downgrade_detected |
| 190 |
|
| 191 |
|
| 192 |
device.update(encrypted_adv) |
| 193 |
assert not device.downgrade_detected |
| 194 |
|
| 195 |
|
| 196 |
def test_bindkey_wrong(): |
| 197 |
"""Test BTHome parser with wrong encryption key.""" |
| 198 |
bindkey = "814aac74c4f17b6c1581e1ab87816b99" |
| 199 |
data_string = b'\xfb\xa45\xe4\xd3\xc3\x12\xfb\x00\x11"3W\xd9\n\x99' |
| 200 |
advertisement = bytes_to_encrypted_service_info( |
| 201 |
data_string, |
| 202 |
local_name="ATC_8D18B2", |
| 203 |
address="A4:C1:38:8D:18:B2", |
| 204 |
) |
| 205 |
|
| 206 |
device = BTHomeBluetoothDeviceData(bindkey=bytes.fromhex(bindkey)) |
| 207 |
assert device.supported(advertisement) |
| 208 |
assert not device.bindkey_verified |
| 209 |
assert device.update(advertisement) == SensorUpdate( |
| 210 |
title="ATC 18B2", |
| 211 |
devices={ |
| 212 |
None: SensorDeviceInfo( |
| 213 |
name="ATC 18B2", |
| 214 |
manufacturer="Xiaomi", |
| 215 |
model="Temperature/Humidity sensor", |
| 216 |
sw_version="BTHome BLE v1 (encrypted)", |
| 217 |
hw_version=None, |
| 218 |
) |
| 219 |
}, |
| 220 |
entity_descriptions={ |
| 221 |
KEY_SIGNAL_STRENGTH: SensorDescription( |
| 222 |
device_key=KEY_SIGNAL_STRENGTH, |
| 223 |
device_class=SensorDeviceClass.SIGNAL_STRENGTH, |
| 224 |
native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT, |
| 225 |
), |
| 226 |
}, |
| 227 |
entity_values={ |
| 228 |
KEY_SIGNAL_STRENGTH: SensorValue( |
| 229 |
name="Signal Strength", device_key=KEY_SIGNAL_STRENGTH, native_value=-60 |
| 230 |
), |
| 231 |
}, |
| 232 |
) |
| 233 |
|
| 234 |
|
| 235 |
def test_bindkey_correct(): |
| 236 |
"""Test BTHome parser with correct encryption key.""" |
| 237 |
bindkey = "231d39c1d7cc1ab1aee224cd096db932" |
| 238 |
data_string = b'\xfb\xa45\xe4\xd3\xc3\x12\xfb\x00\x11"3W\xd9\n\x99' |
| 239 |
advertisement = bytes_to_encrypted_service_info( |
| 240 |
data_string, |
| 241 |
local_name="TEST DEVICE", |
| 242 |
address="54:48:E6:8F:80:A5", |
| 243 |
) |
| 244 |
|
| 245 |
device = BTHomeBluetoothDeviceData(bindkey=bytes.fromhex(bindkey)) |
| 246 |
assert device.supported(advertisement) |
| 247 |
assert device.bindkey_verified |
| 248 |
assert device.update(advertisement) == SensorUpdate( |
| 249 |
title="TEST DEVICE 80A5", |
| 250 |
devices={ |
| 251 |
None: SensorDeviceInfo( |
| 252 |
name="TEST DEVICE 80A5", |
| 253 |
manufacturer=None, |
| 254 |
model="BTHome sensor", |
| 255 |
sw_version="BTHome BLE v1 (encrypted)", |
| 256 |
hw_version=None, |
| 257 |
) |
| 258 |
}, |
| 259 |
entity_descriptions={ |
| 260 |
KEY_TEMPERATURE: SensorDescription( |
| 261 |
device_key=KEY_TEMPERATURE, |
| 262 |
device_class=SensorDeviceClass.TEMPERATURE, |
| 263 |
native_unit_of_measurement=Units.TEMP_CELSIUS, |
| 264 |
), |
| 265 |
KEY_HUMIDITY: SensorDescription( |
| 266 |
device_key=KEY_HUMIDITY, |
| 267 |
device_class=SensorDeviceClass.HUMIDITY, |
| 268 |
native_unit_of_measurement=Units.PERCENTAGE, |
| 269 |
), |
| 270 |
KEY_SIGNAL_STRENGTH: SensorDescription( |
| 271 |
device_key=KEY_SIGNAL_STRENGTH, |
| 272 |
device_class=SensorDeviceClass.SIGNAL_STRENGTH, |
| 273 |
native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT, |
| 274 |
), |
| 275 |
}, |
| 276 |
entity_values={ |
| 277 |
KEY_TEMPERATURE: SensorValue( |
| 278 |
device_key=KEY_TEMPERATURE, name="Temperature", native_value=25.06 |
| 279 |
), |
| 280 |
KEY_HUMIDITY: SensorValue( |
| 281 |
device_key=KEY_HUMIDITY, name="Humidity", native_value=50.55 |
| 282 |
), |
| 283 |
KEY_SIGNAL_STRENGTH: SensorValue( |
| 284 |
device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60 |
| 285 |
), |
| 286 |
}, |
| 287 |
) |
| 288 |
|
| 289 |
|
| 290 |
def test_bindkey_verified_can_be_unset(): |
| 291 |
"""Test BTHome parser with wrong encryption key.""" |
| 292 |
bindkey = "814aac74c4f17b6c1581e1ab87816b99" |
| 293 |
data_string = b'\xfb\xa45\xe4\xd3\xc3\x12\xfb\x00\x11"3W\xd9\n\x99' |
| 294 |
advertisement = bytes_to_encrypted_service_info( |
| 295 |
data_string, |
| 296 |
local_name="ATC_8D18B2", |
| 297 |
address="A4:C1:38:8D:18:B2", |
| 298 |
) |
| 299 |
|
| 300 |
device = BTHomeBluetoothDeviceData(bindkey=bytes.fromhex(bindkey)) |
| 301 |
device.bindkey_verified = True |
| 302 |
|
| 303 |
assert device.supported(advertisement) |
| 304 |
assert not device.bindkey_verified |
| 305 |
|
| 306 |
|
| 307 |
def test_bthome_temperature_humidity(caplog): |
| 308 |
"""Test BTHome parser for temperature humidity reading without encryption.""" |
| 309 |
data_string = b"#\x02\xca\t\x03\x03\xbf\x13" |
| 310 |
advertisement = bytes_to_service_info( |
| 311 |
data_string, local_name="ATC_8D18B2", address="A4:C1:38:8D:18:B2" |
| 312 |
) |
| 313 |
|
| 314 |
device = BTHomeBluetoothDeviceData() |
| 315 |
assert device.update(advertisement) == SensorUpdate( |
| 316 |
title="ATC 18B2", |
| 317 |
devices={ |
| 318 |
None: SensorDeviceInfo( |
| 319 |
name="ATC 18B2", |
| 320 |
manufacturer="Xiaomi", |
| 321 |
model="Temperature/Humidity sensor", |
| 322 |
sw_version="BTHome BLE v1", |
| 323 |
hw_version=None, |
| 324 |
) |
| 325 |
}, |
| 326 |
entity_descriptions={ |
| 327 |
KEY_TEMPERATURE: SensorDescription( |
| 328 |
device_key=KEY_TEMPERATURE, |
| 329 |
device_class=SensorDeviceClass.TEMPERATURE, |
| 330 |
native_unit_of_measurement=Units.TEMP_CELSIUS, |
| 331 |
), |
| 332 |
KEY_HUMIDITY: SensorDescription( |
| 333 |
device_key=KEY_HUMIDITY, |
| 334 |
device_class=SensorDeviceClass.HUMIDITY, |
| 335 |
native_unit_of_measurement=Units.PERCENTAGE, |
| 336 |
), |
| 337 |
KEY_SIGNAL_STRENGTH: SensorDescription( |
| 338 |
device_key=KEY_SIGNAL_STRENGTH, |
| 339 |
device_class=SensorDeviceClass.SIGNAL_STRENGTH, |
| 340 |
native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT, |
| 341 |
), |
| 342 |
}, |
| 343 |
entity_values={ |
| 344 |
KEY_TEMPERATURE: SensorValue( |
| 345 |
device_key=KEY_TEMPERATURE, name="Temperature", native_value=25.06 |
| 346 |
), |
| 347 |
KEY_HUMIDITY: SensorValue( |
| 348 |
device_key=KEY_HUMIDITY, name="Humidity", native_value=50.55 |
| 349 |
), |
| 350 |
KEY_SIGNAL_STRENGTH: SensorValue( |
| 351 |
device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60 |
| 352 |
), |
| 353 |
}, |
| 354 |
) |
| 355 |
|
| 356 |
|
| 357 |
def test_bthome_temperature_humidity_battery(caplog): |
| 358 |
"""Test BTHome parser for temperature humidity battery reading.""" |
| 359 |
data_string = b"#\x02]\t\x03\x03\xb7\x18\x02\x01]" |
| 360 |
advertisement = bytes_to_service_info( |
| 361 |
data_string, local_name="ATC_8D18B2", address="A4:C1:38:8D:18:B2" |
| 362 |
) |
| 363 |
|
| 364 |
device = BTHomeBluetoothDeviceData() |
| 365 |
assert device.update(advertisement) == SensorUpdate( |
| 366 |
title="ATC 18B2", |
| 367 |
devices={ |
| 368 |
None: SensorDeviceInfo( |
| 369 |
name="ATC 18B2", |
| 370 |
manufacturer="Xiaomi", |
| 371 |
model="Temperature/Humidity sensor", |
| 372 |
sw_version="BTHome BLE v1", |
| 373 |
hw_version=None, |
| 374 |
) |
| 375 |
}, |
| 376 |
entity_descriptions={ |
| 377 |
KEY_TEMPERATURE: SensorDescription( |
| 378 |
device_key=KEY_TEMPERATURE, |
| 379 |
device_class=SensorDeviceClass.TEMPERATURE, |
| 380 |
native_unit_of_measurement=Units.TEMP_CELSIUS, |
| 381 |
), |
| 382 |
KEY_HUMIDITY: SensorDescription( |
| 383 |
device_key=KEY_HUMIDITY, |
| 384 |
device_class=SensorDeviceClass.HUMIDITY, |
| 385 |
native_unit_of_measurement=Units.PERCENTAGE, |
| 386 |
), |
| 387 |
KEY_BATTERY: SensorDescription( |
| 388 |
device_key=KEY_BATTERY, |
| 389 |
device_class=SensorDeviceClass.BATTERY, |
| 390 |
native_unit_of_measurement=Units.PERCENTAGE, |
| 391 |
), |
| 392 |
KEY_SIGNAL_STRENGTH: SensorDescription( |
| 393 |
device_key=KEY_SIGNAL_STRENGTH, |
| 394 |
device_class=SensorDeviceClass.SIGNAL_STRENGTH, |
| 395 |
native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT, |
| 396 |
), |
| 397 |
}, |
| 398 |
entity_values={ |
| 399 |
KEY_TEMPERATURE: SensorValue( |
| 400 |
device_key=KEY_TEMPERATURE, name="Temperature", native_value=23.97 |
| 401 |
), |
| 402 |
KEY_HUMIDITY: SensorValue( |
| 403 |
device_key=KEY_HUMIDITY, name="Humidity", native_value=63.27 |
| 404 |
), |
| 405 |
KEY_BATTERY: SensorValue( |
| 406 |
device_key=KEY_BATTERY, name="Battery", native_value=93 |
| 407 |
), |
| 408 |
KEY_SIGNAL_STRENGTH: SensorValue( |
| 409 |
device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60 |
| 410 |
), |
| 411 |
}, |
| 412 |
) |
| 413 |
|
| 414 |
|
| 415 |
def test_bthome_pressure(caplog): |
| 416 |
"""Test BTHome parser for pressure reading without encryption.""" |
| 417 |
data_string = b"\x04\x04\x13\x8a\x01" |
| 418 |
advertisement = bytes_to_service_info( |
| 419 |
data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2" |
| 420 |
) |
| 421 |
|
| 422 |
device = BTHomeBluetoothDeviceData() |
| 423 |
assert device.update(advertisement) == SensorUpdate( |
| 424 |
title="TEST DEVICE 18B2", |
| 425 |
devices={ |
| 426 |
None: SensorDeviceInfo( |
| 427 |
name="TEST DEVICE 18B2", |
| 428 |
manufacturer=None, |
| 429 |
model="BTHome sensor", |
| 430 |
sw_version="BTHome BLE v1", |
| 431 |
hw_version=None, |
| 432 |
) |
| 433 |
}, |
| 434 |
entity_descriptions={ |
| 435 |
KEY_PRESSURE: SensorDescription( |
| 436 |
device_key=KEY_PRESSURE, |
| 437 |
device_class=SensorDeviceClass.PRESSURE, |
| 438 |
native_unit_of_measurement=Units.PRESSURE_MBAR, |
| 439 |
), |
| 440 |
KEY_SIGNAL_STRENGTH: SensorDescription( |
| 441 |
device_key=KEY_SIGNAL_STRENGTH, |
| 442 |
device_class=SensorDeviceClass.SIGNAL_STRENGTH, |
| 443 |
native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT, |
| 444 |
), |
| 445 |
}, |
| 446 |
entity_values={ |
| 447 |
KEY_PRESSURE: SensorValue( |
| 448 |
device_key=KEY_PRESSURE, name="Pressure", native_value=1008.83 |
| 449 |
), |
| 450 |
KEY_SIGNAL_STRENGTH: SensorValue( |
| 451 |
device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60 |
| 452 |
), |
| 453 |
}, |
| 454 |
) |
| 455 |
|
| 456 |
|
| 457 |
def test_bthome_illuminance(caplog): |
| 458 |
"""Test BTHome parser for illuminance reading without encryption.""" |
| 459 |
data_string = b"\x04\x05\x13\x8a\x14" |
| 460 |
advertisement = bytes_to_service_info( |
| 461 |
data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2" |
| 462 |
) |
| 463 |
|
| 464 |
device = BTHomeBluetoothDeviceData() |
| 465 |
assert device.update(advertisement) == SensorUpdate( |
| 466 |
title="TEST DEVICE 18B2", |
| 467 |
devices={ |
| 468 |
None: SensorDeviceInfo( |
| 469 |
name="TEST DEVICE 18B2", |
| 470 |
manufacturer=None, |
| 471 |
model="BTHome sensor", |
| 472 |
sw_version="BTHome BLE v1", |
| 473 |
hw_version=None, |
| 474 |
) |
| 475 |
}, |
| 476 |
entity_descriptions={ |
| 477 |
KEY_ILLUMINANCE: SensorDescription( |
| 478 |
device_key=KEY_ILLUMINANCE, |
| 479 |
device_class=SensorDeviceClass.ILLUMINANCE, |
| 480 |
native_unit_of_measurement=Units.LIGHT_LUX, |
| 481 |
), |
| 482 |
KEY_SIGNAL_STRENGTH: SensorDescription( |
| 483 |
device_key=KEY_SIGNAL_STRENGTH, |
| 484 |
device_class=SensorDeviceClass.SIGNAL_STRENGTH, |
| 485 |
native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT, |
| 486 |
), |
| 487 |
}, |
| 488 |
entity_values={ |
| 489 |
KEY_ILLUMINANCE: SensorValue( |
| 490 |
device_key=KEY_ILLUMINANCE, name="Illuminance", native_value=13460.67 |
| 491 |
), |
| 492 |
KEY_SIGNAL_STRENGTH: SensorValue( |
| 493 |
device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60 |
| 494 |
), |
| 495 |
}, |
| 496 |
) |
| 497 |
|
| 498 |
|
| 499 |
def test_bthome_mass_kilograms(caplog): |
| 500 |
"""Test BTHome parser for mass reading in kilograms without encryption.""" |
| 501 |
data_string = b"\x43\x06\xfe\x70" |
| 502 |
advertisement = bytes_to_service_info( |
| 503 |
data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2" |
| 504 |
) |
| 505 |
|
| 506 |
device = BTHomeBluetoothDeviceData() |
| 507 |
assert device.update(advertisement) == SensorUpdate( |
| 508 |
title="TEST DEVICE 18B2", |
| 509 |
devices={ |
| 510 |
None: SensorDeviceInfo( |
| 511 |
name="TEST DEVICE 18B2", |
| 512 |
manufacturer=None, |
| 513 |
model="BTHome sensor", |
| 514 |
sw_version="BTHome BLE v1", |
| 515 |
hw_version=None, |
| 516 |
) |
| 517 |
}, |
| 518 |
entity_descriptions={ |
| 519 |
KEY_MASS: SensorDescription( |
| 520 |
device_key=KEY_MASS, |
| 521 |
device_class=SensorDeviceClass.MASS, |
| 522 |
native_unit_of_measurement=Units.MASS_KILOGRAMS, |
| 523 |
), |
| 524 |
KEY_SIGNAL_STRENGTH: SensorDescription( |
| 525 |
device_key=KEY_SIGNAL_STRENGTH, |
| 526 |
device_class=SensorDeviceClass.SIGNAL_STRENGTH, |
| 527 |
native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT, |
| 528 |
), |
| 529 |
}, |
| 530 |
entity_values={ |
| 531 |
KEY_MASS: SensorValue( |
| 532 |
device_key=KEY_MASS, name="Mass", native_value=102.24 |
| 533 |
), |
| 534 |
KEY_SIGNAL_STRENGTH: SensorValue( |
| 535 |
device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60 |
| 536 |
), |
| 537 |
}, |
| 538 |
) |
| 539 |
|
| 540 |
|
| 541 |
def test_bthome_mass_pounds(caplog): |
| 542 |
"""Test BTHome parser for mass reading in pounds without encryption.""" |
| 543 |
data_string = b"\x03\x07\x3e\x1d" |
| 544 |
advertisement = bytes_to_service_info( |
| 545 |
data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2" |
| 546 |
) |
| 547 |
|
| 548 |
device = BTHomeBluetoothDeviceData() |
| 549 |
assert device.update(advertisement) == SensorUpdate( |
| 550 |
title="TEST DEVICE 18B2", |
| 551 |
devices={ |
| 552 |
None: SensorDeviceInfo( |
| 553 |
name="TEST DEVICE 18B2", |
| 554 |
manufacturer=None, |
| 555 |
model="BTHome sensor", |
| 556 |
sw_version="BTHome BLE v1", |
| 557 |
hw_version=None, |
| 558 |
) |
| 559 |
}, |
| 560 |
entity_descriptions={ |
| 561 |
KEY_MASS: SensorDescription( |
| 562 |
device_key=KEY_MASS, |
| 563 |
device_class=SensorDeviceClass.MASS, |
| 564 |
native_unit_of_measurement=Units.MASS_POUNDS, |
| 565 |
), |
| 566 |
KEY_SIGNAL_STRENGTH: SensorDescription( |
| 567 |
device_key=KEY_SIGNAL_STRENGTH, |
| 568 |
device_class=SensorDeviceClass.SIGNAL_STRENGTH, |
| 569 |
native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT, |
| 570 |
), |
| 571 |
}, |
| 572 |
entity_values={ |
| 573 |
KEY_MASS: SensorValue(device_key=KEY_MASS, name="Mass", native_value=74.86), |
| 574 |
KEY_SIGNAL_STRENGTH: SensorValue( |
| 575 |
device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60 |
| 576 |
), |
| 577 |
}, |
| 578 |
) |
| 579 |
|
| 580 |
|
| 581 |
def test_bthome_dew_point(caplog): |
| 582 |
"""Test BTHome parser for dew point reading without encryption.""" |
| 583 |
data_string = b"\x23\x08\xca\x06" |
| 584 |
advertisement = bytes_to_service_info( |
| 585 |
data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2" |
| 586 |
) |
| 587 |
|
| 588 |
device = BTHomeBluetoothDeviceData() |
| 589 |
assert device.update(advertisement) == SensorUpdate( |
| 590 |
title="TEST DEVICE 18B2", |
| 591 |
devices={ |
| 592 |
None: SensorDeviceInfo( |
| 593 |
name="TEST DEVICE 18B2", |
| 594 |
manufacturer=None, |
| 595 |
model="BTHome sensor", |
| 596 |
sw_version="BTHome BLE v1", |
| 597 |
hw_version=None, |
| 598 |
) |
| 599 |
}, |
| 600 |
entity_descriptions={ |
| 601 |
KEY_DEW_POINT: SensorDescription( |
| 602 |
device_key=KEY_DEW_POINT, |
| 603 |
device_class=SensorDeviceClass.DEW_POINT, |
| 604 |
native_unit_of_measurement=Units.TEMP_CELSIUS, |
| 605 |
), |
| 606 |
KEY_SIGNAL_STRENGTH: SensorDescription( |
| 607 |
device_key=KEY_SIGNAL_STRENGTH, |
| 608 |
device_class=SensorDeviceClass.SIGNAL_STRENGTH, |
| 609 |
native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT, |
| 610 |
), |
| 611 |
}, |
| 612 |
entity_values={ |
| 613 |
KEY_DEW_POINT: SensorValue( |
| 614 |
device_key=KEY_DEW_POINT, name="Dew Point", native_value=17.38 |
| 615 |
), |
| 616 |
KEY_SIGNAL_STRENGTH: SensorValue( |
| 617 |
device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60 |
| 618 |
), |
| 619 |
}, |
| 620 |
) |
| 621 |
|
| 622 |
|
| 623 |
def test_bthome_count(caplog): |
| 624 |
"""Test BTHome parser for counter reading without encryption.""" |
| 625 |
data_string = b"\x02\x09\x60" |
| 626 |
advertisement = bytes_to_service_info( |
| 627 |
data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2" |
| 628 |
) |
| 629 |
|
| 630 |
device = BTHomeBluetoothDeviceData() |
| 631 |
assert device.update(advertisement) == SensorUpdate( |
| 632 |
title="TEST DEVICE 18B2", |
| 633 |
devices={ |
| 634 |
None: SensorDeviceInfo( |
| 635 |
name="TEST DEVICE 18B2", |
| 636 |
manufacturer=None, |
| 637 |
model="BTHome sensor", |
| 638 |
sw_version="BTHome BLE v1", |
| 639 |
hw_version=None, |
| 640 |
) |
| 641 |
}, |
| 642 |
entity_descriptions={ |
| 643 |
KEY_COUNT: SensorDescription( |
| 644 |
device_key=KEY_COUNT, |
| 645 |
device_class=SensorDeviceClass.COUNT, |
| 646 |
native_unit_of_measurement=None, |
| 647 |
), |
| 648 |
KEY_SIGNAL_STRENGTH: SensorDescription( |
| 649 |
device_key=KEY_SIGNAL_STRENGTH, |
| 650 |
device_class=SensorDeviceClass.SIGNAL_STRENGTH, |
| 651 |
native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT, |
| 652 |
), |
| 653 |
}, |
| 654 |
entity_values={ |
| 655 |
KEY_COUNT: SensorValue(device_key=KEY_COUNT, name="Count", native_value=96), |
| 656 |
KEY_SIGNAL_STRENGTH: SensorValue( |
| 657 |
device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60 |
| 658 |
), |
| 659 |
}, |
| 660 |
) |
| 661 |
|
| 662 |
|
| 663 |
def test_bthome_energy(caplog): |
| 664 |
"""Test BTHome parser for energy reading without encryption.""" |
| 665 |
data_string = b"\x04\n\x13\x8a\x14" |
| 666 |
advertisement = bytes_to_service_info( |
| 667 |
data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2" |
| 668 |
) |
| 669 |
|
| 670 |
device = BTHomeBluetoothDeviceData() |
| 671 |
assert device.update(advertisement) == SensorUpdate( |
| 672 |
title="TEST DEVICE 18B2", |
| 673 |
devices={ |
| 674 |
None: SensorDeviceInfo( |
| 675 |
name="TEST DEVICE 18B2", |
| 676 |
manufacturer=None, |
| 677 |
model="BTHome sensor", |
| 678 |
sw_version="BTHome BLE v1", |
| 679 |
hw_version=None, |
| 680 |
) |
| 681 |
}, |
| 682 |
entity_descriptions={ |
| 683 |
KEY_ENERGY: SensorDescription( |
| 684 |
device_key=KEY_ENERGY, |
| 685 |
device_class=SensorDeviceClass.ENERGY, |
| 686 |
native_unit_of_measurement=Units.ENERGY_KILO_WATT_HOUR, |
| 687 |
), |
| 688 |
KEY_SIGNAL_STRENGTH: SensorDescription( |
| 689 |
device_key=KEY_SIGNAL_STRENGTH, |
| 690 |
device_class=SensorDeviceClass.SIGNAL_STRENGTH, |
| 691 |
native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT, |
| 692 |
), |
| 693 |
}, |
| 694 |
entity_values={ |
| 695 |
KEY_ENERGY: SensorValue( |
| 696 |
device_key=KEY_ENERGY, name="Energy", native_value=1346.067 |
| 697 |
), |
| 698 |
KEY_SIGNAL_STRENGTH: SensorValue( |
| 699 |
device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60 |
| 700 |
), |
| 701 |
}, |
| 702 |
) |
| 703 |
|
| 704 |
|
| 705 |
def test_bthome_power(caplog): |
| 706 |
"""Test BTHome parser for power reading without encryption.""" |
| 707 |
data_string = b"\x04\x0b\x02\x1b\x00" |
| 708 |
advertisement = bytes_to_service_info( |
| 709 |
data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2" |
| 710 |
) |
| 711 |
|
| 712 |
device = BTHomeBluetoothDeviceData() |
| 713 |
assert device.update(advertisement) == SensorUpdate( |
| 714 |
title="TEST DEVICE 18B2", |
| 715 |
devices={ |
| 716 |
None: SensorDeviceInfo( |
| 717 |
name="TEST DEVICE 18B2", |
| 718 |
manufacturer=None, |
| 719 |
model="BTHome sensor", |
| 720 |
sw_version="BTHome BLE v1", |
| 721 |
hw_version=None, |
| 722 |
) |
| 723 |
}, |
| 724 |
entity_descriptions={ |
| 725 |
KEY_POWER: SensorDescription( |
| 726 |
device_key=KEY_POWER, |
| 727 |
device_class=SensorDeviceClass.POWER, |
| 728 |
native_unit_of_measurement=Units.POWER_WATT, |
| 729 |
), |
| 730 |
KEY_SIGNAL_STRENGTH: SensorDescription( |
| 731 |
device_key=KEY_SIGNAL_STRENGTH, |
| 732 |
device_class=SensorDeviceClass.SIGNAL_STRENGTH, |
| 733 |
native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT, |
| 734 |
), |
| 735 |
}, |
| 736 |
entity_values={ |
| 737 |
KEY_POWER: SensorValue( |
| 738 |
device_key=KEY_POWER, name="Power", native_value=69.14 |
| 739 |
), |
| 740 |
KEY_SIGNAL_STRENGTH: SensorValue( |
| 741 |
device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60 |
| 742 |
), |
| 743 |
}, |
| 744 |
) |
| 745 |
|
| 746 |
|
| 747 |
def test_bthome_voltage(caplog): |
| 748 |
"""Test BThome parser for voltage reading without encryption.""" |
| 749 |
data_string = b"\x03\x0c\x02\x0c" |
| 750 |
advertisement = bytes_to_service_info( |
| 751 |
data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2" |
| 752 |
) |
| 753 |
|
| 754 |
device = BTHomeBluetoothDeviceData() |
| 755 |
assert device.update(advertisement) == SensorUpdate( |
| 756 |
title="TEST DEVICE 18B2", |
| 757 |
devices={ |
| 758 |
None: SensorDeviceInfo( |
| 759 |
name="TEST DEVICE 18B2", |
| 760 |
manufacturer=None, |
| 761 |
model="BTHome sensor", |
| 762 |
sw_version="BTHome BLE v1", |
| 763 |
hw_version=None, |
| 764 |
) |
| 765 |
}, |
| 766 |
entity_descriptions={ |
| 767 |
KEY_VOLTAGE: SensorDescription( |
| 768 |
device_key=KEY_VOLTAGE, |
| 769 |
device_class=SensorDeviceClass.VOLTAGE, |
| 770 |
native_unit_of_measurement=Units.ELECTRIC_POTENTIAL_VOLT, |
| 771 |
), |
| 772 |
KEY_SIGNAL_STRENGTH: SensorDescription( |
| 773 |
device_key=KEY_SIGNAL_STRENGTH, |
| 774 |
device_class=SensorDeviceClass.SIGNAL_STRENGTH, |
| 775 |
native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT, |
| 776 |
), |
| 777 |
}, |
| 778 |
entity_values={ |
| 779 |
KEY_VOLTAGE: SensorValue( |
| 780 |
device_key=KEY_VOLTAGE, name="Voltage", native_value=3.074 |
| 781 |
), |
| 782 |
KEY_SIGNAL_STRENGTH: SensorValue( |
| 783 |
device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60 |
| 784 |
), |
| 785 |
}, |
| 786 |
) |
| 787 |
|
| 788 |
|
| 789 |
def test_bthome_binary_sensor(caplog): |
| 790 |
"""Test BTHome parser for binary sensor without device class, without encryption.""" |
| 791 |
data_string = b"\x02\x0f\x01" |
| 792 |
advertisement = bytes_to_service_info( |
| 793 |
data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2" |
| 794 |
) |
| 795 |
|
| 796 |
device = BTHomeBluetoothDeviceData() |
| 797 |
|
| 798 |
assert device.update(advertisement) == SensorUpdate( |
| 799 |
title="TEST DEVICE 18B2", |
| 800 |
devices={ |
| 801 |
None: SensorDeviceInfo( |
| 802 |
name="TEST DEVICE 18B2", |
| 803 |
manufacturer=None, |
| 804 |
model="BTHome sensor", |
| 805 |
sw_version="BTHome BLE v1", |
| 806 |
hw_version=None, |
| 807 |
) |
| 808 |
}, |
| 809 |
entity_descriptions={ |
| 810 |
KEY_SIGNAL_STRENGTH: SensorDescription( |
| 811 |
device_key=KEY_SIGNAL_STRENGTH, |
| 812 |
device_class=SensorDeviceClass.SIGNAL_STRENGTH, |
| 813 |
native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT, |
| 814 |
), |
| 815 |
}, |
| 816 |
entity_values={ |
| 817 |
KEY_SIGNAL_STRENGTH: SensorValue( |
| 818 |
device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60 |
| 819 |
), |
| 820 |
}, |
| 821 |
binary_entity_descriptions={ |
| 822 |
KEY_BINARY_GENERIC: BinarySensorDescription( |
| 823 |
device_key=KEY_BINARY_GENERIC, |
| 824 |
device_class=BinarySensorDeviceClass.GENERIC, |
| 825 |
), |
| 826 |
}, |
| 827 |
binary_entity_values={ |
| 828 |
KEY_BINARY_GENERIC: BinarySensorValue( |
| 829 |
device_key=KEY_BINARY_GENERIC, name="Generic", native_value=True |
| 830 |
), |
| 831 |
}, |
| 832 |
) |
| 833 |
|
| 834 |
|
| 835 |
def test_bthome_binary_sensor_power(caplog): |
| 836 |
"""Test BTHome parser for binary sensor power without encryption.""" |
| 837 |
data_string = b"\x02\x10\x01" |
| 838 |
advertisement = bytes_to_service_info( |
| 839 |
data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2" |
| 840 |
) |
| 841 |
|
| 842 |
device = BTHomeBluetoothDeviceData() |
| 843 |
|
| 844 |
assert device.update(advertisement) == SensorUpdate( |
| 845 |
title="TEST DEVICE 18B2", |
| 846 |
devices={ |
| 847 |
None: SensorDeviceInfo( |
| 848 |
name="TEST DEVICE 18B2", |
| 849 |
manufacturer=None, |
| 850 |
model="BTHome sensor", |
| 851 |
sw_version="BTHome BLE v1", |
| 852 |
hw_version=None, |
| 853 |
) |
| 854 |
}, |
| 855 |
entity_descriptions={ |
| 856 |
KEY_SIGNAL_STRENGTH: SensorDescription( |
| 857 |
device_key=KEY_SIGNAL_STRENGTH, |
| 858 |
device_class=SensorDeviceClass.SIGNAL_STRENGTH, |
| 859 |
native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT, |
| 860 |
), |
| 861 |
}, |
| 862 |
entity_values={ |
| 863 |
KEY_SIGNAL_STRENGTH: SensorValue( |
| 864 |
device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60 |
| 865 |
), |
| 866 |
}, |
| 867 |
binary_entity_descriptions={ |
| 868 |
KEY_BINARY_POWER: BinarySensorDescription( |
| 869 |
device_key=KEY_BINARY_POWER, |
| 870 |
device_class=BinarySensorDeviceClass.POWER, |
| 871 |
), |
| 872 |
}, |
| 873 |
binary_entity_values={ |
| 874 |
KEY_BINARY_POWER: BinarySensorValue( |
| 875 |
device_key=KEY_BINARY_POWER, name="Power", native_value=True |
| 876 |
), |
| 877 |
}, |
| 878 |
) |
| 879 |
|
| 880 |
|
| 881 |
def test_bthome_binary_sensor_opening(caplog): |
| 882 |
"""Test BTHome parser for binary sensor opening without encryption.""" |
| 883 |
data_string = b"\x02\x11\x00" |
| 884 |
advertisement = bytes_to_service_info( |
| 885 |
data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2" |
| 886 |
) |
| 887 |
|
| 888 |
device = BTHomeBluetoothDeviceData() |
| 889 |
|
| 890 |
assert device.update(advertisement) == SensorUpdate( |
| 891 |
title="TEST DEVICE 18B2", |
| 892 |
devices={ |
| 893 |
None: SensorDeviceInfo( |
| 894 |
name="TEST DEVICE 18B2", |
| 895 |
manufacturer=None, |
| 896 |
model="BTHome sensor", |
| 897 |
sw_version="BTHome BLE v1", |
| 898 |
hw_version=None, |
| 899 |
) |
| 900 |
}, |
| 901 |
entity_descriptions={ |
| 902 |
KEY_SIGNAL_STRENGTH: SensorDescription( |
| 903 |
device_key=KEY_SIGNAL_STRENGTH, |
| 904 |
device_class=SensorDeviceClass.SIGNAL_STRENGTH, |
| 905 |
native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT, |
| 906 |
), |
| 907 |
}, |
| 908 |
entity_values={ |
| 909 |
KEY_SIGNAL_STRENGTH: SensorValue( |
| 910 |
device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60 |
| 911 |
), |
| 912 |
}, |
| 913 |
binary_entity_descriptions={ |
| 914 |
KEY_BINARY_OPENING: BinarySensorDescription( |
| 915 |
device_key=KEY_BINARY_OPENING, |
| 916 |
device_class=BinarySensorDeviceClass.OPENING, |
| 917 |
), |
| 918 |
}, |
| 919 |
binary_entity_values={ |
| 920 |
KEY_BINARY_OPENING: BinarySensorValue( |
| 921 |
device_key=KEY_BINARY_OPENING, name="Opening", native_value=False |
| 922 |
), |
| 923 |
}, |
| 924 |
) |
| 925 |
|
| 926 |
|
| 927 |
def test_bthome_pm(caplog): |
| 928 |
"""Test BTHome parser for PM2.5 and PM10 reading without encryption.""" |
| 929 |
data_string = b"\x03\r\x12\x0c\x03\x0e\x02\x1c" |
| 930 |
advertisement = bytes_to_service_info( |
| 931 |
data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2" |
| 932 |
) |
| 933 |
|
| 934 |
device = BTHomeBluetoothDeviceData() |
| 935 |
assert device.update(advertisement) == SensorUpdate( |
| 936 |
title="TEST DEVICE 18B2", |
| 937 |
devices={ |
| 938 |
None: SensorDeviceInfo( |
| 939 |
name="TEST DEVICE 18B2", |
| 940 |
manufacturer=None, |
| 941 |
model="BTHome sensor", |
| 942 |
sw_version="BTHome BLE v1", |
| 943 |
hw_version=None, |
| 944 |
) |
| 945 |
}, |
| 946 |
entity_descriptions={ |
| 947 |
KEY_PM25: SensorDescription( |
| 948 |
device_key=KEY_PM25, |
| 949 |
device_class=SensorDeviceClass.PM25, |
| 950 |
native_unit_of_measurement=( |
| 951 |
Units.CONCENTRATION_MICROGRAMS_PER_CUBIC_METER |
| 952 |
), |
| 953 |
), |
| 954 |
KEY_PM10: SensorDescription( |
| 955 |
device_key=KEY_PM10, |
| 956 |
device_class=SensorDeviceClass.PM10, |
| 957 |
native_unit_of_measurement=( |
| 958 |
Units.CONCENTRATION_MICROGRAMS_PER_CUBIC_METER |
| 959 |
), |
| 960 |
), |
| 961 |
KEY_SIGNAL_STRENGTH: SensorDescription( |
| 962 |
device_key=KEY_SIGNAL_STRENGTH, |
| 963 |
device_class=SensorDeviceClass.SIGNAL_STRENGTH, |
| 964 |
native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT, |
| 965 |
), |
| 966 |
}, |
| 967 |
entity_values={ |
| 968 |
KEY_PM25: SensorValue(device_key=KEY_PM25, name="Pm25", native_value=3090), |
| 969 |
KEY_PM10: SensorValue(device_key=KEY_PM10, name="Pm10", native_value=7170), |
| 970 |
KEY_SIGNAL_STRENGTH: SensorValue( |
| 971 |
device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60 |
| 972 |
), |
| 973 |
}, |
| 974 |
) |
| 975 |
|
| 976 |
|
| 977 |
def test_bthome_co2(caplog): |
| 978 |
"""Test BTHome parser for CO2 reading without encryption.""" |
| 979 |
data_string = b"\x03\x12\xe2\x04" |
| 980 |
advertisement = bytes_to_service_info( |
| 981 |
data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2" |
| 982 |
) |
| 983 |
|
| 984 |
device = BTHomeBluetoothDeviceData() |
| 985 |
assert device.update(advertisement) == SensorUpdate( |
| 986 |
title="TEST DEVICE 18B2", |
| 987 |
devices={ |
| 988 |
None: SensorDeviceInfo( |
| 989 |
name="TEST DEVICE 18B2", |
| 990 |
manufacturer=None, |
| 991 |
model="BTHome sensor", |
| 992 |
sw_version="BTHome BLE v1", |
| 993 |
hw_version=None, |
| 994 |
) |
| 995 |
}, |
| 996 |
entity_descriptions={ |
| 997 |
KEY_CO2: SensorDescription( |
| 998 |
device_key=KEY_CO2, |
| 999 |
device_class=SensorDeviceClass.CO2, |
| 1000 |
native_unit_of_measurement=Units.CONCENTRATION_PARTS_PER_MILLION, |
| 1001 |
), |
| 1002 |
KEY_SIGNAL_STRENGTH: SensorDescription( |
| 1003 |
device_key=KEY_SIGNAL_STRENGTH, |
| 1004 |
device_class=SensorDeviceClass.SIGNAL_STRENGTH, |
| 1005 |
native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT, |
| 1006 |
), |
| 1007 |
}, |
| 1008 |
entity_values={ |
| 1009 |
KEY_CO2: SensorValue( |
| 1010 |
device_key=KEY_CO2, name="Carbon Dioxide", native_value=1250 |
| 1011 |
), |
| 1012 |
KEY_SIGNAL_STRENGTH: SensorValue( |
| 1013 |
device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60 |
| 1014 |
), |
| 1015 |
}, |
| 1016 |
) |
| 1017 |
|
| 1018 |
|
| 1019 |
def test_bthome_timestamp(caplog): |
| 1020 |
"""Test BTHome parser for Unix timestamp (seconds from 1-1-1970).""" |
| 1021 |
data_string = b"\xa5\x50\x5d\x39\x61\x64" |
| 1022 |
advertisement = bytes_to_service_info( |
| 1023 |
data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2" |
| 1024 |
) |
| 1025 |
|
| 1026 |
device = BTHomeBluetoothDeviceData() |
| 1027 |
|
| 1028 |
assert device.update(advertisement) == SensorUpdate( |
| 1029 |
title="TEST DEVICE 18B2", |
| 1030 |
devices={ |
| 1031 |
None: SensorDeviceInfo( |
| 1032 |
name="TEST DEVICE 18B2", |
| 1033 |
manufacturer=None, |
| 1034 |
model="BTHome sensor", |
| 1035 |
sw_version="BTHome BLE v1", |
| 1036 |
hw_version=None, |
| 1037 |
) |
| 1038 |
}, |
| 1039 |
entity_descriptions={ |
| 1040 |
KEY_TIMESTAMP: SensorDescription( |
| 1041 |
device_key=KEY_TIMESTAMP, |
| 1042 |
device_class=SensorDeviceClass.TIMESTAMP, |
| 1043 |
native_unit_of_measurement=None, |
| 1044 |
), |
| 1045 |
KEY_SIGNAL_STRENGTH: SensorDescription( |
| 1046 |
device_key=KEY_SIGNAL_STRENGTH, |
| 1047 |
device_class=SensorDeviceClass.SIGNAL_STRENGTH, |
| 1048 |
native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT, |
| 1049 |
), |
| 1050 |
}, |
| 1051 |
entity_values={ |
| 1052 |
KEY_TIMESTAMP: SensorValue( |
| 1053 |
device_key=KEY_TIMESTAMP, |
| 1054 |
name="Timestamp", |
| 1055 |
native_value=datetime(2023, 5, 14, 19, 41, 17, tzinfo=UTC), |
| 1056 |
), |
| 1057 |
KEY_SIGNAL_STRENGTH: SensorValue( |
| 1058 |
device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60 |
| 1059 |
), |
| 1060 |
}, |
| 1061 |
) |
| 1062 |
|
| 1063 |
|
| 1064 |
def test_bthome_voc(caplog): |
| 1065 |
"""Test BTHome parser for VOC reading without encryption.""" |
| 1066 |
data_string = b"\x03\x133\x01" |
| 1067 |
advertisement = bytes_to_service_info( |
| 1068 |
data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2" |
| 1069 |
) |
| 1070 |
|
| 1071 |
device = BTHomeBluetoothDeviceData() |
| 1072 |
assert device.update(advertisement) == SensorUpdate( |
| 1073 |
title="TEST DEVICE 18B2", |
| 1074 |
devices={ |
| 1075 |
None: SensorDeviceInfo( |
| 1076 |
name="TEST DEVICE 18B2", |
| 1077 |
manufacturer=None, |
| 1078 |
model="BTHome sensor", |
| 1079 |
sw_version="BTHome BLE v1", |
| 1080 |
hw_version=None, |
| 1081 |
) |
| 1082 |
}, |
| 1083 |
entity_descriptions={ |
| 1084 |
KEY_VOC: SensorDescription( |
| 1085 |
device_key=KEY_VOC, |
| 1086 |
device_class=SensorDeviceClass.VOLATILE_ORGANIC_COMPOUNDS, |
| 1087 |
native_unit_of_measurement=Units.CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, |
| 1088 |
), |
| 1089 |
KEY_SIGNAL_STRENGTH: SensorDescription( |
| 1090 |
device_key=KEY_SIGNAL_STRENGTH, |
| 1091 |
device_class=SensorDeviceClass.SIGNAL_STRENGTH, |
| 1092 |
native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT, |
| 1093 |
), |
| 1094 |
}, |
| 1095 |
entity_values={ |
| 1096 |
KEY_VOC: SensorValue( |
| 1097 |
device_key=KEY_VOC, name="Volatile Organic Compounds", native_value=307 |
| 1098 |
), |
| 1099 |
KEY_SIGNAL_STRENGTH: SensorValue( |
| 1100 |
device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60 |
| 1101 |
), |
| 1102 |
}, |
| 1103 |
) |
| 1104 |
|
| 1105 |
|
| 1106 |
def test_bthome_moisture(caplog): |
| 1107 |
"""Test BTHome parser for moisture reading from b-parasite sensor.""" |
| 1108 |
data_string = b"\x03\x14\x02\x0c" |
| 1109 |
advertisement = bytes_to_service_info( |
| 1110 |
data_string, local_name="prst", address="A4:C1:38:8D:18:B2" |
| 1111 |
) |
| 1112 |
|
| 1113 |
device = BTHomeBluetoothDeviceData() |
| 1114 |
assert device.update(advertisement) == SensorUpdate( |
| 1115 |
title="b-parasite 18B2", |
| 1116 |
devices={ |
| 1117 |
None: SensorDeviceInfo( |
| 1118 |
name="b-parasite 18B2", |
| 1119 |
manufacturer="b-parasite", |
| 1120 |
model="Plant sensor", |
| 1121 |
sw_version="BTHome BLE v1", |
| 1122 |
hw_version=None, |
| 1123 |
) |
| 1124 |
}, |
| 1125 |
entity_descriptions={ |
| 1126 |
KEY_MOISTURE: SensorDescription( |
| 1127 |
device_key=KEY_MOISTURE, |
| 1128 |
device_class=SensorDeviceClass.MOISTURE, |
| 1129 |
native_unit_of_measurement=Units.PERCENTAGE, |
| 1130 |
), |
| 1131 |
KEY_SIGNAL_STRENGTH: SensorDescription( |
| 1132 |
device_key=KEY_SIGNAL_STRENGTH, |
| 1133 |
device_class=SensorDeviceClass.SIGNAL_STRENGTH, |
| 1134 |
native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT, |
| 1135 |
), |
| 1136 |
}, |
| 1137 |
entity_values={ |
| 1138 |
KEY_MOISTURE: SensorValue( |
| 1139 |
device_key=KEY_MOISTURE, name="Moisture", native_value=30.74 |
| 1140 |
), |
| 1141 |
KEY_SIGNAL_STRENGTH: SensorValue( |
| 1142 |
device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60 |
| 1143 |
), |
| 1144 |
}, |
| 1145 |
) |
| 1146 |
|
| 1147 |
|
| 1148 |
def test_bthome_event_button_long_press(caplog): |
| 1149 |
"""Test BTHome parser for an event of a long press on a button without encryption.""" |
| 1150 |
data_string = b"\x02\x3a\x04" |
| 1151 |
advertisement = bytes_to_service_info( |
| 1152 |
data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2" |
| 1153 |
) |
| 1154 |
|
| 1155 |
device = BTHomeBluetoothDeviceData() |
| 1156 |
|
| 1157 |
assert device.update(advertisement) == SensorUpdate( |
| 1158 |
title="TEST DEVICE 18B2", |
| 1159 |
devices={ |
| 1160 |
None: SensorDeviceInfo( |
| 1161 |
name="TEST DEVICE 18B2", |
| 1162 |
manufacturer=None, |
| 1163 |
model="BTHome sensor", |
| 1164 |
sw_version="BTHome BLE v1", |
| 1165 |
hw_version=None, |
| 1166 |
) |
| 1167 |
}, |
| 1168 |
entity_descriptions={ |
| 1169 |
KEY_SIGNAL_STRENGTH: SensorDescription( |
| 1170 |
device_key=KEY_SIGNAL_STRENGTH, |
| 1171 |
device_class=SensorDeviceClass.SIGNAL_STRENGTH, |
| 1172 |
native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT, |
| 1173 |
), |
| 1174 |
}, |
| 1175 |
entity_values={ |
| 1176 |
KEY_SIGNAL_STRENGTH: SensorValue( |
| 1177 |
device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60 |
| 1178 |
), |
| 1179 |
}, |
| 1180 |
events={ |
| 1181 |
KEY_BUTTON: Event( |
| 1182 |
device_key=KEY_BUTTON, |
| 1183 |
name="Button", |
| 1184 |
event_type="long_press", |
| 1185 |
event_properties=None, |
| 1186 |
), |
| 1187 |
}, |
| 1188 |
) |
| 1189 |
|
| 1190 |
|
| 1191 |
def test_bthome_event_dimmer_rotate_left_3_steps(caplog): |
| 1192 |
"""Test BTHome parser for an event rotating a dimmer 3 steps left.""" |
| 1193 |
data_string = b"\x03\x3c\x01\x03" |
| 1194 |
advertisement = bytes_to_service_info( |
| 1195 |
data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2" |
| 1196 |
) |
| 1197 |
|
| 1198 |
device = BTHomeBluetoothDeviceData() |
| 1199 |
|
| 1200 |
assert device.update(advertisement) == SensorUpdate( |
| 1201 |
title="TEST DEVICE 18B2", |
| 1202 |
devices={ |
| 1203 |
None: SensorDeviceInfo( |
| 1204 |
name="TEST DEVICE 18B2", |
| 1205 |
manufacturer=None, |
| 1206 |
model="BTHome sensor", |
| 1207 |
sw_version="BTHome BLE v1", |
| 1208 |
hw_version=None, |
| 1209 |
) |
| 1210 |
}, |
| 1211 |
entity_descriptions={ |
| 1212 |
KEY_SIGNAL_STRENGTH: SensorDescription( |
| 1213 |
device_key=KEY_SIGNAL_STRENGTH, |
| 1214 |
device_class=SensorDeviceClass.SIGNAL_STRENGTH, |
| 1215 |
native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT, |
| 1216 |
), |
| 1217 |
}, |
| 1218 |
entity_values={ |
| 1219 |
KEY_SIGNAL_STRENGTH: SensorValue( |
| 1220 |
device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60 |
| 1221 |
), |
| 1222 |
}, |
| 1223 |
events={ |
| 1224 |
KEY_DIMMER: Event( |
| 1225 |
device_key=KEY_DIMMER, |
| 1226 |
name="Dimmer", |
| 1227 |
event_type="rotate_left", |
| 1228 |
event_properties={"steps": 3}, |
| 1229 |
), |
| 1230 |
}, |
| 1231 |
) |
| 1232 |
|
| 1233 |
|
| 1234 |
def test_bthome_event_dimmer_rotate_right_high_steps(caplog): |
| 1235 |
"""Steps is an unsigned byte; values >127 must not wrap to negative.""" |
| 1236 |
|
| 1237 |
data_string = b"\x03\x3c\x02\xc8" |
| 1238 |
advertisement = bytes_to_service_info( |
| 1239 |
data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2" |
| 1240 |
) |
| 1241 |
|
| 1242 |
device = BTHomeBluetoothDeviceData() |
| 1243 |
|
| 1244 |
assert device.update(advertisement) == SensorUpdate( |
| 1245 |
title="TEST DEVICE 18B2", |
| 1246 |
devices={ |
| 1247 |
None: SensorDeviceInfo( |
| 1248 |
name="TEST DEVICE 18B2", |
| 1249 |
manufacturer=None, |
| 1250 |
model="BTHome sensor", |
| 1251 |
sw_version="BTHome BLE v1", |
| 1252 |
hw_version=None, |
| 1253 |
) |
| 1254 |
}, |
| 1255 |
entity_descriptions={ |
| 1256 |
KEY_SIGNAL_STRENGTH: SensorDescription( |
| 1257 |
device_key=KEY_SIGNAL_STRENGTH, |
| 1258 |
device_class=SensorDeviceClass.SIGNAL_STRENGTH, |
| 1259 |
native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT, |
| 1260 |
), |
| 1261 |
}, |
| 1262 |
entity_values={ |
| 1263 |
KEY_SIGNAL_STRENGTH: SensorValue( |
| 1264 |
device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60 |
| 1265 |
), |
| 1266 |
}, |
| 1267 |
events={ |
| 1268 |
KEY_DIMMER: Event( |
| 1269 |
device_key=KEY_DIMMER, |
| 1270 |
name="Dimmer", |
| 1271 |
event_type="rotate_right", |
| 1272 |
event_properties={"steps": 200}, |
| 1273 |
), |
| 1274 |
}, |
| 1275 |
) |
| 1276 |
|
| 1277 |
|
| 1278 |
def test_bthome_multiple_uuids(caplog): |
| 1279 |
"""Test BTHome parser for a device that broadcasts multiple uuids.""" |
| 1280 |
advertisement = BluetoothServiceInfoBleak( |
| 1281 |
name="ATC_8D18B2", |
| 1282 |
address="A4:C1:38:8D:18:B2", |
| 1283 |
rssi=-60, |
| 1284 |
manufacturer_data={}, |
| 1285 |
service_data={ |
| 1286 |
"0000181a-0000-1000-8000-00805f9b34fb": b"\xc4$\x818\xc1\xa4V\x08\x83\x18\xbf", |
| 1287 |
"0000fe95-0000-1000-8000-00805f9b34fb": b"0X[\x05\x01\xc4$\x818\xc1\xa4(\x01\x00", |
| 1288 |
"0000181c-0000-1000-8000-00805f9b34fb": b"\x02\x00\xb4\x02\x10\x00\x03\x0c\xbe\x0b", |
| 1289 |
}, |
| 1290 |
service_uuids=[ |
| 1291 |
"0000181a-0000-1000-8000-00805f9b34fb", |
| 1292 |
"0000fe95-0000-1000-8000-00805f9b34fb", |
| 1293 |
"0000181c-0000-1000-8000-00805f9b34fb", |
| 1294 |
], |
| 1295 |
source="", |
| 1296 |
device=None, |
| 1297 |
advertisement=None, |
| 1298 |
connectable=False, |
| 1299 |
time=ADVERTISEMENT_TIME, |
| 1300 |
tx_power=None, |
| 1301 |
) |
| 1302 |
|
| 1303 |
device = BTHomeBluetoothDeviceData() |
| 1304 |
|
| 1305 |
assert device.update(advertisement) == SensorUpdate( |
| 1306 |
title="ATC 18B2", |
| 1307 |
devices={ |
| 1308 |
None: SensorDeviceInfo( |
| 1309 |
name="ATC 18B2", |
| 1310 |
manufacturer="Xiaomi", |
| 1311 |
model="Temperature/Humidity sensor", |
| 1312 |
sw_version="BTHome BLE v1", |
| 1313 |
hw_version=None, |
| 1314 |
) |
| 1315 |
}, |
| 1316 |
entity_descriptions={ |
| 1317 |
KEY_PACKET_ID: SensorDescription( |
| 1318 |
device_key=KEY_PACKET_ID, |
| 1319 |
device_class=SensorDeviceClass.PACKET_ID, |
| 1320 |
native_unit_of_measurement=None, |
| 1321 |
), |
| 1322 |
KEY_VOLTAGE: SensorDescription( |
| 1323 |
device_key=KEY_VOLTAGE, |
| 1324 |
device_class=SensorDeviceClass.VOLTAGE, |
| 1325 |
native_unit_of_measurement=Units.ELECTRIC_POTENTIAL_VOLT, |
| 1326 |
), |
| 1327 |
KEY_SIGNAL_STRENGTH: SensorDescription( |
| 1328 |
device_key=KEY_SIGNAL_STRENGTH, |
| 1329 |
device_class=SensorDeviceClass.SIGNAL_STRENGTH, |
| 1330 |
native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT, |
| 1331 |
), |
| 1332 |
}, |
| 1333 |
entity_values={ |
| 1334 |
KEY_PACKET_ID: SensorValue( |
| 1335 |
device_key=KEY_PACKET_ID, name="Packet Id", native_value=180 |
| 1336 |
), |
| 1337 |
KEY_VOLTAGE: SensorValue( |
| 1338 |
device_key=KEY_VOLTAGE, name="Voltage", native_value=3.006 |
| 1339 |
), |
| 1340 |
KEY_SIGNAL_STRENGTH: SensorValue( |
| 1341 |
device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60 |
| 1342 |
), |
| 1343 |
}, |
| 1344 |
binary_entity_descriptions={ |
| 1345 |
DeviceKey(key="power", device_id=None): BinarySensorDescription( |
| 1346 |
device_key=DeviceKey(key="power", device_id=None), |
| 1347 |
device_class=BinarySensorDeviceClass.POWER, |
| 1348 |
) |
| 1349 |
}, |
| 1350 |
binary_entity_values={ |
| 1351 |
DeviceKey(key="power", device_id=None): BinarySensorValue( |
| 1352 |
device_key=DeviceKey(key="power", device_id=None), |
| 1353 |
name="Power", |
| 1354 |
native_value=False, |
| 1355 |
) |
| 1356 |
}, |
| 1357 |
) |
| 1358 |
|
| 1359 |
|
| 1360 |
def test_truncated_v1_payload_missing_meas_type() -> None: |
| 1361 |
"""Regression: V1 payload truncated to control byte only must not crash.""" |
| 1362 |
device = BTHomeBluetoothDeviceData() |
| 1363 |
device.set_title("test") |
| 1364 |
device.bthome_version = BTHomeVersion.V1 |
| 1365 |
|
| 1366 |
assert device._parse_payload(b"\x02", 0.0) is False |