| 1 |
"""Tests for the Bluetooth base scanner models.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import asyncio |
| 6 |
import time |
| 7 |
from datetime import timedelta |
| 8 |
from typing import Any |
| 9 |
from unittest.mock import ANY, MagicMock, Mock, patch |
| 10 |
|
| 11 |
import pytest |
| 12 |
from bleak.backends.device import BLEDevice |
| 13 |
from bleak.backends.scanner import AdvertisementData |
| 14 |
from bleak_retry_connector import Allocations |
| 15 |
from bluetooth_data_tools import monotonic_time_coarse |
| 16 |
|
| 17 |
from habluetooth import ( |
| 18 |
BaseHaRemoteScanner, |
| 19 |
BaseHaScanner, |
| 20 |
BluetoothManager, |
| 21 |
BluetoothScannerDevice, |
| 22 |
BluetoothScanningMode, |
| 23 |
HaBluetoothConnector, |
| 24 |
HaScannerDetails, |
| 25 |
HaScannerModeChange, |
| 26 |
HaScannerType, |
| 27 |
get_manager, |
| 28 |
set_manager, |
| 29 |
) |
| 30 |
from habluetooth.const import ( |
| 31 |
CONNECTABLE_FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS, |
| 32 |
FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS, |
| 33 |
SCANNER_WATCHDOG_INTERVAL, |
| 34 |
SCANNER_WATCHDOG_TIMEOUT, |
| 35 |
) |
| 36 |
from habluetooth.storage import ( |
| 37 |
DiscoveredDeviceAdvertisementData, |
| 38 |
) |
| 39 |
|
| 40 |
from . import ( |
| 41 |
HCI0_SOURCE_ADDRESS, |
| 42 |
MockBleakClient, |
| 43 |
async_fire_time_changed, |
| 44 |
generate_advertisement_data, |
| 45 |
generate_ble_device, |
| 46 |
patch_bluetooth_time, |
| 47 |
utcnow, |
| 48 |
) |
| 49 |
from . import ( |
| 50 |
InjectableRemoteScanner as FakeScanner, |
| 51 |
) |
| 52 |
from .conftest import FakeBluetoothAdapters, MockBluetoothManagerWithCallbacks |
| 53 |
|
| 54 |
|
| 55 |
@pytest.mark.parametrize("name_2", [None, "w"]) |
| 56 |
@pytest.mark.usefixtures("enable_bluetooth") |
| 57 |
@pytest.mark.asyncio |
| 58 |
async def test_remote_scanner(name_2: str | None) -> None: |
| 59 |
"""Test the remote scanner base class merges advertisement_data.""" |
| 60 |
manager = get_manager() |
| 61 |
|
| 62 |
switchbot_device = generate_ble_device( |
| 63 |
"44:44:33:11:23:45", |
| 64 |
"wohand", |
| 65 |
{}, |
| 66 |
rssi=-100, |
| 67 |
) |
| 68 |
switchbot_device_adv = generate_advertisement_data( |
| 69 |
local_name="wohand", |
| 70 |
service_uuids=["050a021a-0000-1000-8000-00805f9b34fb"], |
| 71 |
service_data={"050a021a-0000-1000-8000-00805f9b34fb": b"\n\xff"}, |
| 72 |
manufacturer_data={1: b"\x01"}, |
| 73 |
rssi=-100, |
| 74 |
) |
| 75 |
switchbot_device_2 = generate_ble_device( |
| 76 |
"44:44:33:11:23:45", |
| 77 |
name_2, |
| 78 |
{}, |
| 79 |
rssi=-100, |
| 80 |
) |
| 81 |
switchbot_device_adv_2 = generate_advertisement_data( |
| 82 |
local_name=name_2, |
| 83 |
service_uuids=["00000001-0000-1000-8000-00805f9b34fb"], |
| 84 |
service_data={"00000001-0000-1000-8000-00805f9b34fb": b"\n\xff"}, |
| 85 |
manufacturer_data={1: b"\x01", 2: b"\x02"}, |
| 86 |
rssi=-100, |
| 87 |
) |
| 88 |
switchbot_device_3 = generate_ble_device( |
| 89 |
"44:44:33:11:23:45", |
| 90 |
"wohandlonger", |
| 91 |
{}, |
| 92 |
rssi=-100, |
| 93 |
) |
| 94 |
switchbot_device_adv_3 = generate_advertisement_data( |
| 95 |
local_name="wohandlonger", |
| 96 |
service_uuids=["00000001-0000-1000-8000-00805f9b34fb"], |
| 97 |
service_data={"00000001-0000-1000-8000-00805f9b34fb": b"\n\xff"}, |
| 98 |
manufacturer_data={1: b"\x01", 2: b"\x02"}, |
| 99 |
rssi=-100, |
| 100 |
) |
| 101 |
switchbot_device_adv_4 = generate_advertisement_data( |
| 102 |
local_name="wohandlonger", |
| 103 |
service_uuids=["00000001-0000-1000-8000-00805f9b34fb"], |
| 104 |
service_data={"00000001-0000-1000-8000-00805f9b34fb": b"\n\xff"}, |
| 105 |
manufacturer_data={1: b"\x04", 2: b"\x02", 3: b"\x03"}, |
| 106 |
rssi=-100, |
| 107 |
) |
| 108 |
switchbot_device_adv_5 = generate_advertisement_data( |
| 109 |
local_name="wohandlonger", |
| 110 |
service_uuids=["00000001-0000-1000-8000-00805f9b34fb"], |
| 111 |
service_data={"00000001-0000-1000-8000-00805f9b34fb": b"\n\xff"}, |
| 112 |
manufacturer_data={1: b"\x04", 2: b"\x01"}, |
| 113 |
rssi=-100, |
| 114 |
) |
| 115 |
connector = HaBluetoothConnector( |
| 116 |
MockBleakClient, "mock_bleak_client", lambda: False |
| 117 |
) |
| 118 |
scanner = FakeScanner("esp32", "esp32", connector, True) |
| 119 |
details = scanner.details |
| 120 |
assert details == HaScannerDetails( |
| 121 |
source=scanner.source, |
| 122 |
connectable=scanner.connectable, |
| 123 |
name=scanner.name, |
| 124 |
adapter=scanner.adapter, |
| 125 |
scanner_type=HaScannerType.REMOTE, |
| 126 |
) |
| 127 |
unsetup = scanner.async_setup() |
| 128 |
cancel = manager.async_register_scanner(scanner) |
| 129 |
|
| 130 |
scanner.inject_advertisement(switchbot_device, switchbot_device_adv) |
| 131 |
|
| 132 |
data = scanner.discovered_devices_and_advertisement_data |
| 133 |
discovered_device, discovered_adv_data = data[switchbot_device.address] |
| 134 |
assert discovered_device.address == switchbot_device.address |
| 135 |
assert discovered_device.name == switchbot_device.name |
| 136 |
assert ( |
| 137 |
discovered_adv_data.manufacturer_data == switchbot_device_adv.manufacturer_data |
| 138 |
) |
| 139 |
assert discovered_adv_data.service_data == switchbot_device_adv.service_data |
| 140 |
assert discovered_adv_data.service_uuids == switchbot_device_adv.service_uuids |
| 141 |
scanner.inject_advertisement(switchbot_device_2, switchbot_device_adv_2) |
| 142 |
|
| 143 |
data = scanner.discovered_devices_and_advertisement_data |
| 144 |
discovered_device, discovered_adv_data = data[switchbot_device.address] |
| 145 |
assert discovered_device.address == switchbot_device.address |
| 146 |
assert discovered_device.name == switchbot_device.name |
| 147 |
assert discovered_adv_data.manufacturer_data == {1: b"\x01", 2: b"\x02"} |
| 148 |
assert discovered_adv_data.service_data == { |
| 149 |
"050a021a-0000-1000-8000-00805f9b34fb": b"\n\xff", |
| 150 |
"00000001-0000-1000-8000-00805f9b34fb": b"\n\xff", |
| 151 |
} |
| 152 |
assert set(discovered_adv_data.service_uuids) == { |
| 153 |
"050a021a-0000-1000-8000-00805f9b34fb", |
| 154 |
"00000001-0000-1000-8000-00805f9b34fb", |
| 155 |
} |
| 156 |
|
| 157 |
# The longer name should be used |
| 158 |
scanner.inject_advertisement(switchbot_device_3, switchbot_device_adv_3) |
| 159 |
assert discovered_device.name == switchbot_device_3.name |
| 160 |
|
| 161 |
# Inject the shorter name / None again to make |
| 162 |
# sure we always keep the longer name |
| 163 |
scanner.inject_advertisement(switchbot_device_2, switchbot_device_adv_2) |
| 164 |
assert discovered_device.name == switchbot_device_3.name |
| 165 |
|
| 166 |
scanner.inject_advertisement(switchbot_device_2, switchbot_device_adv_4) |
| 167 |
assert scanner.discovered_devices_and_advertisement_data[ |
| 168 |
switchbot_device_2.address |
| 169 |
][1].manufacturer_data == {1: b"\x04", 2: b"\x02", 3: b"\x03"} |
| 170 |
scanner.inject_advertisement(switchbot_device_2, switchbot_device_adv_5) |
| 171 |
assert scanner.discovered_devices_and_advertisement_data[ |
| 172 |
switchbot_device_2.address |
| 173 |
][1].manufacturer_data == {1: b"\x04", 2: b"\x01", 3: b"\x03"} |
| 174 |
|
| 175 |
assert ( |
| 176 |
"00090401-0052-036b-3206-ff0a050a021a" |
| 177 |
not in scanner.discovered_devices_and_advertisement_data[ |
| 178 |
switchbot_device_2.address |
| 179 |
][1].service_data |
| 180 |
) |
| 181 |
|
| 182 |
scanner.inject_raw_advertisement( |
| 183 |
switchbot_device_2.address, |
| 184 |
0, |
| 185 |
b"\x12\x21\x1a\x02\n\x05\n\xff\x062k\x03R\x00\x01\x04\t\x00\x04", |
| 186 |
) |
| 187 |
|
| 188 |
assert ( |
| 189 |
"00090401-0052-036b-3206-ff0a050a021a" |
| 190 |
in scanner.discovered_devices_and_advertisement_data[ |
| 191 |
switchbot_device_2.address |
| 192 |
][1].service_data |
| 193 |
) |
| 194 |
|
| 195 |
assert scanner.serialize_discovered_devices() == DiscoveredDeviceAdvertisementData( |
| 196 |
connectable=True, |
| 197 |
expire_seconds=195, |
| 198 |
discovered_device_advertisement_datas={"44:44:33:11:23:45": ANY}, |
| 199 |
discovered_device_timestamps={"44:44:33:11:23:45": ANY}, |
| 200 |
discovered_device_raw={ |
| 201 |
"44:44:33:11:23:45": b"\x12!\x1a\x02" |
| 202 |
b"\n\x05\n\xff" |
| 203 |
b"\x062k\x03" |
| 204 |
b"R\x00\x01\x04" |
| 205 |
b"\t\x00\x04" |
| 206 |
}, |
| 207 |
) |
| 208 |
|
| 209 |
cancel() |
| 210 |
unsetup() |
| 211 |
|
| 212 |
|
| 213 |
@pytest.mark.usefixtures("enable_bluetooth") |
| 214 |
@pytest.mark.asyncio |
| 215 |
async def test_raw_advertisement_fast_path_unchanged() -> None: |
| 216 |
"""Test that sending the same raw bytes twice uses the fast path.""" |
| 217 |
manager = get_manager() |
| 218 |
|
| 219 |
connector = HaBluetoothConnector( |
| 220 |
MockBleakClient, "mock_bleak_client", lambda: False |
| 221 |
) |
| 222 |
scanner = FakeScanner("esp32", "esp32", connector, True) |
| 223 |
unsetup = scanner.async_setup() |
| 224 |
cancel = manager.async_register_scanner(scanner) |
| 225 |
|
| 226 |
raw_adv = b"\x12\x21\x1a\x02\n\x05\n\xff\x062k\x03R\x00\x01\x04\t\x00\x04" |
| 227 |
address = "44:44:33:11:23:45" |
| 228 |
|
| 229 |
# First raw advertisement — takes the slow path (parse + merge) |
| 230 |
scanner.inject_raw_advertisement(address, -60, raw_adv, 1.0) |
| 231 |
info1 = scanner._previous_service_info[address] |
| 232 |
assert info1.rssi == -60 |
| 233 |
assert info1.raw == raw_adv |
| 234 |
|
| 235 |
# Second raw advertisement with same bytes — takes the fast path |
| 236 |
scanner.inject_raw_advertisement(address, -50, raw_adv, 2.0) |
| 237 |
info2 = scanner._previous_service_info[address] |
| 238 |
assert info2.rssi == -50 |
| 239 |
assert info2.time == 2.0 |
| 240 |
# Fast path reuses parsed data from prev_info |
| 241 |
assert info2.manufacturer_data is info1.manufacturer_data |
| 242 |
assert info2.service_data is info1.service_data |
| 243 |
assert info2.service_uuids is info1.service_uuids |
| 244 |
assert info2.name is info1.name |
| 245 |
assert info2.device is info1.device |
| 246 |
assert info2.raw is info1.raw |
| 247 |
|
| 248 |
# Third raw advertisement with different bytes — takes the slow path |
| 249 |
raw_adv_changed = b"\x12\x21\x1a\x02\n\x05\n\xff\x062k\x03R\x00\x01\x04\t\x00\x05" |
| 250 |
scanner.inject_raw_advertisement(address, -40, raw_adv_changed, 3.0) |
| 251 |
info3 = scanner._previous_service_info[address] |
| 252 |
assert info3.rssi == -40 |
| 253 |
assert info3.raw == raw_adv_changed |
| 254 |
# Slow path re-parsed, so objects may differ |
| 255 |
assert info3.raw is not info1.raw |
| 256 |
|
| 257 |
cancel() |
| 258 |
unsetup() |
| 259 |
|
| 260 |
|
| 261 |
@pytest.mark.usefixtures("enable_bluetooth") |
| 262 |
@pytest.mark.asyncio |
| 263 |
async def test_remote_scanner_expires_connectable() -> None: |
| 264 |
"""Test the remote scanner expires stale connectable data.""" |
| 265 |
manager = get_manager() |
| 266 |
|
| 267 |
switchbot_device = generate_ble_device( |
| 268 |
"44:44:33:11:23:45", |
| 269 |
"wohand", |
| 270 |
{}, |
| 271 |
rssi=-100, |
| 272 |
) |
| 273 |
switchbot_device_adv = generate_advertisement_data( |
| 274 |
local_name="wohand", |
| 275 |
service_uuids=[], |
| 276 |
manufacturer_data={1: b"\x01"}, |
| 277 |
rssi=-100, |
| 278 |
) |
| 279 |
|
| 280 |
connector = HaBluetoothConnector( |
| 281 |
MockBleakClient, "mock_bleak_client", lambda: False |
| 282 |
) |
| 283 |
scanner = FakeScanner("esp32", "esp32", connector, True) |
| 284 |
unsetup = scanner.async_setup() |
| 285 |
cancel = manager.async_register_scanner(scanner) |
| 286 |
|
| 287 |
start_time_monotonic = time.monotonic() |
| 288 |
scanner.inject_advertisement(switchbot_device, switchbot_device_adv) |
| 289 |
|
| 290 |
devices = scanner.discovered_devices |
| 291 |
assert len(scanner.discovered_devices) == 1 |
| 292 |
assert len(scanner.discovered_devices_and_advertisement_data) == 1 |
| 293 |
assert devices[0].name == "wohand" |
| 294 |
|
| 295 |
expire_monotonic = ( |
| 296 |
start_time_monotonic |
| 297 |
+ CONNECTABLE_FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS |
| 298 |
+ 1 |
| 299 |
) |
| 300 |
expire_utc = utcnow() + timedelta( |
| 301 |
seconds=CONNECTABLE_FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS + 1 |
| 302 |
) |
| 303 |
with patch_bluetooth_time(expire_monotonic): |
| 304 |
async_fire_time_changed(expire_utc) |
| 305 |
await asyncio.sleep(0) |
| 306 |
|
| 307 |
devices = scanner.discovered_devices |
| 308 |
assert len(scanner.discovered_devices) == 0 |
| 309 |
assert len(scanner.discovered_devices_and_advertisement_data) == 0 |
| 310 |
|
| 311 |
cancel() |
| 312 |
unsetup() |
| 313 |
|
| 314 |
|
| 315 |
@pytest.mark.usefixtures("enable_bluetooth") |
| 316 |
@pytest.mark.asyncio |
| 317 |
async def test_remote_scanner_expires_non_connectable() -> None: |
| 318 |
"""Test the remote scanner expires stale non connectable data.""" |
| 319 |
manager = get_manager() |
| 320 |
|
| 321 |
switchbot_device = generate_ble_device( |
| 322 |
"44:44:33:11:23:45", |
| 323 |
"wohand", |
| 324 |
{}, |
| 325 |
rssi=-100, |
| 326 |
) |
| 327 |
switchbot_device_adv = generate_advertisement_data( |
| 328 |
local_name="wohand", |
| 329 |
service_uuids=[], |
| 330 |
manufacturer_data={1: b"\x01"}, |
| 331 |
rssi=-100, |
| 332 |
) |
| 333 |
|
| 334 |
connector = HaBluetoothConnector( |
| 335 |
MockBleakClient, "mock_bleak_client", lambda: False |
| 336 |
) |
| 337 |
scanner = FakeScanner("esp32", "esp32", connector, True) |
| 338 |
unsetup = scanner.async_setup() |
| 339 |
cancel = manager.async_register_scanner(scanner) |
| 340 |
|
| 341 |
start_time_monotonic = time.monotonic() |
| 342 |
scanner.inject_advertisement(switchbot_device, switchbot_device_adv) |
| 343 |
|
| 344 |
devices = scanner.discovered_devices |
| 345 |
assert len(scanner.discovered_devices) == 1 |
| 346 |
assert len(scanner.discovered_devices_and_advertisement_data) == 1 |
| 347 |
assert len(scanner.discovered_device_timestamps) == 1 |
| 348 |
dev_adv = scanner.get_discovered_device_advertisement_data(switchbot_device.address) |
| 349 |
assert dev_adv is not None |
| 350 |
dev, adv = dev_adv |
| 351 |
assert dev.name == "wohand" |
| 352 |
assert adv.local_name == "wohand" |
| 353 |
assert adv.manufacturer_data == switchbot_device_adv.manufacturer_data |
| 354 |
assert devices[0].name == "wohand" |
| 355 |
|
| 356 |
assert ( |
| 357 |
FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS |
| 358 |
> CONNECTABLE_FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS |
| 359 |
) |
| 360 |
|
| 361 |
# The connectable timeout is used for all devices |
| 362 |
# as the manager takes care of availability and the scanner |
| 363 |
# if only concerned about making a connection |
| 364 |
expire_monotonic = ( |
| 365 |
start_time_monotonic |
| 366 |
+ CONNECTABLE_FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS |
| 367 |
+ 1 |
| 368 |
) |
| 369 |
expire_utc = utcnow() + timedelta( |
| 370 |
seconds=CONNECTABLE_FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS + 1 |
| 371 |
) |
| 372 |
with patch_bluetooth_time(expire_monotonic): |
| 373 |
async_fire_time_changed(expire_utc) |
| 374 |
await asyncio.sleep(0) |
| 375 |
|
| 376 |
assert len(scanner.discovered_devices) == 0 |
| 377 |
assert len(scanner.discovered_devices_and_advertisement_data) == 0 |
| 378 |
assert len(scanner.discovered_device_timestamps) == 0 |
| 379 |
assert ( |
| 380 |
scanner.get_discovered_device_advertisement_data(switchbot_device.address) |
| 381 |
is None |
| 382 |
) |
| 383 |
|
| 384 |
expire_monotonic = ( |
| 385 |
start_time_monotonic + FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS + 1 |
| 386 |
) |
| 387 |
expire_utc = utcnow() + timedelta( |
| 388 |
seconds=FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS + 1 |
| 389 |
) |
| 390 |
with patch_bluetooth_time(expire_monotonic): |
| 391 |
async_fire_time_changed(expire_utc) |
| 392 |
await asyncio.sleep(0) |
| 393 |
|
| 394 |
assert len(scanner.discovered_devices) == 0 |
| 395 |
assert len(scanner.discovered_devices_and_advertisement_data) == 0 |
| 396 |
assert len(scanner.discovered_device_timestamps) == 0 |
| 397 |
assert ( |
| 398 |
scanner.get_discovered_device_advertisement_data(switchbot_device.address) |
| 399 |
is None |
| 400 |
) |
| 401 |
|
| 402 |
cancel() |
| 403 |
unsetup() |
| 404 |
|
| 405 |
|
| 406 |
@pytest.mark.usefixtures("enable_bluetooth") |
| 407 |
@pytest.mark.asyncio |
| 408 |
async def test_remote_scanner_discovered_device_timestamps_deprecated() -> None: |
| 409 |
"""The leading-underscore shim still returns timestamps but warns.""" |
| 410 |
manager = get_manager() |
| 411 |
switchbot_device = generate_ble_device("44:44:33:11:23:45", "wohand", {}, rssi=-100) |
| 412 |
switchbot_device_adv = generate_advertisement_data( |
| 413 |
local_name="wohand", service_uuids=[], manufacturer_data={1: b"\x01"}, rssi=-100 |
| 414 |
) |
| 415 |
connector = HaBluetoothConnector( |
| 416 |
MockBleakClient, "mock_bleak_client", lambda: False |
| 417 |
) |
| 418 |
scanner = FakeScanner("esp32", "esp32", connector, True) |
| 419 |
unsetup = scanner.async_setup() |
| 420 |
cancel = manager.async_register_scanner(scanner) |
| 421 |
scanner.inject_advertisement(switchbot_device, switchbot_device_adv) |
| 422 |
|
| 423 |
with pytest.warns( |
| 424 |
FutureWarning, match=r"BaseHaScanner\._discovered_device_timestamps" |
| 425 |
): |
| 426 |
legacy = scanner._discovered_device_timestamps |
| 427 |
|
| 428 |
assert legacy == scanner.discovered_device_timestamps |
| 429 |
assert len(legacy) == 1 |
| 430 |
|
| 431 |
cancel() |
| 432 |
unsetup() |
| 433 |
|
| 434 |
|
| 435 |
@pytest.mark.usefixtures("enable_bluetooth") |
| 436 |
@pytest.mark.asyncio |
| 437 |
async def test_base_scanner_connecting_behavior() -> None: |
| 438 |
"""Test the default behavior is to mark the scanner as not scanning on connect.""" |
| 439 |
manager = get_manager() |
| 440 |
|
| 441 |
switchbot_device = generate_ble_device( |
| 442 |
"44:44:33:11:23:45", |
| 443 |
"wohand", |
| 444 |
{}, |
| 445 |
rssi=-100, |
| 446 |
) |
| 447 |
switchbot_device_adv = generate_advertisement_data( |
| 448 |
local_name="wohand", |
| 449 |
service_uuids=[], |
| 450 |
manufacturer_data={1: b"\x01"}, |
| 451 |
rssi=-100, |
| 452 |
) |
| 453 |
|
| 454 |
connector = HaBluetoothConnector( |
| 455 |
MockBleakClient, "mock_bleak_client", lambda: False |
| 456 |
) |
| 457 |
scanner = FakeScanner("esp32", "esp32", connector, True) |
| 458 |
unsetup = scanner.async_setup() |
| 459 |
cancel = manager.async_register_scanner(scanner) |
| 460 |
|
| 461 |
with scanner.connecting(): |
| 462 |
assert scanner.scanning is False |
| 463 |
|
| 464 |
# We should still accept new advertisements while connecting |
| 465 |
# since advertisements are delivered asynchronously and |
| 466 |
# we don't want to miss any even when we are willing to |
| 467 |
# accept advertisements from another scanner in the brief window |
| 468 |
# between when we start connecting and when we stop scanning |
| 469 |
scanner.inject_advertisement(switchbot_device, switchbot_device_adv) |
| 470 |
|
| 471 |
devices = scanner.discovered_devices |
| 472 |
assert len(scanner.discovered_devices) == 1 |
| 473 |
assert len(scanner.discovered_devices_and_advertisement_data) == 1 |
| 474 |
assert devices[0].name == "wohand" |
| 475 |
|
| 476 |
cancel() |
| 477 |
unsetup() |
| 478 |
|
| 479 |
|
| 480 |
@pytest.mark.usefixtures("enable_bluetooth") |
| 481 |
@pytest.mark.asyncio |
| 482 |
async def test_scanner_stops_responding() -> None: |
| 483 |
"""Test we mark a scanner are not scanning when it stops responding.""" |
| 484 |
manager = get_manager() |
| 485 |
|
| 486 |
connector = HaBluetoothConnector( |
| 487 |
MockBleakClient, "mock_bleak_client", lambda: False |
| 488 |
) |
| 489 |
scanner = FakeScanner( |
| 490 |
"esp32", |
| 491 |
"esp32", |
| 492 |
connector, |
| 493 |
True, |
| 494 |
current_mode=BluetoothScanningMode.ACTIVE, |
| 495 |
requested_mode=BluetoothScanningMode.ACTIVE, |
| 496 |
) |
| 497 |
unsetup = scanner.async_setup() |
| 498 |
cancel = manager.async_register_scanner(scanner) |
| 499 |
|
| 500 |
start_time_monotonic = time.monotonic() |
| 501 |
|
| 502 |
assert scanner.scanning is True |
| 503 |
failure_reached_time = ( |
| 504 |
start_time_monotonic |
| 505 |
+ SCANNER_WATCHDOG_TIMEOUT |
| 506 |
+ SCANNER_WATCHDOG_INTERVAL.total_seconds() |
| 507 |
) |
| 508 |
# We hit the timer with no detections, |
| 509 |
# so we reset the adapter and restart the scanner |
| 510 |
with patch_bluetooth_time(failure_reached_time): |
| 511 |
async_fire_time_changed(utcnow() + SCANNER_WATCHDOG_INTERVAL) |
| 512 |
await asyncio.sleep(0) |
| 513 |
|
| 514 |
assert scanner.scanning is False |
| 515 |
|
| 516 |
bparasite_device = generate_ble_device( # type: ignore[unreachable] |
| 517 |
"44:44:33:11:23:45", |
| 518 |
"bparasite", |
| 519 |
{}, |
| 520 |
rssi=-100, |
| 521 |
) |
| 522 |
bparasite_device_adv = generate_advertisement_data( |
| 523 |
local_name="bparasite", |
| 524 |
service_uuids=[], |
| 525 |
manufacturer_data={1: b"\x01"}, |
| 526 |
rssi=-100, |
| 527 |
) |
| 528 |
|
| 529 |
failure_reached_time += 1 |
| 530 |
|
| 531 |
with patch_bluetooth_time(failure_reached_time): |
| 532 |
scanner.inject_advertisement( |
| 533 |
bparasite_device, bparasite_device_adv, failure_reached_time |
| 534 |
) |
| 535 |
|
| 536 |
# As soon as we get a detection, we know the scanner is working again |
| 537 |
assert scanner.scanning is True |
| 538 |
assert scanner.requested_mode == BluetoothScanningMode.ACTIVE |
| 539 |
assert scanner.current_mode == BluetoothScanningMode.ACTIVE |
| 540 |
|
| 541 |
cancel() |
| 542 |
unsetup() |
| 543 |
|
| 544 |
|
| 545 |
@pytest.mark.usefixtures("enable_bluetooth") |
| 546 |
@pytest.mark.asyncio |
| 547 |
async def test_merge_manufacturer_data_history_existing() -> None: |
| 548 |
"""Test merging manufacturer data history.""" |
| 549 |
manager = get_manager() |
| 550 |
|
| 551 |
sensor_push_device = generate_ble_device( |
| 552 |
"44:44:33:11:23:45", |
| 553 |
"", |
| 554 |
{}, |
| 555 |
rssi=-60, |
| 556 |
) |
| 557 |
sensor_push_device_adv = generate_advertisement_data( |
| 558 |
local_name="", |
| 559 |
rssi=-60, |
| 560 |
manufacturer_data={ |
| 561 |
64256: b"B\r.\xa9\xb6", |
| 562 |
31488: b"\x98\xfa\xb6\x91\xb6", |
| 563 |
}, |
| 564 |
service_uuids=["ef090000-11d6-42ba-93b8-9dd7ec090ab0"], |
| 565 |
service_data={}, |
| 566 |
) |
| 567 |
|
| 568 |
sensor_push_adv_2 = generate_advertisement_data( |
| 569 |
local_name="", |
| 570 |
service_uuids=["ef090000-11d6-42ba-93b8-9dd7ec090ab0"], |
| 571 |
service_data={}, |
| 572 |
manufacturer_data={ |
| 573 |
31488: b"\x98\xfa\xb6\x91\xb6", |
| 574 |
}, |
| 575 |
rssi=-100, |
| 576 |
) |
| 577 |
|
| 578 |
connector = HaBluetoothConnector( |
| 579 |
MockBleakClient, "mock_bleak_client", lambda: False |
| 580 |
) |
| 581 |
scanner = FakeScanner("esp32", "esp32", connector, True) |
| 582 |
details = scanner.details |
| 583 |
assert details == HaScannerDetails( |
| 584 |
source=scanner.source, |
| 585 |
connectable=scanner.connectable, |
| 586 |
name=scanner.name, |
| 587 |
adapter=scanner.adapter, |
| 588 |
scanner_type=HaScannerType.REMOTE, |
| 589 |
) |
| 590 |
unsetup = scanner.async_setup() |
| 591 |
cancel = manager.async_register_scanner(scanner) |
| 592 |
|
| 593 |
scanner.inject_advertisement(sensor_push_device, sensor_push_device_adv) |
| 594 |
|
| 595 |
data = scanner.discovered_devices_and_advertisement_data |
| 596 |
discovered_device, discovered_adv_data = data[sensor_push_device.address] |
| 597 |
assert discovered_device.address == sensor_push_device.address |
| 598 |
assert discovered_device.name == sensor_push_device.name |
| 599 |
assert ( |
| 600 |
discovered_adv_data.manufacturer_data |
| 601 |
== sensor_push_device_adv.manufacturer_data |
| 602 |
) |
| 603 |
assert discovered_adv_data.service_data == sensor_push_device_adv.service_data |
| 604 |
assert discovered_adv_data.service_uuids == sensor_push_device_adv.service_uuids |
| 605 |
scanner.inject_advertisement(sensor_push_device, sensor_push_adv_2) |
| 606 |
|
| 607 |
data = scanner.discovered_devices_and_advertisement_data |
| 608 |
discovered_device, discovered_adv_data = data[sensor_push_device.address] |
| 609 |
assert discovered_device.address == sensor_push_device.address |
| 610 |
assert discovered_device.name == sensor_push_device.name |
| 611 |
assert discovered_adv_data.manufacturer_data == { |
| 612 |
**sensor_push_device_adv.manufacturer_data, |
| 613 |
**sensor_push_adv_2.manufacturer_data, |
| 614 |
} |
| 615 |
assert discovered_adv_data.service_data == {} |
| 616 |
assert set(discovered_adv_data.service_uuids) == { |
| 617 |
*sensor_push_device_adv.service_uuids |
| 618 |
} |
| 619 |
|
| 620 |
cancel() |
| 621 |
unsetup() |
| 622 |
|
| 623 |
|
| 624 |
@pytest.mark.usefixtures("enable_bluetooth") |
| 625 |
@pytest.mark.asyncio |
| 626 |
async def test_merge_manufacturer_data_history_new() -> None: |
| 627 |
"""Test merging manufacturer data history.""" |
| 628 |
manager = get_manager() |
| 629 |
|
| 630 |
sensor_push_device = generate_ble_device( |
| 631 |
"44:44:33:11:23:45", |
| 632 |
"", |
| 633 |
{}, |
| 634 |
rssi=-60, |
| 635 |
) |
| 636 |
sensor_push_device_adv = generate_advertisement_data( |
| 637 |
local_name="", |
| 638 |
rssi=-60, |
| 639 |
manufacturer_data={ |
| 640 |
64256: b"B\r.\xa9\xb6", |
| 641 |
31488: b"\x98\xfa\xb6\x91\xb6", |
| 642 |
}, |
| 643 |
service_uuids=["ef090000-11d6-42ba-93b8-9dd7ec090ab0"], |
| 644 |
service_data={}, |
| 645 |
) |
| 646 |
|
| 647 |
sensor_push_adv_2 = generate_advertisement_data( |
| 648 |
local_name="", |
| 649 |
service_uuids=["ef090000-11d6-42ba-93b8-9dd7ec090ab0"], |
| 650 |
service_data={}, |
| 651 |
manufacturer_data={ |
| 652 |
21248: b"\xb9\xe9\xe1\xb9\xb6", |
| 653 |
}, |
| 654 |
rssi=-100, |
| 655 |
) |
| 656 |
|
| 657 |
connector = HaBluetoothConnector( |
| 658 |
MockBleakClient, "mock_bleak_client", lambda: False |
| 659 |
) |
| 660 |
scanner = FakeScanner("esp32", "esp32", connector, True) |
| 661 |
details = scanner.details |
| 662 |
assert details == HaScannerDetails( |
| 663 |
source=scanner.source, |
| 664 |
connectable=scanner.connectable, |
| 665 |
name=scanner.name, |
| 666 |
adapter=scanner.adapter, |
| 667 |
scanner_type=HaScannerType.REMOTE, |
| 668 |
) |
| 669 |
unsetup = scanner.async_setup() |
| 670 |
cancel = manager.async_register_scanner(scanner) |
| 671 |
|
| 672 |
scanner.inject_advertisement(sensor_push_device, sensor_push_device_adv) |
| 673 |
|
| 674 |
data = scanner.discovered_devices_and_advertisement_data |
| 675 |
discovered_device, discovered_adv_data = data[sensor_push_device.address] |
| 676 |
assert discovered_device.address == sensor_push_device.address |
| 677 |
assert discovered_device.name == sensor_push_device.name |
| 678 |
assert ( |
| 679 |
discovered_adv_data.manufacturer_data |
| 680 |
== sensor_push_device_adv.manufacturer_data |
| 681 |
) |
| 682 |
assert discovered_adv_data.service_data == sensor_push_device_adv.service_data |
| 683 |
assert discovered_adv_data.service_uuids == sensor_push_device_adv.service_uuids |
| 684 |
scanner.inject_advertisement(sensor_push_device, sensor_push_adv_2) |
| 685 |
|
| 686 |
data = scanner.discovered_devices_and_advertisement_data |
| 687 |
discovered_device, discovered_adv_data = data[sensor_push_device.address] |
| 688 |
assert discovered_device.address == sensor_push_device.address |
| 689 |
assert discovered_device.name == sensor_push_device.name |
| 690 |
assert discovered_adv_data.manufacturer_data == { |
| 691 |
**sensor_push_device_adv.manufacturer_data, |
| 692 |
**sensor_push_adv_2.manufacturer_data, |
| 693 |
} |
| 694 |
assert discovered_adv_data.service_data == {} |
| 695 |
assert set(discovered_adv_data.service_uuids) == { |
| 696 |
*sensor_push_device_adv.service_uuids |
| 697 |
} |
| 698 |
|
| 699 |
cancel() |
| 700 |
unsetup() |
| 701 |
|
| 702 |
|
| 703 |
@pytest.mark.usefixtures("enable_bluetooth") |
| 704 |
@pytest.mark.asyncio |
| 705 |
async def test_filter_apple_data() -> None: |
| 706 |
"""Test filtering apple data accepts bytes that start with 01.""" |
| 707 |
manager = get_manager() |
| 708 |
|
| 709 |
device = generate_ble_device( |
| 710 |
"44:44:33:11:23:45", |
| 711 |
"", |
| 712 |
{}, |
| 713 |
rssi=-60, |
| 714 |
) |
| 715 |
device_adv = generate_advertisement_data( |
| 716 |
local_name="", |
| 717 |
rssi=-60, |
| 718 |
manufacturer_data={ |
| 719 |
76: b"\x01\r.\xa9\xb6", |
| 720 |
}, |
| 721 |
service_uuids=["ef090000-11d6-42ba-93b8-9dd7ec090ab0"], |
| 722 |
service_data={}, |
| 723 |
) |
| 724 |
|
| 725 |
connector = HaBluetoothConnector( |
| 726 |
MockBleakClient, "mock_bleak_client", lambda: False |
| 727 |
) |
| 728 |
scanner = FakeScanner("esp32", "esp32", connector, True) |
| 729 |
details = scanner.details |
| 730 |
assert details == HaScannerDetails( |
| 731 |
source=scanner.source, |
| 732 |
connectable=scanner.connectable, |
| 733 |
name=scanner.name, |
| 734 |
adapter=scanner.adapter, |
| 735 |
scanner_type=HaScannerType.REMOTE, |
| 736 |
) |
| 737 |
unsetup = scanner.async_setup() |
| 738 |
cancel = manager.async_register_scanner(scanner) |
| 739 |
|
| 740 |
scanner.inject_advertisement(device, device_adv) |
| 741 |
|
| 742 |
data = scanner.discovered_devices_and_advertisement_data |
| 743 |
discovered_device, discovered_adv_data = data[device.address] |
| 744 |
assert discovered_device.address == device.address |
| 745 |
assert discovered_device.name == device.name |
| 746 |
assert discovered_adv_data.manufacturer_data == device_adv.manufacturer_data |
| 747 |
unsetup() |
| 748 |
cancel() |
| 749 |
|
| 750 |
|
| 751 |
@pytest.mark.usefixtures("register_hci0_scanner") |
| 752 |
def test_connection_history_count_in_progress() -> None: |
| 753 |
"""Test connection history in process counting.""" |
| 754 |
manager = get_manager() |
| 755 |
device1_address = "44:44:33:11:23:12" |
| 756 |
device2_address = "44:44:33:11:23:13" |
| 757 |
hci0_scanner = manager.async_scanner_by_source(HCI0_SOURCE_ADDRESS) |
| 758 |
assert hci0_scanner is not None |
| 759 |
hci0_scanner._add_connecting(device1_address) |
| 760 |
assert hci0_scanner._connections_in_progress() == 1 |
| 761 |
hci0_scanner._add_connecting(device1_address) |
| 762 |
hci0_scanner._add_connecting(device2_address) |
| 763 |
assert hci0_scanner._connections_in_progress() == 3 |
| 764 |
hci0_scanner._finished_connecting(device1_address, True) |
| 765 |
assert hci0_scanner._connections_in_progress() == 2 |
| 766 |
hci0_scanner._finished_connecting(device1_address, False) |
| 767 |
assert hci0_scanner._connections_in_progress() == 1 |
| 768 |
hci0_scanner._finished_connecting(device2_address, False) |
| 769 |
assert hci0_scanner._connections_in_progress() == 0 |
| 770 |
|
| 771 |
|
| 772 |
@pytest.mark.usefixtures("register_hci0_scanner") |
| 773 |
def test_connection_history_failure_count(caplog: pytest.LogCaptureFixture) -> None: |
| 774 |
"""Test connection history failure count.""" |
| 775 |
manager = get_manager() |
| 776 |
device1_address = "44:44:33:11:23:12" |
| 777 |
device2_address = "44:44:33:11:23:13" |
| 778 |
hci0_scanner = manager.async_scanner_by_source(HCI0_SOURCE_ADDRESS) |
| 779 |
assert hci0_scanner is not None |
| 780 |
hci0_scanner._add_connecting(device1_address) |
| 781 |
hci0_scanner._finished_connecting(device1_address, False) |
| 782 |
assert hci0_scanner._connection_failures(device1_address) == 1 |
| 783 |
hci0_scanner._add_connecting(device1_address) |
| 784 |
hci0_scanner._add_connecting(device2_address) |
| 785 |
hci0_scanner._finished_connecting(device1_address, False) |
| 786 |
assert hci0_scanner._connection_failures(device1_address) == 2 |
| 787 |
hci0_scanner._finished_connecting(device2_address, False) |
| 788 |
assert hci0_scanner._connection_failures(device2_address) == 1 |
| 789 |
hci0_scanner._add_connecting(device1_address) |
| 790 |
hci0_scanner._finished_connecting(device1_address, True) |
| 791 |
# On success, we should reset the failure count |
| 792 |
assert hci0_scanner._connection_failures(device1_address) == 0 |
| 793 |
|
| 794 |
assert "Removing a non-existing connecting" not in caplog.text |
| 795 |
hci0_scanner._finished_connecting(device1_address, True) |
| 796 |
assert "Removing a non-existing connecting" in caplog.text |
| 797 |
|
| 798 |
|
| 799 |
@pytest.mark.usefixtures("enable_bluetooth") |
| 800 |
@pytest.mark.asyncio |
| 801 |
async def test_scanner_mode_changes() -> None: |
| 802 |
"""Test scanner mode change methods notify the manager.""" |
| 803 |
manager = get_manager() |
| 804 |
|
| 805 |
# Track mode changes |
| 806 |
mode_changes: list[HaScannerModeChange] = [] |
| 807 |
|
| 808 |
def mode_callback(change: HaScannerModeChange) -> None: |
| 809 |
"""Track mode changes.""" |
| 810 |
mode_changes.append(change) |
| 811 |
|
| 812 |
cancel = manager.async_register_scanner_mode_change_callback(mode_callback, None) |
| 813 |
|
| 814 |
# Create a scanner with initial modes |
| 815 |
scanner = FakeScanner( |
| 816 |
HCI0_SOURCE_ADDRESS, |
| 817 |
"hci0", |
| 818 |
connectable=True, |
| 819 |
requested_mode=BluetoothScanningMode.PASSIVE, |
| 820 |
current_mode=BluetoothScanningMode.PASSIVE, |
| 821 |
) |
| 822 |
|
| 823 |
# Set up the scanner |
| 824 |
unsetup = scanner.async_setup() |
| 825 |
|
| 826 |
# Test changing requested mode |
| 827 |
scanner.set_requested_mode(BluetoothScanningMode.ACTIVE) |
| 828 |
assert len(mode_changes) == 1 |
| 829 |
assert mode_changes[0].scanner == scanner |
| 830 |
assert mode_changes[0].requested_mode == BluetoothScanningMode.ACTIVE |
| 831 |
assert mode_changes[0].current_mode == BluetoothScanningMode.PASSIVE |
| 832 |
assert scanner.requested_mode == BluetoothScanningMode.ACTIVE |
| 833 |
|
| 834 |
# Test changing current mode |
| 835 |
scanner.set_current_mode(BluetoothScanningMode.ACTIVE) |
| 836 |
assert len(mode_changes) == 2 |
| 837 |
assert mode_changes[1].scanner == scanner |
| 838 |
assert mode_changes[1].requested_mode == BluetoothScanningMode.ACTIVE |
| 839 |
assert mode_changes[1].current_mode == BluetoothScanningMode.ACTIVE |
| 840 |
assert scanner.current_mode == BluetoothScanningMode.ACTIVE |
| 841 |
|
| 842 |
# Test no notification when mode doesn't change |
| 843 |
scanner.set_current_mode(BluetoothScanningMode.ACTIVE) |
| 844 |
assert len(mode_changes) == 2 # No new notification |
| 845 |
|
| 846 |
# Test setting to None |
| 847 |
scanner.set_requested_mode(None) |
| 848 |
assert len(mode_changes) == 3 |
| 849 |
assert mode_changes[2].requested_mode is None |
| 850 |
assert scanner.requested_mode is None |
| 851 |
|
| 852 |
scanner.set_current_mode(None) # type: ignore[unreachable] |
| 853 |
assert len(mode_changes) == 4 |
| 854 |
assert mode_changes[3].current_mode is None |
| 855 |
assert scanner.current_mode is None |
| 856 |
|
| 857 |
# Clean up |
| 858 |
unsetup() |
| 859 |
cancel() |
| 860 |
|
| 861 |
|
| 862 |
def test_remote_scanner_type() -> None: |
| 863 |
"""Test that remote scanners have REMOTE type.""" |
| 864 |
|
| 865 |
class TestRemoteScanner(BaseHaRemoteScanner): |
| 866 |
"""Test remote scanner implementation.""" |
| 867 |
|
| 868 |
scanner = TestRemoteScanner("test_source", "test_adapter") |
| 869 |
assert scanner.details.scanner_type is HaScannerType.REMOTE |
| 870 |
|
| 871 |
|
| 872 |
def test_base_scanner_with_connector() -> None: |
| 873 |
"""Test BaseHaScanner with connector and adapter type.""" |
| 874 |
manager = get_manager() |
| 875 |
|
| 876 |
mock_adapters: dict[str, dict[str, Any]] = { |
| 877 |
"test_adapter": { |
| 878 |
"address": "00:1A:7D:DA:71:04", |
| 879 |
"adapter_type": "usb", |
| 880 |
} |
| 881 |
} |
| 882 |
|
| 883 |
connector = HaBluetoothConnector( |
| 884 |
client=MagicMock, source="test_source", can_connect=lambda: True |
| 885 |
) |
| 886 |
|
| 887 |
with patch.object(manager, "_adapters", mock_adapters): |
| 888 |
scanner = BaseHaScanner( |
| 889 |
source="test_source", |
| 890 |
adapter="test_adapter", |
| 891 |
connector=connector, |
| 892 |
connectable=True, |
| 893 |
) |
| 894 |
assert scanner.details.scanner_type is HaScannerType.USB |
| 895 |
|
| 896 |
|
| 897 |
class TestScanner(BaseHaScanner): |
| 898 |
"""Test scanner without slots for mocking.""" |
| 899 |
|
| 900 |
__test__ = False |
| 901 |
|
| 902 |
def __init__(self, *args, **kwargs): |
| 903 |
super().__init__(*args, **kwargs) |
| 904 |
self._test_allocations = None |
| 905 |
|
| 906 |
def get_allocations(self): |
| 907 |
"""Override to return test allocations.""" |
| 908 |
return self._test_allocations |
| 909 |
|
| 910 |
|
| 911 |
def test_score_with_no_allocations(): |
| 912 |
"""Test scoring when no allocation info is available.""" |
| 913 |
scanner = TestScanner( |
| 914 |
source="test_source", |
| 915 |
adapter="test_adapter", |
| 916 |
connectable=True, |
| 917 |
) |
| 918 |
|
| 919 |
ble_device = BLEDevice( |
| 920 |
address="00:11:22:33:44:55", |
| 921 |
name="Test Device", |
| 922 |
details={}, |
| 923 |
) |
| 924 |
advertisement = AdvertisementData( |
| 925 |
local_name="Test Device", |
| 926 |
manufacturer_data={}, |
| 927 |
service_data={}, |
| 928 |
service_uuids=[], |
| 929 |
rssi=-50, |
| 930 |
platform_data=(), |
| 931 |
tx_power=None, |
| 932 |
) |
| 933 |
scanner_device = BluetoothScannerDevice( |
| 934 |
scanner=scanner, |
| 935 |
ble_device=ble_device, |
| 936 |
advertisement=advertisement, |
| 937 |
) |
| 938 |
|
| 939 |
# No allocation info available, should just use RSSI |
| 940 |
score = scanner._score_connection_paths(10, scanner_device) |
| 941 |
assert score == -50 |
| 942 |
|
| 943 |
|
| 944 |
def test_score_with_all_slots_free(): |
| 945 |
"""Test scoring when all slots are free.""" |
| 946 |
scanner = TestScanner( |
| 947 |
source="test_source", |
| 948 |
adapter="test_adapter", |
| 949 |
connectable=True, |
| 950 |
) |
| 951 |
|
| 952 |
# Set test allocations to return all slots free |
| 953 |
scanner._test_allocations = Allocations( |
| 954 |
adapter="test_adapter", |
| 955 |
slots=5, |
| 956 |
free=5, |
| 957 |
allocated=[], |
| 958 |
) |
| 959 |
|
| 960 |
ble_device = BLEDevice( |
| 961 |
address="00:11:22:33:44:55", |
| 962 |
name="Test Device", |
| 963 |
details={}, |
| 964 |
) |
| 965 |
advertisement = AdvertisementData( |
| 966 |
local_name="Test Device", |
| 967 |
manufacturer_data={}, |
| 968 |
service_data={}, |
| 969 |
service_uuids=[], |
| 970 |
rssi=-50, |
| 971 |
platform_data=(), |
| 972 |
tx_power=None, |
| 973 |
) |
| 974 |
scanner_device = BluetoothScannerDevice( |
| 975 |
scanner=scanner, |
| 976 |
ble_device=ble_device, |
| 977 |
advertisement=advertisement, |
| 978 |
) |
| 979 |
|
| 980 |
# All slots free, no penalty |
| 981 |
score = scanner._score_connection_paths(10, scanner_device) |
| 982 |
assert score == -50 |
| 983 |
|
| 984 |
|
| 985 |
def test_score_with_one_slot_remaining(): |
| 986 |
"""Test scoring when only one slot remains.""" |
| 987 |
scanner = TestScanner( |
| 988 |
source="test_source", |
| 989 |
adapter="test_adapter", |
| 990 |
connectable=True, |
| 991 |
) |
| 992 |
|
| 993 |
# Set test allocations to return only 1 slot free |
| 994 |
scanner._test_allocations = Allocations( |
| 995 |
adapter="test_adapter", |
| 996 |
slots=5, |
| 997 |
free=1, |
| 998 |
allocated=[ |
| 999 |
"AA:BB:CC:DD:EE:01", |
| 1000 |
"AA:BB:CC:DD:EE:02", |
| 1001 |
"AA:BB:CC:DD:EE:03", |
| 1002 |
"AA:BB:CC:DD:EE:04", |
| 1003 |
], |
| 1004 |
) |
| 1005 |
|
| 1006 |
ble_device = BLEDevice( |
| 1007 |
address="00:11:22:33:44:55", |
| 1008 |
name="Test Device", |
| 1009 |
details={}, |
| 1010 |
) |
| 1011 |
advertisement = AdvertisementData( |
| 1012 |
local_name="Test Device", |
| 1013 |
manufacturer_data={}, |
| 1014 |
service_data={}, |
| 1015 |
service_uuids=[], |
| 1016 |
rssi=-50, |
| 1017 |
platform_data=(), |
| 1018 |
tx_power=None, |
| 1019 |
) |
| 1020 |
scanner_device = BluetoothScannerDevice( |
| 1021 |
scanner=scanner, |
| 1022 |
ble_device=ble_device, |
| 1023 |
advertisement=advertisement, |
| 1024 |
) |
| 1025 |
|
| 1026 |
# One slot remaining, small penalty (rssi_diff * 0.76) |
| 1027 |
score = scanner._score_connection_paths(10, scanner_device) |
| 1028 |
assert score == -50 - (10 * 0.76) |
| 1029 |
assert score == -57.6 |
| 1030 |
|
| 1031 |
|
| 1032 |
def test_score_with_no_slots_available(): |
| 1033 |
"""Test scoring when no slots are available.""" |
| 1034 |
scanner = TestScanner( |
| 1035 |
source="test_source", |
| 1036 |
adapter="test_adapter", |
| 1037 |
connectable=True, |
| 1038 |
) |
| 1039 |
|
| 1040 |
# Set test allocations to return no slots free |
| 1041 |
scanner._test_allocations = Allocations( |
| 1042 |
adapter="test_adapter", |
| 1043 |
slots=5, |
| 1044 |
free=0, |
| 1045 |
allocated=[ |
| 1046 |
"AA:BB:CC:DD:EE:01", |
| 1047 |
"AA:BB:CC:DD:EE:02", |
| 1048 |
"AA:BB:CC:DD:EE:03", |
| 1049 |
"AA:BB:CC:DD:EE:04", |
| 1050 |
"AA:BB:CC:DD:EE:05", |
| 1051 |
], |
| 1052 |
) |
| 1053 |
|
| 1054 |
ble_device = BLEDevice( |
| 1055 |
address="00:11:22:33:44:55", |
| 1056 |
name="Test Device", |
| 1057 |
details={}, |
| 1058 |
) |
| 1059 |
advertisement = AdvertisementData( |
| 1060 |
local_name="Test Device", |
| 1061 |
manufacturer_data={}, |
| 1062 |
service_data={}, |
| 1063 |
service_uuids=[], |
| 1064 |
rssi=-50, |
| 1065 |
platform_data=(), |
| 1066 |
tx_power=None, |
| 1067 |
) |
| 1068 |
scanner_device = BluetoothScannerDevice( |
| 1069 |
scanner=scanner, |
| 1070 |
ble_device=ble_device, |
| 1071 |
advertisement=advertisement, |
| 1072 |
) |
| 1073 |
|
| 1074 |
# No slots available, returns NO_RSSI_VALUE (-127) |
| 1075 |
score = scanner._score_connection_paths(10, scanner_device) |
| 1076 |
assert score == -127 |
| 1077 |
|
| 1078 |
|
| 1079 |
def test_score_comparison_with_different_slot_availability(): |
| 1080 |
"""Test that scanners with more free slots score better.""" |
| 1081 |
# Scanner with all slots free |
| 1082 |
scanner_all_free = TestScanner( |
| 1083 |
source="adapter1", |
| 1084 |
adapter="adapter1", |
| 1085 |
connectable=True, |
| 1086 |
) |
| 1087 |
|
| 1088 |
# Scanner with one slot remaining |
| 1089 |
scanner_one_slot = TestScanner( |
| 1090 |
source="adapter2", |
| 1091 |
adapter="adapter2", |
| 1092 |
connectable=True, |
| 1093 |
) |
| 1094 |
|
| 1095 |
# Scanner with no slots |
| 1096 |
scanner_no_slots = TestScanner( |
| 1097 |
source="adapter3", |
| 1098 |
adapter="adapter3", |
| 1099 |
connectable=True, |
| 1100 |
) |
| 1101 |
|
| 1102 |
ble_device = BLEDevice( |
| 1103 |
address="00:11:22:33:44:55", |
| 1104 |
name="Test Device", |
| 1105 |
details={}, |
| 1106 |
) |
| 1107 |
advertisement = AdvertisementData( |
| 1108 |
local_name="Test Device", |
| 1109 |
manufacturer_data={}, |
| 1110 |
service_data={}, |
| 1111 |
service_uuids=[], |
| 1112 |
rssi=-50, |
| 1113 |
platform_data=(), |
| 1114 |
tx_power=None, |
| 1115 |
) |
| 1116 |
|
| 1117 |
# Create scanner devices for each scanner |
| 1118 |
device_all_free = BluetoothScannerDevice( |
| 1119 |
scanner=scanner_all_free, |
| 1120 |
ble_device=ble_device, |
| 1121 |
advertisement=advertisement, |
| 1122 |
) |
| 1123 |
device_one_slot = BluetoothScannerDevice( |
| 1124 |
scanner=scanner_one_slot, |
| 1125 |
ble_device=ble_device, |
| 1126 |
advertisement=advertisement, |
| 1127 |
) |
| 1128 |
device_no_slots = BluetoothScannerDevice( |
| 1129 |
scanner=scanner_no_slots, |
| 1130 |
ble_device=ble_device, |
| 1131 |
advertisement=advertisement, |
| 1132 |
) |
| 1133 |
|
| 1134 |
# Set allocations for each scanner |
| 1135 |
scanner_all_free._test_allocations = Allocations( |
| 1136 |
adapter="adapter1", |
| 1137 |
slots=5, |
| 1138 |
free=5, |
| 1139 |
allocated=[], |
| 1140 |
) |
| 1141 |
scanner_one_slot._test_allocations = Allocations( |
| 1142 |
adapter="adapter2", |
| 1143 |
slots=5, |
| 1144 |
free=1, |
| 1145 |
allocated=[ |
| 1146 |
"AA:BB:CC:DD:EE:01", |
| 1147 |
"AA:BB:CC:DD:EE:02", |
| 1148 |
"AA:BB:CC:DD:EE:03", |
| 1149 |
"AA:BB:CC:DD:EE:04", |
| 1150 |
], |
| 1151 |
) |
| 1152 |
scanner_no_slots._test_allocations = Allocations( |
| 1153 |
adapter="adapter3", |
| 1154 |
slots=5, |
| 1155 |
free=0, |
| 1156 |
allocated=[ |
| 1157 |
"AA:BB:CC:DD:EE:01", |
| 1158 |
"AA:BB:CC:DD:EE:02", |
| 1159 |
"AA:BB:CC:DD:EE:03", |
| 1160 |
"AA:BB:CC:DD:EE:04", |
| 1161 |
"AA:BB:CC:DD:EE:05", |
| 1162 |
], |
| 1163 |
) |
| 1164 |
|
| 1165 |
score_all_free = scanner_all_free._score_connection_paths(10, device_all_free) |
| 1166 |
score_one_slot = scanner_one_slot._score_connection_paths(10, device_one_slot) |
| 1167 |
score_no_slots = scanner_no_slots._score_connection_paths(10, device_no_slots) |
| 1168 |
|
| 1169 |
# Verify the scoring order: all_free > one_slot > no_slots |
| 1170 |
assert score_all_free > score_one_slot |
| 1171 |
assert score_one_slot > score_no_slots |
| 1172 |
|
| 1173 |
# Verify specific values |
| 1174 |
assert score_all_free == -50 |
| 1175 |
assert score_one_slot == -57.6 |
| 1176 |
assert score_no_slots == -127 # NO_RSSI_VALUE |
| 1177 |
|
| 1178 |
|
| 1179 |
def test_score_with_connections_in_progress_and_slots(): |
| 1180 |
"""Test that both connection progress and slot availability are considered.""" |
| 1181 |
scanner = TestScanner( |
| 1182 |
source="test_source", |
| 1183 |
adapter="test_adapter", |
| 1184 |
connectable=True, |
| 1185 |
) |
| 1186 |
|
| 1187 |
# Add a connection in progress |
| 1188 |
scanner._add_connecting("FF:EE:DD:CC:BB:AA") |
| 1189 |
|
| 1190 |
# Set test allocations to return only 1 slot free |
| 1191 |
scanner._test_allocations = Allocations( |
| 1192 |
adapter="test_adapter", |
| 1193 |
slots=5, |
| 1194 |
free=1, |
| 1195 |
allocated=[ |
| 1196 |
"AA:BB:CC:DD:EE:01", |
| 1197 |
"AA:BB:CC:DD:EE:02", |
| 1198 |
"AA:BB:CC:DD:EE:03", |
| 1199 |
"AA:BB:CC:DD:EE:04", |
| 1200 |
], |
| 1201 |
) |
| 1202 |
|
| 1203 |
ble_device = BLEDevice( |
| 1204 |
address="00:11:22:33:44:55", |
| 1205 |
name="Test Device", |
| 1206 |
details={}, |
| 1207 |
) |
| 1208 |
advertisement = AdvertisementData( |
| 1209 |
local_name="Test Device", |
| 1210 |
manufacturer_data={}, |
| 1211 |
service_data={}, |
| 1212 |
service_uuids=[], |
| 1213 |
rssi=-50, |
| 1214 |
platform_data=(), |
| 1215 |
tx_power=None, |
| 1216 |
) |
| 1217 |
scanner_device = BluetoothScannerDevice( |
| 1218 |
scanner=scanner, |
| 1219 |
ble_device=ble_device, |
| 1220 |
advertisement=advertisement, |
| 1221 |
) |
| 1222 |
|
| 1223 |
# Both penalties should apply |
| 1224 |
score = scanner._score_connection_paths(10, scanner_device) |
| 1225 |
# -50 (RSSI) - 10.1 (connection in progress) - 7.6 (last slot) |
| 1226 |
assert score == -50 - 10.1 - 7.6 |
| 1227 |
assert score == -67.7 |
| 1228 |
|
| 1229 |
|
| 1230 |
@pytest.mark.asyncio |
| 1231 |
async def test_on_scanner_start_callback_remote_scanner( |
| 1232 |
async_mock_manager_with_scanner_callbacks: MockBluetoothManagerWithCallbacks, |
| 1233 |
) -> None: |
| 1234 |
"""Test that on_scanner_start is called when a remote scanner starts.""" |
| 1235 |
manager = async_mock_manager_with_scanner_callbacks |
| 1236 |
|
| 1237 |
# Create a fake remote scanner |
| 1238 |
scanner = FakeScanner( |
| 1239 |
source="esp32_proxy", |
| 1240 |
adapter="esp32_proxy", |
| 1241 |
connector=None, |
| 1242 |
connectable=True, |
| 1243 |
) |
| 1244 |
|
| 1245 |
# Simulate scanner start success |
| 1246 |
scanner._on_start_success() |
| 1247 |
|
| 1248 |
# Verify the callback was called |
| 1249 |
assert len(manager.scanner_start_calls) == 1 |
| 1250 |
assert manager.scanner_start_calls[0] is scanner |
| 1251 |
|
| 1252 |
|
| 1253 |
@pytest.mark.asyncio |
| 1254 |
async def test_on_scanner_start_multiple_scanners( |
| 1255 |
async_mock_manager_with_scanner_callbacks: MockBluetoothManagerWithCallbacks, |
| 1256 |
) -> None: |
| 1257 |
"""Test that on_scanner_start is called for multiple scanners.""" |
| 1258 |
manager = async_mock_manager_with_scanner_callbacks |
| 1259 |
|
| 1260 |
# Create multiple scanners |
| 1261 |
scanner1 = FakeScanner( |
| 1262 |
source="scanner1", |
| 1263 |
adapter="scanner1", |
| 1264 |
connector=None, |
| 1265 |
connectable=True, |
| 1266 |
) |
| 1267 |
|
| 1268 |
scanner2 = FakeScanner( |
| 1269 |
source="scanner2", |
| 1270 |
adapter="scanner2", |
| 1271 |
connector=None, |
| 1272 |
connectable=True, |
| 1273 |
) |
| 1274 |
|
| 1275 |
# Simulate both scanners starting |
| 1276 |
scanner1._on_start_success() |
| 1277 |
scanner2._on_start_success() |
| 1278 |
|
| 1279 |
# Verify both callbacks were called |
| 1280 |
assert len(manager.scanner_start_calls) == 2 |
| 1281 |
assert scanner1 in manager.scanner_start_calls |
| 1282 |
assert scanner2 in manager.scanner_start_calls |
| 1283 |
|
| 1284 |
|
| 1285 |
@pytest.mark.asyncio |
| 1286 |
async def test_scanner_without_manager() -> None: |
| 1287 |
"""Test that _on_start_success handles scanner without manager gracefully.""" |
| 1288 |
# Set a temporary manager for scanner creation |
| 1289 |
mock_bluetooth_adapters = FakeBluetoothAdapters() |
| 1290 |
temp_manager = BluetoothManager( |
| 1291 |
mock_bluetooth_adapters, |
| 1292 |
slot_manager=Mock(), |
| 1293 |
) |
| 1294 |
|
| 1295 |
original_manager = get_manager() |
| 1296 |
set_manager(temp_manager) |
| 1297 |
|
| 1298 |
try: |
| 1299 |
# Create a scanner |
| 1300 |
scanner = FakeScanner( |
| 1301 |
source="test", |
| 1302 |
adapter="test", |
| 1303 |
connector=None, |
| 1304 |
connectable=True, |
| 1305 |
) |
| 1306 |
|
| 1307 |
# Clear the manager to simulate no manager scenario |
| 1308 |
scanner._manager = None # type: ignore[assignment] |
| 1309 |
|
| 1310 |
# Should not raise an exception |
| 1311 |
scanner._on_start_success() |
| 1312 |
|
| 1313 |
finally: |
| 1314 |
# Restore original manager |
| 1315 |
set_manager(original_manager) |
| 1316 |
|
| 1317 |
|
| 1318 |
@pytest.mark.usefixtures("enable_bluetooth") |
| 1319 |
@pytest.mark.asyncio |
| 1320 |
async def test_remote_scanner_restore_discovered_devices() -> None: |
| 1321 |
"""Test serialize→restore round-trip on a BaseHaRemoteScanner.""" |
| 1322 |
manager = get_manager() |
| 1323 |
connector = HaBluetoothConnector( |
| 1324 |
MockBleakClient, "mock_bleak_client", lambda: False |
| 1325 |
) |
| 1326 |
|
| 1327 |
# First scanner: receives advertisements, then serializes. |
| 1328 |
src_scanner = FakeScanner("esp32", "esp32", connector, True) |
| 1329 |
src_unsetup = src_scanner.async_setup() |
| 1330 |
src_cancel = manager.async_register_scanner(src_scanner) |
| 1331 |
|
| 1332 |
address = "44:44:33:11:23:45" |
| 1333 |
device = generate_ble_device(address, "wohand", {}) |
| 1334 |
adv = generate_advertisement_data( |
| 1335 |
local_name="wohand", |
| 1336 |
service_uuids=["050a021a-0000-1000-8000-00805f9b34fb"], |
| 1337 |
service_data={"050a021a-0000-1000-8000-00805f9b34fb": b"\n\xff"}, |
| 1338 |
manufacturer_data={1: b"\x01"}, |
| 1339 |
rssi=-60, |
| 1340 |
tx_power=4, |
| 1341 |
) |
| 1342 |
now = monotonic_time_coarse() |
| 1343 |
src_scanner.inject_advertisement(device, adv, now=now) |
| 1344 |
raw_adv = b"\x12\x21\x1a\x02\n\x05\n\xff\x062k\x03R\x00\x01\x04\t\x00\x04" |
| 1345 |
src_scanner.inject_raw_advertisement(address, -55, raw_adv, now=now + 1.0) |
| 1346 |
|
| 1347 |
history = src_scanner.serialize_discovered_devices() |
| 1348 |
assert history.discovered_device_raw[address] == raw_adv |
| 1349 |
assert history.discovered_device_timestamps[address] == now + 1.0 |
| 1350 |
|
| 1351 |
# Second scanner: fresh instance, restore from serialized blob. |
| 1352 |
dst_scanner = FakeScanner("esp32-replay", "esp32-replay", connector, True) |
| 1353 |
dst_unsetup = dst_scanner.async_setup() |
| 1354 |
dst_cancel = manager.async_register_scanner(dst_scanner) |
| 1355 |
|
| 1356 |
assert dst_scanner.discovered_devices == [] |
| 1357 |
dst_scanner.restore_discovered_devices(history) |
| 1358 |
|
| 1359 |
restored = dst_scanner.discovered_devices_and_advertisement_data |
| 1360 |
assert set(restored) == {address} |
| 1361 |
rest_device, rest_adv = restored[address] |
| 1362 |
assert rest_device.address == address |
| 1363 |
assert rest_device.name == "wohand" |
| 1364 |
assert rest_adv.manufacturer_data == adv.manufacturer_data |
| 1365 |
assert rest_adv.service_uuids == adv.service_uuids |
| 1366 |
# Restored source overrides the previous scanner's source — the device |
| 1367 |
# belongs to the scanner that owns it now. |
| 1368 |
assert dst_scanner._previous_service_info[address].source == "esp32-replay" |
| 1369 |
# Raw advertisement bytes survive the round-trip. |
| 1370 |
assert dst_scanner._previous_service_info[address].raw == raw_adv |
| 1371 |
|
| 1372 |
src_cancel() |
| 1373 |
src_unsetup() |
| 1374 |
dst_cancel() |
| 1375 |
dst_unsetup() |
| 1376 |
|
| 1377 |
|
| 1378 |
@pytest.mark.usefixtures("enable_bluetooth") |
| 1379 |
@pytest.mark.asyncio |
| 1380 |
async def test_remote_scanner_async_diagnostics() -> None: |
| 1381 |
"""Test BaseHaRemoteScanner.async_diagnostics shape.""" |
| 1382 |
manager = get_manager() |
| 1383 |
connector = HaBluetoothConnector( |
| 1384 |
MockBleakClient, "mock_bleak_client", lambda: False |
| 1385 |
) |
| 1386 |
scanner = FakeScanner("esp32", "esp32", connector, True) |
| 1387 |
unsetup = scanner.async_setup() |
| 1388 |
cancel = manager.async_register_scanner(scanner) |
| 1389 |
|
| 1390 |
address = "44:44:33:11:23:45" |
| 1391 |
device = generate_ble_device(address, "wohand", {}) |
| 1392 |
adv = generate_advertisement_data( |
| 1393 |
local_name="wohand", |
| 1394 |
service_uuids=[], |
| 1395 |
service_data={}, |
| 1396 |
manufacturer_data={1: b"\x01"}, |
| 1397 |
rssi=-60, |
| 1398 |
) |
| 1399 |
now = monotonic_time_coarse() |
| 1400 |
scanner.inject_advertisement(device, adv, now=now) |
| 1401 |
raw = b"\x02\x01\x06" |
| 1402 |
scanner.inject_raw_advertisement(address, -55, raw, now=now + 1.0) |
| 1403 |
|
| 1404 |
diagnostics = await scanner.async_diagnostics() |
| 1405 |
# Remote scanner adds these three keys on top of the base diagnostics. |
| 1406 |
assert diagnostics["raw_advertisement_data"] == {address: raw} |
| 1407 |
assert diagnostics["discovered_device_timestamps"] == {address: now + 1.0} |
| 1408 |
assert set(diagnostics["time_since_last_device_detection"]) == {address} |
| 1409 |
# Base keys still present. |
| 1410 |
assert diagnostics["source"] == "esp32" |
| 1411 |
assert diagnostics["connectable"] is True |
| 1412 |
# Connection counters start empty. |
| 1413 |
assert diagnostics["connect_in_progress"] == {} |
| 1414 |
assert diagnostics["connect_failures"] == {} |
| 1415 |
|
| 1416 |
cancel() |
| 1417 |
unsetup() |
| 1418 |
|
| 1419 |
|
| 1420 |
@pytest.mark.usefixtures("enable_bluetooth") |
| 1421 |
@pytest.mark.asyncio |
| 1422 |
async def test_diagnostics_surface_connect_state() -> None: |
| 1423 |
"""async_diagnostics should expose connect-in-progress and failure counts.""" |
| 1424 |
manager = get_manager() |
| 1425 |
scanner = FakeScanner( |
| 1426 |
source="diag", adapter="diag", connector=None, connectable=True |
| 1427 |
) |
| 1428 |
unsetup = scanner.async_setup() |
| 1429 |
cancel = manager.async_register_scanner(scanner) |
| 1430 |
|
| 1431 |
addr_a = "AA:BB:CC:DD:EE:01" |
| 1432 |
addr_b = "AA:BB:CC:DD:EE:02" |
| 1433 |
|
| 1434 |
# Two attempts in flight for addr_a, one for addr_b. |
| 1435 |
scanner._add_connecting(addr_a) |
| 1436 |
scanner._add_connecting(addr_a) |
| 1437 |
scanner._add_connecting(addr_b) |
| 1438 |
|
| 1439 |
# addr_a then succeeds (clears its failures), addr_b fails twice. |
| 1440 |
scanner._finished_connecting(addr_a, connected=True) |
| 1441 |
scanner._finished_connecting(addr_b, connected=False) |
| 1442 |
scanner._add_connecting(addr_b) |
| 1443 |
scanner._finished_connecting(addr_b, connected=False) |
| 1444 |
|
| 1445 |
diagnostics = await scanner.async_diagnostics() |
| 1446 |
# addr_a still has one in-flight attempt, addr_b is fully drained. |
| 1447 |
assert diagnostics["connect_in_progress"] == {addr_a: 1} |
| 1448 |
assert diagnostics["connect_failures"] == {addr_b: 2} |
| 1449 |
# Lifetime counters: one success (addr_a), two failures (addr_b twice). |
| 1450 |
assert diagnostics["connect_completed_total"] == 1 |
| 1451 |
assert diagnostics["connect_failed_total"] == 2 |
| 1452 |
assert diagnostics["last_connect_completed_time"] > 0.0 |
| 1453 |
# Returned dicts must be copies — mutating them doesn't poison scanner state. |
| 1454 |
diagnostics["connect_in_progress"].clear() |
| 1455 |
diagnostics["connect_failures"].clear() |
| 1456 |
assert scanner._connect_in_progress == {addr_a: 1} |
| 1457 |
assert scanner._connect_failures == {addr_b: 2} |
| 1458 |
|
| 1459 |
cancel() |
| 1460 |
unsetup() |
| 1461 |
|
| 1462 |
|
| 1463 |
@pytest.mark.usefixtures("enable_bluetooth") |
| 1464 |
@pytest.mark.asyncio |
| 1465 |
async def test_lifetime_connect_counters_reset_on_history_clear() -> None: |
| 1466 |
"""Lifetime counters should reset whenever connection history is cleared.""" |
| 1467 |
manager = get_manager() |
| 1468 |
scanner = FakeScanner( |
| 1469 |
source="counters", adapter="counters", connector=None, connectable=True |
| 1470 |
) |
| 1471 |
unsetup = scanner.async_setup() |
| 1472 |
cancel = manager.async_register_scanner(scanner) |
| 1473 |
|
| 1474 |
addr = "AA:BB:CC:DD:EE:99" |
| 1475 |
# Three successes, one failure → totals reflect lifetime, not per-address state. |
| 1476 |
for _ in range(3): |
| 1477 |
scanner._add_connecting(addr) |
| 1478 |
scanner._finished_connecting(addr, connected=True) |
| 1479 |
scanner._add_connecting(addr) |
| 1480 |
scanner._finished_connecting(addr, connected=False) |
| 1481 |
|
| 1482 |
assert scanner._connect_completed_total == 3 |
| 1483 |
assert scanner._connect_failed_total == 1 |
| 1484 |
assert scanner._last_connect_completed_time > 0.0 |
| 1485 |
# Per-address failure count survives because success preceded the last failure. |
| 1486 |
assert scanner._connect_failures == {addr: 1} |
| 1487 |
|
| 1488 |
scanner._clear_connection_history() |
| 1489 |
assert scanner._connect_completed_total == 0 |
| 1490 |
assert scanner._connect_failed_total == 0 |
| 1491 |
assert scanner._last_connect_completed_time == 0.0 |
| 1492 |
assert scanner._connect_failures == {} |
| 1493 |
assert scanner._connect_in_progress == {} |
| 1494 |
|
| 1495 |
cancel() |
| 1496 |
unsetup() |
| 1497 |
|
| 1498 |
|
| 1499 |
@pytest.mark.usefixtures("enable_bluetooth") |
| 1500 |
@pytest.mark.asyncio |
| 1501 |
async def test_set_mode_noop_when_unchanged() -> None: |
| 1502 |
"""set_requested_mode and set_current_mode should be no-ops on same value.""" |
| 1503 |
manager = get_manager() |
| 1504 |
scanner = FakeScanner( |
| 1505 |
source="modes", |
| 1506 |
adapter="modes", |
| 1507 |
connector=None, |
| 1508 |
connectable=True, |
| 1509 |
requested_mode=BluetoothScanningMode.ACTIVE, |
| 1510 |
) |
| 1511 |
with patch.object(manager, "scanner_mode_changed") as notify: |
| 1512 |
# Same value → setter is a no-op, no notify. |
| 1513 |
scanner.set_requested_mode(BluetoothScanningMode.ACTIVE) |
| 1514 |
notify.assert_not_called() |
| 1515 |
|
| 1516 |
# Different value → notify fires once. |
| 1517 |
scanner.set_requested_mode(BluetoothScanningMode.PASSIVE) |
| 1518 |
assert notify.call_count == 1 |
| 1519 |
|
| 1520 |
# current_mode was None at init; setting None is a no-op. |
| 1521 |
scanner.set_current_mode(None) |
| 1522 |
assert notify.call_count == 1 |
| 1523 |
|
| 1524 |
# Different value → notify fires again. |
| 1525 |
scanner.set_current_mode(BluetoothScanningMode.PASSIVE) |
| 1526 |
assert notify.call_count == 2 |
| 1527 |
|
| 1528 |
# Repeating same value is a no-op. |
| 1529 |
scanner.set_current_mode(BluetoothScanningMode.PASSIVE) |
| 1530 |
assert notify.call_count == 2 |