| 1 |
from unittest.mock import ANY, MagicMock |
| 2 |
|
| 3 |
import pytest |
| 4 |
from bleak.backends.device import BLEDevice |
| 5 |
from bleak.backends.scanner import AdvertisementData |
| 6 |
|
| 7 |
from habluetooth import ( |
| 8 |
BaseHaRemoteScanner, |
| 9 |
BaseHaScanner, |
| 10 |
BluetoothScanningMode, |
| 11 |
HaBluetoothConnector, |
| 12 |
HaScanner, |
| 13 |
get_manager, |
| 14 |
) |
| 15 |
from habluetooth.models import BluetoothServiceInfoBleak |
| 16 |
|
| 17 |
|
| 18 |
class MockBleakClient: |
| 19 |
pass |
| 20 |
|
| 21 |
|
| 22 |
def test_create_scanner(): |
| 23 |
connector = HaBluetoothConnector(MockBleakClient, "any", lambda: True) |
| 24 |
|
| 25 |
class MockScanner(BaseHaScanner): |
| 26 |
@property |
| 27 |
def discovered_devices_and_advertisement_data(self): |
| 28 |
return [] |
| 29 |
|
| 30 |
@property |
| 31 |
def discovered_devices(self): |
| 32 |
return [] |
| 33 |
|
| 34 |
scanner = MockScanner("any", "any", connector) |
| 35 |
assert isinstance(scanner, BaseHaScanner) |
| 36 |
|
| 37 |
|
| 38 |
def test_create_remote_scanner(): |
| 39 |
connector = HaBluetoothConnector(MockBleakClient, "any", lambda: True) |
| 40 |
|
| 41 |
scanner = BaseHaRemoteScanner("any", "any", connector, True) |
| 42 |
assert isinstance(scanner, BaseHaRemoteScanner) |
| 43 |
|
| 44 |
|
| 45 |
def test__async_on_advertisement(): |
| 46 |
connector = HaBluetoothConnector(MockBleakClient, "any", lambda: True) |
| 47 |
|
| 48 |
scanner = BaseHaRemoteScanner("any", "any", connector, True) |
| 49 |
details = scanner._details | {} |
| 50 |
scanner._async_on_advertisement( |
| 51 |
"AA:BB:CC:DD:EE:FF", |
| 52 |
-88, |
| 53 |
"name", |
| 54 |
["service_uuid"], |
| 55 |
{"service_uuid": b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b"}, |
| 56 |
{32: b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b"}, |
| 57 |
-88, |
| 58 |
details, |
| 59 |
1.0, |
| 60 |
) |
| 61 |
scanner._async_on_advertisement( |
| 62 |
"AA:BB:CC:DD:EE:FF", |
| 63 |
-21, |
| 64 |
"name", |
| 65 |
["service_uuid2"], |
| 66 |
{"service_uuid2": b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b"}, |
| 67 |
{21: b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b"}, |
| 68 |
-88, |
| 69 |
details, |
| 70 |
1.0, |
| 71 |
) |
| 72 |
ble_device = BLEDevice( |
| 73 |
"AA:BB:CC:DD:EE:FF", |
| 74 |
"name", |
| 75 |
details, |
| 76 |
) |
| 77 |
first_device = scanner.discovered_devices[0] |
| 78 |
assert first_device.address == ble_device.address |
| 79 |
assert first_device.details == ble_device.details |
| 80 |
assert first_device.name == ble_device.name |
| 81 |
assert "AA:BB:CC:DD:EE:FF" in scanner.discovered_devices_and_advertisement_data |
| 82 |
adv = scanner.discovered_devices_and_advertisement_data["AA:BB:CC:DD:EE:FF"][1] |
| 83 |
assert set(adv.service_data) == {"service_uuid", "service_uuid2"} |
| 84 |
assert adv == AdvertisementData( |
| 85 |
local_name="name", |
| 86 |
manufacturer_data={ |
| 87 |
32: b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b", |
| 88 |
21: b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b", |
| 89 |
}, |
| 90 |
service_data={ |
| 91 |
"service_uuid": b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b", |
| 92 |
"service_uuid2": b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b", |
| 93 |
}, |
| 94 |
service_uuids=ANY, |
| 95 |
tx_power=-88, |
| 96 |
rssi=-21, |
| 97 |
platform_data=(), |
| 98 |
) |
| 99 |
assert len(scanner.discovered_devices) == 1 |
| 100 |
assert scanner.discovered_devices[0].address == "AA:BB:CC:DD:EE:FF" |
| 101 |
assert len(scanner.discovered_devices_and_advertisement_data) == 1 |
| 102 |
# BLEDevice no longer has rssi attribute in bleak 1.0+ |
| 103 |
# rssi is only available in AdvertisementData |
| 104 |
assert ( |
| 105 |
scanner.discovered_devices_and_advertisement_data["AA:BB:CC:DD:EE:FF"][1].rssi |
| 106 |
== -21 |
| 107 |
) |
| 108 |
assert "AA:BB:CC:DD:EE:FF" in scanner.discovered_addresses |
| 109 |
device_adv = scanner.get_discovered_device_advertisement_data("AA:BB:CC:DD:EE:FF") |
| 110 |
assert device_adv is not None |
| 111 |
assert device_adv[1] == adv |
| 112 |
|
| 113 |
|
| 114 |
def test__async_on_advertisement_first(): |
| 115 |
connector = HaBluetoothConnector(MockBleakClient, "any", lambda: True) |
| 116 |
|
| 117 |
scanner = BaseHaRemoteScanner("any", "any", connector, True) |
| 118 |
details = scanner._details | {} |
| 119 |
scanner._async_on_advertisement( |
| 120 |
"AA:BB:CC:DD:EE:FF", |
| 121 |
-88, |
| 122 |
"name", |
| 123 |
["service_uuid"], |
| 124 |
{"service_uuid": b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b"}, |
| 125 |
{32: b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b"}, |
| 126 |
-88, |
| 127 |
details, |
| 128 |
1.0, |
| 129 |
) |
| 130 |
device_adv = scanner.get_discovered_device_advertisement_data("AA:BB:CC:DD:EE:FF") |
| 131 |
assert device_adv is not None |
| 132 |
device, adv = device_adv |
| 133 |
assert device is not None |
| 134 |
assert adv is not None |
| 135 |
assert device.address == "AA:BB:CC:DD:EE:FF" |
| 136 |
assert adv.rssi == -88 |
| 137 |
assert adv.service_uuids == ["service_uuid"] |
| 138 |
assert adv.service_data == { |
| 139 |
"service_uuid": b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b" |
| 140 |
} |
| 141 |
assert adv.manufacturer_data == { |
| 142 |
32: b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b" |
| 143 |
} |
| 144 |
assert adv.service_uuids == ANY |
| 145 |
assert adv.tx_power == -88 |
| 146 |
assert adv.rssi == -88 |
| 147 |
assert adv.platform_data == () |
| 148 |
assert device.name == "name" |
| 149 |
assert device.details == details |
| 150 |
|
| 151 |
|
| 152 |
def test__async_on_advertisement_prefers_longest_local_name(): |
| 153 |
connector = HaBluetoothConnector(MockBleakClient, "any", lambda: True) |
| 154 |
|
| 155 |
scanner = BaseHaRemoteScanner("any", "any", connector, True) |
| 156 |
details = scanner._details | {} |
| 157 |
scanner._async_on_advertisement( |
| 158 |
"AA:BB:CC:DD:EE:FF", |
| 159 |
-88, |
| 160 |
"shortname", |
| 161 |
[], |
| 162 |
{}, |
| 163 |
{}, |
| 164 |
-88, |
| 165 |
details, |
| 166 |
1.0, |
| 167 |
) |
| 168 |
device_adv = scanner.get_discovered_device_advertisement_data("AA:BB:CC:DD:EE:FF") |
| 169 |
assert device_adv is not None |
| 170 |
device, adv = device_adv |
| 171 |
assert device is not None |
| 172 |
assert adv is not None |
| 173 |
assert device.name == "shortname" |
| 174 |
assert adv.local_name == "shortname" |
| 175 |
scanner._async_on_advertisement( |
| 176 |
"AA:BB:CC:DD:EE:FF", |
| 177 |
-88, |
| 178 |
"tinyname", |
| 179 |
[], |
| 180 |
{}, |
| 181 |
{}, |
| 182 |
-88, |
| 183 |
details, |
| 184 |
1.0, |
| 185 |
) |
| 186 |
device_adv = scanner.get_discovered_device_advertisement_data("AA:BB:CC:DD:EE:FF") |
| 187 |
assert device_adv is not None |
| 188 |
device, adv = device_adv |
| 189 |
assert device is not None |
| 190 |
assert adv is not None |
| 191 |
assert device.name == "shortname" |
| 192 |
assert adv.local_name == "shortname" |
| 193 |
scanner._async_on_advertisement( |
| 194 |
"AA:BB:CC:DD:EE:FF", |
| 195 |
-88, |
| 196 |
"longername", |
| 197 |
[], |
| 198 |
{}, |
| 199 |
{}, |
| 200 |
-88, |
| 201 |
details, |
| 202 |
1.0, |
| 203 |
) |
| 204 |
device_adv = scanner.get_discovered_device_advertisement_data("AA:BB:CC:DD:EE:FF") |
| 205 |
assert device_adv is not None |
| 206 |
device, adv = device_adv |
| 207 |
assert device is not None |
| 208 |
assert adv is not None |
| 209 |
assert device.name == "longername" |
| 210 |
assert adv.local_name == "longername" |
| 211 |
|
| 212 |
|
| 213 |
def test_create_ha_scanner(): |
| 214 |
scanner = HaScanner(BluetoothScanningMode.ACTIVE, "hci0", "AA:BB:CC:DD:EE:FF") |
| 215 |
assert isinstance(scanner, HaScanner) |
| 216 |
|
| 217 |
|
| 218 |
@pytest.mark.asyncio |
| 219 |
async def test_dedup_unchanged_same_source(): |
| 220 |
"""Test that unchanged data from the same source is deduped (skipped).""" |
| 221 |
manager = get_manager() |
| 222 |
connector = HaBluetoothConnector(MockBleakClient, "any", lambda: True) |
| 223 |
scanner = BaseHaRemoteScanner("source1", "source1", connector, True) |
| 224 |
cancel = manager.async_register_scanner(scanner) |
| 225 |
details = scanner._details | {} |
| 226 |
|
| 227 |
# First advertisement — seeds _all_history |
| 228 |
scanner._async_on_advertisement( |
| 229 |
"AA:BB:CC:DD:EE:FF", |
| 230 |
-88, |
| 231 |
"name", |
| 232 |
["service_uuid"], |
| 233 |
{"service_uuid": b"\x01"}, |
| 234 |
{1: b"\x01"}, |
| 235 |
-88, |
| 236 |
details, |
| 237 |
1.0, |
| 238 |
) |
| 239 |
mock_discover = MagicMock() |
| 240 |
manager._subclass_discover_info = mock_discover |
| 241 |
|
| 242 |
# Second identical advertisement — same source, so dedup should skip dispatch |
| 243 |
scanner._async_on_advertisement( |
| 244 |
"AA:BB:CC:DD:EE:FF", |
| 245 |
-88, |
| 246 |
"name", |
| 247 |
["service_uuid"], |
| 248 |
{"service_uuid": b"\x01"}, |
| 249 |
{1: b"\x01"}, |
| 250 |
-88, |
| 251 |
details, |
| 252 |
2.0, |
| 253 |
) |
| 254 |
# Dedup should have returned early — _subclass_discover_info not called |
| 255 |
mock_discover.assert_not_called() |
| 256 |
|
| 257 |
cancel() |
| 258 |
|
| 259 |
|
| 260 |
@pytest.mark.asyncio |
| 261 |
async def test_dedup_unchanged_different_source(): |
| 262 |
"""Test unchanged data from a different source dispatches when data changes.""" |
| 263 |
manager = get_manager() |
| 264 |
connector = HaBluetoothConnector(MockBleakClient, "any", lambda: True) |
| 265 |
|
| 266 |
scanner1 = BaseHaRemoteScanner("source1", "source1", connector, True) |
| 267 |
cancel1 = manager.async_register_scanner(scanner1) |
| 268 |
|
| 269 |
scanner2 = BaseHaRemoteScanner("source2", "source2", connector, True) |
| 270 |
cancel2 = manager.async_register_scanner(scanner2) |
| 271 |
|
| 272 |
details: dict[str, str] = {} |
| 273 |
|
| 274 |
# Scanner 1 sends advertisement — seeds _all_history with source1 |
| 275 |
scanner1._async_on_advertisement( |
| 276 |
"AA:BB:CC:DD:EE:FF", |
| 277 |
-50, |
| 278 |
"name", |
| 279 |
["svc"], |
| 280 |
{"svc": b"\x01"}, |
| 281 |
{1: b"\x01"}, |
| 282 |
-88, |
| 283 |
details, |
| 284 |
1.0, |
| 285 |
) |
| 286 |
|
| 287 |
# Scanner 2 sends first adv — seeds scanner2's _previous_service_info. |
| 288 |
# _all_history switches to source2 (stale time diff). |
| 289 |
scanner2._async_on_advertisement( |
| 290 |
"AA:BB:CC:DD:EE:FF", |
| 291 |
-40, |
| 292 |
"name", |
| 293 |
["svc"], |
| 294 |
{"svc": b"\x01"}, |
| 295 |
{1: b"\x01"}, |
| 296 |
-88, |
| 297 |
details, |
| 298 |
1000.0, |
| 299 |
) |
| 300 |
assert manager._all_history["AA:BB:CC:DD:EE:FF"].source == "source2" |
| 301 |
|
| 302 |
# Scanner 1 sends again — seeds scanner1's _previous_service_info with |
| 303 |
# same data. _all_history now has source2. |
| 304 |
scanner1._async_on_advertisement( |
| 305 |
"AA:BB:CC:DD:EE:FF", |
| 306 |
-50, |
| 307 |
"name", |
| 308 |
["svc"], |
| 309 |
{"svc": b"\x01"}, |
| 310 |
{1: b"\x01"}, |
| 311 |
-88, |
| 312 |
details, |
| 313 |
2001.0, |
| 314 |
) |
| 315 |
# _all_history switches back to source1 (stale time diff) |
| 316 |
assert manager._all_history["AA:BB:CC:DD:EE:FF"].source == "source1" |
| 317 |
|
| 318 |
mock_discover = MagicMock() |
| 319 |
manager._subclass_discover_info = mock_discover |
| 320 |
|
| 321 |
# Scanner 1 sends SAME data again — unchanged from scanner1's perspective, |
| 322 |
# dedup via field comparison detects no change → returns early. |
| 323 |
scanner1._async_on_advertisement( |
| 324 |
"AA:BB:CC:DD:EE:FF", |
| 325 |
-50, |
| 326 |
"name", |
| 327 |
["svc"], |
| 328 |
{"svc": b"\x01"}, |
| 329 |
{1: b"\x01"}, |
| 330 |
-88, |
| 331 |
details, |
| 332 |
2002.0, |
| 333 |
) |
| 334 |
# Same data, same source — dedup should skip dispatch |
| 335 |
mock_discover.assert_not_called() |
| 336 |
|
| 337 |
# Scanner 2 sends same data — source switches (stale), but field comparison |
| 338 |
# detects no data change, so dedup still skips dispatch on main. |
| 339 |
scanner2._async_on_advertisement( |
| 340 |
"AA:BB:CC:DD:EE:FF", |
| 341 |
-40, |
| 342 |
"name", |
| 343 |
["svc"], |
| 344 |
{"svc": b"\x01"}, |
| 345 |
{1: b"\x01"}, |
| 346 |
-88, |
| 347 |
details, |
| 348 |
3001.0, |
| 349 |
) |
| 350 |
# On main, field-level dedup returns early even with source change |
| 351 |
mock_discover.assert_not_called() |
| 352 |
# But _all_history still tracks the source switch |
| 353 |
assert manager._all_history["AA:BB:CC:DD:EE:FF"].source == "source2" |
| 354 |
|
| 355 |
# Now scanner 2 sends CHANGED data — should dispatch |
| 356 |
scanner2._async_on_advertisement( |
| 357 |
"AA:BB:CC:DD:EE:FF", |
| 358 |
-40, |
| 359 |
"name", |
| 360 |
["svc"], |
| 361 |
{"svc": b"\x02"}, |
| 362 |
{1: b"\x02"}, |
| 363 |
-88, |
| 364 |
details, |
| 365 |
3002.0, |
| 366 |
) |
| 367 |
mock_discover.assert_called_once() |
| 368 |
|
| 369 |
cancel1() |
| 370 |
cancel2() |
| 371 |
|
| 372 |
|
| 373 |
@pytest.mark.asyncio |
| 374 |
async def test_dedup_same_data_via_scanner_adv_received(): |
| 375 |
"""Test that scanner_adv_received deduplicates same data via field comparison.""" |
| 376 |
manager = get_manager() |
| 377 |
connector = HaBluetoothConnector(MockBleakClient, "any", lambda: True) |
| 378 |
scanner = BaseHaRemoteScanner("source1", "source1", connector, True) |
| 379 |
cancel = manager.async_register_scanner(scanner) |
| 380 |
|
| 381 |
device = BLEDevice("AA:BB:CC:DD:EE:FF", "name", {}) |
| 382 |
mfr_data = {1: b"\x01"} |
| 383 |
svc_data = {"service_uuid": b"\x01"} |
| 384 |
svc_uuids = ["service_uuid"] |
| 385 |
|
| 386 |
# First advertisement — seeds _all_history |
| 387 |
info1 = BluetoothServiceInfoBleak( |
| 388 |
name="name", |
| 389 |
address="AA:BB:CC:DD:EE:FF", |
| 390 |
rssi=-88, |
| 391 |
manufacturer_data=mfr_data, |
| 392 |
service_data=svc_data, |
| 393 |
service_uuids=svc_uuids, |
| 394 |
source="source1", |
| 395 |
device=device, |
| 396 |
advertisement=None, |
| 397 |
connectable=True, |
| 398 |
time=1.0, |
| 399 |
tx_power=-88, |
| 400 |
) |
| 401 |
manager.scanner_adv_received(info1) |
| 402 |
|
| 403 |
# Second advertisement with same data |
| 404 |
info2 = BluetoothServiceInfoBleak( |
| 405 |
name="name", |
| 406 |
address="AA:BB:CC:DD:EE:FF", |
| 407 |
rssi=-88, |
| 408 |
manufacturer_data=mfr_data, |
| 409 |
service_data=svc_data, |
| 410 |
service_uuids=svc_uuids, |
| 411 |
source="source1", |
| 412 |
device=device, |
| 413 |
advertisement=None, |
| 414 |
connectable=True, |
| 415 |
time=2.0, |
| 416 |
tx_power=-88, |
| 417 |
) |
| 418 |
|
| 419 |
mock_discover = MagicMock() |
| 420 |
manager._subclass_discover_info = mock_discover |
| 421 |
|
| 422 |
manager.scanner_adv_received(info2) |
| 423 |
|
| 424 |
# Same data — field comparison should detect no change and dedup |
| 425 |
mock_discover.assert_not_called() |
| 426 |
|
| 427 |
cancel() |
| 428 |
|
| 429 |
|
| 430 |
@pytest.mark.asyncio |
| 431 |
async def test_async_clear_advertisement_history(): |
| 432 |
"""Test clearing advertisement history allows same data to trigger callbacks.""" |
| 433 |
manager = get_manager() |
| 434 |
connector = HaBluetoothConnector(MockBleakClient, "any", lambda: True) |
| 435 |
scanner = BaseHaRemoteScanner("source1", "source1", connector, True) |
| 436 |
cancel = manager.async_register_scanner(scanner) |
| 437 |
|
| 438 |
address = "AA:BB:CC:DD:EE:FF" |
| 439 |
device = BLEDevice(address, "name", {}) |
| 440 |
mfr_data = {1: b"\x01"} |
| 441 |
svc_data = {"service_uuid": b"\x01"} |
| 442 |
svc_uuids = ["service_uuid"] |
| 443 |
|
| 444 |
# First advertisement — seeds history |
| 445 |
info1 = BluetoothServiceInfoBleak( |
| 446 |
name="name", |
| 447 |
address=address, |
| 448 |
rssi=-88, |
| 449 |
manufacturer_data=mfr_data, |
| 450 |
service_data=svc_data, |
| 451 |
service_uuids=svc_uuids, |
| 452 |
source="source1", |
| 453 |
device=device, |
| 454 |
advertisement=None, |
| 455 |
connectable=True, |
| 456 |
time=1.0, |
| 457 |
tx_power=-88, |
| 458 |
) |
| 459 |
manager.scanner_adv_received(info1) |
| 460 |
|
| 461 |
mock_discover = MagicMock() |
| 462 |
manager._subclass_discover_info = mock_discover |
| 463 |
|
| 464 |
# Same data again — should be deduped |
| 465 |
info2 = BluetoothServiceInfoBleak( |
| 466 |
name="name", |
| 467 |
address=address, |
| 468 |
rssi=-88, |
| 469 |
manufacturer_data=mfr_data, |
| 470 |
service_data=svc_data, |
| 471 |
service_uuids=svc_uuids, |
| 472 |
source="source1", |
| 473 |
device=device, |
| 474 |
advertisement=None, |
| 475 |
connectable=True, |
| 476 |
time=2.0, |
| 477 |
tx_power=-88, |
| 478 |
) |
| 479 |
manager.scanner_adv_received(info2) |
| 480 |
mock_discover.assert_not_called() |
| 481 |
|
| 482 |
# Clear history — next advertisement should be treated as new |
| 483 |
manager.async_clear_advertisement_history(address) |
| 484 |
|
| 485 |
info3 = BluetoothServiceInfoBleak( |
| 486 |
name="name", |
| 487 |
address=address, |
| 488 |
rssi=-88, |
| 489 |
manufacturer_data=mfr_data, |
| 490 |
service_data=svc_data, |
| 491 |
service_uuids=svc_uuids, |
| 492 |
source="source1", |
| 493 |
device=device, |
| 494 |
advertisement=None, |
| 495 |
connectable=True, |
| 496 |
time=3.0, |
| 497 |
tx_power=-88, |
| 498 |
) |
| 499 |
manager.scanner_adv_received(info3) |
| 500 |
mock_discover.assert_called_once() |
| 501 |
|
| 502 |
cancel() |
| 503 |
|
| 504 |
|
| 505 |
STATE_A_UUID = "0000e800-0000-1000-8000-00805f9b34fb" |
| 506 |
STATE_B_UUID = "0000e000-0000-1000-8000-00805f9b34fb" |
| 507 |
|
| 508 |
|
| 509 |
def _advertise_state_uuid( |
| 510 |
scanner: BaseHaRemoteScanner, address: str, uuid: str, timestamp: float |
| 511 |
) -> None: |
| 512 |
"""Report an advertisement carrying a single mutually-exclusive state UUID.""" |
| 513 |
scanner._async_on_advertisement( |
| 514 |
address, -88, "name", [uuid], {}, {}, -88, {}, timestamp |
| 515 |
) |
| 516 |
|
| 517 |
|
| 518 |
@pytest.mark.asyncio |
| 519 |
async def test_async_clear_advertisement_history_clears_scanner_merging(): |
| 520 |
"""Test that clearing history resets UUID merging but keeps the device.""" |
| 521 |
manager = get_manager() |
| 522 |
connector = HaBluetoothConnector(MockBleakClient, "any", lambda: True) |
| 523 |
scanner = BaseHaRemoteScanner("source1", "source1", connector, True) |
| 524 |
cancel = manager.async_register_scanner(scanner) |
| 525 |
|
| 526 |
address = "AA:BB:CC:DD:EE:FF" |
| 527 |
|
| 528 |
# Clearing an address the scanner has never seen does not create a record |
| 529 |
manager.async_clear_advertisement_history(address) |
| 530 |
assert address not in scanner._previous_service_info |
| 531 |
|
| 532 |
_advertise_state_uuid(scanner, address, STATE_A_UUID, 1.0) |
| 533 |
device = scanner._previous_service_info[address].device |
| 534 |
# Without a clear, the mutually-exclusive state UUIDs merge into one set |
| 535 |
_advertise_state_uuid(scanner, address, STATE_B_UUID, 2.0) |
| 536 |
assert set(scanner._previous_service_info[address].service_uuids) == { |
| 537 |
STATE_A_UUID, |
| 538 |
STATE_B_UUID, |
| 539 |
} |
| 540 |
|
| 541 |
manager.async_clear_advertisement_history(address) |
| 542 |
|
| 543 |
assert address not in manager._all_history |
| 544 |
assert address not in manager._connectable_history |
| 545 |
|
| 546 |
# bleak-retry-connector resolves a backend through this list; an empty one |
| 547 |
# raises "never seen by any scanner" until the device advertises again |
| 548 |
assert manager.async_scanner_devices_by_address(address, True) |
| 549 |
assert address in scanner.discovered_addresses |
| 550 |
stored = scanner._previous_service_info[address] |
| 551 |
assert stored.device is device |
| 552 |
assert stored.rssi == -88 |
| 553 |
assert stored.time == 2.0 |
| 554 |
assert stored.service_uuids == [] |
| 555 |
|
| 556 |
# The next advertisement is built from scratch instead of being merged |
| 557 |
_advertise_state_uuid(scanner, address, STATE_A_UUID, 3.0) |
| 558 |
assert scanner._previous_service_info[address].service_uuids == [STATE_A_UUID] |
| 559 |
assert scanner._previous_service_info[address].device is device |
| 560 |
|
| 561 |
# ...and only that one advertisement, merging resumes afterwards |
| 562 |
_advertise_state_uuid(scanner, address, STATE_B_UUID, 4.0) |
| 563 |
assert set(scanner._previous_service_info[address].service_uuids) == { |
| 564 |
STATE_A_UUID, |
| 565 |
STATE_B_UUID, |
| 566 |
} |
| 567 |
|
| 568 |
cancel() |
| 569 |
|
| 570 |
|
| 571 |
@pytest.mark.asyncio |
| 572 |
async def test_async_clear_advertisement_history_from_dispatch_callback(): |
| 573 |
"""Test clearing from the callback the advertisement is dispatched to.""" |
| 574 |
manager = get_manager() |
| 575 |
connector = HaBluetoothConnector(MockBleakClient, "any", lambda: True) |
| 576 |
scanner = BaseHaRemoteScanner("source1", "source1", connector, True) |
| 577 |
cancel = manager.async_register_scanner(scanner) |
| 578 |
|
| 579 |
address = "AA:BB:CC:DD:EE:FF" |
| 580 |
dispatched: list[BluetoothServiceInfoBleak] = [] |
| 581 |
|
| 582 |
def _clear_on_dispatch(service_info: BluetoothServiceInfoBleak) -> None: |
| 583 |
dispatched.append(service_info) |
| 584 |
manager.async_clear_advertisement_history(service_info.address) |
| 585 |
|
| 586 |
# The #358 flow: a consumer that needs mutually-exclusive state UUIDs never |
| 587 |
# to accumulate clears on every advertisement, from inside the dispatch the |
| 588 |
# scanner makes after storing its record. |
| 589 |
manager._subclass_discover_info = _clear_on_dispatch |
| 590 |
|
| 591 |
_advertise_state_uuid(scanner, address, STATE_A_UUID, 1.0) |
| 592 |
device = scanner._previous_service_info[address].device |
| 593 |
_advertise_state_uuid(scanner, address, STATE_B_UUID, 2.0) |
| 594 |
_advertise_state_uuid(scanner, address, STATE_A_UUID, 3.0) |
| 595 |
|
| 596 |
# Each advertisement is dispatched with only its own state UUID; the reset |
| 597 |
# builds a new record rather than mutating the one already handed out |
| 598 |
assert [info.service_uuids for info in dispatched] == [ |
| 599 |
[STATE_A_UUID], |
| 600 |
[STATE_B_UUID], |
| 601 |
[STATE_A_UUID], |
| 602 |
] |
| 603 |
|
| 604 |
# ...and the address stays reachable throughout, never dropping out of the |
| 605 |
# scanner between the clear and the next advertisement |
| 606 |
stored = scanner._previous_service_info[address] |
| 607 |
assert stored.service_uuids == [] |
| 608 |
assert stored.device is device |
| 609 |
assert manager.async_scanner_devices_by_address(address, True) |
| 610 |
|
| 611 |
cancel() |
| 612 |
|
| 613 |
|
| 614 |
@pytest.mark.asyncio |
| 615 |
async def test_async_clear_advertisement_history_bypasses_raw_shortcut(): |
| 616 |
"""Test an unchanged raw advertisement still resets merging after a clear.""" |
| 617 |
manager = get_manager() |
| 618 |
connector = HaBluetoothConnector(MockBleakClient, "any", lambda: True) |
| 619 |
scanner = BaseHaRemoteScanner("source1", "source1", connector, True) |
| 620 |
cancel = manager.async_register_scanner(scanner) |
| 621 |
|
| 622 |
address = "AA:BB:CC:DD:EE:FF" |
| 623 |
raw_a = b"\x03\x03\x00\xe8" |
| 624 |
raw_b = b"\x03\x03\x00\xe0" |
| 625 |
scanner._async_on_raw_advertisement(address, -88, raw_a, {}, 1.0) |
| 626 |
scanner._async_on_raw_advertisement(address, -88, raw_b, {}, 2.0) |
| 627 |
assert set(scanner._previous_service_info[address].service_uuids) == { |
| 628 |
STATE_A_UUID, |
| 629 |
STATE_B_UUID, |
| 630 |
} |
| 631 |
|
| 632 |
manager.async_clear_advertisement_history(address) |
| 633 |
|
| 634 |
# Identical raw bytes normally short-circuit straight to the merged data |
| 635 |
scanner._async_on_raw_advertisement(address, -88, raw_b, {}, 3.0) |
| 636 |
assert scanner._previous_service_info[address].service_uuids == [STATE_B_UUID] |
| 637 |
assert scanner._previous_service_info[address].raw == raw_b |
| 638 |
|
| 639 |
cancel() |