Add Bluetooth API to register event-driven devices (#194859)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Shay Levy
2026-11-28 11:15:20 +01:00
committed by GitHub
co-authored by Claude Opus 5.5
parent 5276c552da
commit ccfb18ed18
5 changed files with 944 additions and 2 deletions
@@ -58,6 +58,7 @@ from .api import (
_get_manager,
async_address_present,
async_address_reachability_diagnostics,
async_advertisement_restored,
async_ble_device_from_address,
async_clear_address_from_match_history,
async_clear_advertisement_history,
@@ -72,6 +73,7 @@ from .api import (
async_rediscover_address,
async_register_advertisement_callback,
async_register_callback,
async_register_event_driven,
async_register_scanner,
async_remove_scanner,
async_request_active_scan,
@@ -121,6 +123,7 @@ __all__ = [
"HaBluetoothConnector",
"async_address_present",
"async_address_reachability_diagnostics",
"async_advertisement_restored",
"async_ble_device_from_address",
"async_clear_address_from_match_history",
"async_clear_advertisement_history",
@@ -135,6 +138,7 @@ __all__ = [
"async_rediscover_address",
"async_register_advertisement_callback",
"async_register_callback",
"async_register_event_driven",
"async_register_scanner",
"async_remove_scanner",
"async_request_active_scan",
+16
View File
@@ -324,6 +324,22 @@ def async_set_fallback_availability_interval(
_get_manager(hass).async_set_fallback_availability_interval(address, interval)
@hass_callback
def async_advertisement_restored(
hass: HomeAssistant, service_info: BluetoothServiceInfoBleak
) -> bool:
"""Return if an advertisement was restored rather than heard since start."""
return _get_manager(hass).async_advertisement_restored(service_info)
@hass_callback
def async_register_event_driven(
hass: HomeAssistant, address: str
) -> Callable[[], None]:
"""Take new payloads of an event-driven device from any scanner."""
return _get_manager(hass).async_register_event_driven(address)
async def async_request_active_scan(
hass: HomeAssistant, duration: float | None = None
) -> None:
@@ -1,6 +1,8 @@
"""The bluetooth integration."""
from collections.abc import Callable
from dataclasses import dataclass
from datetime import datetime
from functools import partial
import itertools
import logging
@@ -14,6 +16,7 @@ from bluetooth_adapters import (
adapter_human_name,
adapter_model,
)
from bluetooth_data_tools import monotonic_time_coarse
from habluetooth import (
BaseHaRemoteScanner,
BaseHaScanner,
@@ -37,6 +40,8 @@ from homeassistant.core import (
)
from homeassistant.helpers import discovery_flow, issue_registry as ir
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.event import async_call_later
from homeassistant.helpers.start import async_at_started
from homeassistant.util.package import is_docker_env
from .const import (
@@ -62,13 +67,32 @@ from .util import async_load_history_from_system
_LOGGER = logging.getLogger(__name__)
# Time after start before unclaimed startup registrations are released
EVENT_DRIVEN_CLAIM_SECONDS = 600
@dataclass(slots=True)
class _PendingRestore:
"""A stored accepted advertisement waiting for its address to register."""
accepted: BluetoothServiceInfoBleak
connectable_pending: bool
cancel_heard: CALLBACK_TYPE
all_pending: bool = True
class HomeAssistantBluetoothManager(BluetoothManager):
"""Manage Bluetooth for Home Assistant."""
__slots__ = (
"_callback_index",
"_cancel_event_driven_release",
"_cancel_logging_listener",
"_event_driven_addresses",
"_event_driven_disappeared",
"_event_driven_early",
"_event_driven_restore",
"_history_loaded_time",
"_integration_matcher",
"hass",
"storage",
@@ -88,6 +112,13 @@ class HomeAssistantBluetoothManager(BluetoothManager):
self._integration_matcher = integration_matcher
self._callback_index = BluetoothCallbackMatcherIndex()
self._cancel_logging_listener: CALLBACK_TYPE | None = None
self._event_driven_addresses: dict[str, set[object]] = {}
# Last advertisement of an event-driven address before it expired
self._event_driven_disappeared: dict[str, BluetoothServiceInfoBleak] = {}
self._event_driven_restore: dict[str, _PendingRestore] = {}
self._event_driven_early: dict[str, CALLBACK_TYPE] = {}
self._history_loaded_time = 0.0
self._cancel_event_driven_release: list[CALLBACK_TYPE] = []
super().__init__(bluetooth_adapters, slot_manager)
self._async_logging_changed()
@@ -124,6 +155,78 @@ class HomeAssistantBluetoothManager(BluetoothManager):
if service_info := self._all_history.get(address):
self._async_trigger_matching_discovery(service_info)
@override
def async_register_event_driven(self, address: str) -> Callable[[], None]:
"""Register an event-driven address, restoring its accepted advertisement."""
token = object()
self._event_driven_addresses.setdefault(address, set()).add(token)
unregister = super().async_register_event_driven(address)
if (release := self._event_driven_early.pop(address, None)) is not None:
release()
if (pending := self._event_driven_restore.pop(address, None)) is not None:
pending.cancel_heard()
if pending.all_pending:
self._all_history[address] = pending.accepted
if pending.connectable_pending:
self._connectable_history[address] = pending.accepted
return partial(self._async_forget_event_driven, address, token, unregister)
@hass_callback
def async_advertisement_restored(
self, service_info: BluetoothServiceInfoBleak
) -> bool:
"""Return if an advertisement was restored from before the start."""
return service_info.time <= self._history_loaded_time
@hass_callback
def _async_schedule_event_driven_release(self, hass: HomeAssistant) -> None:
"""Release unclaimed startup registrations once integrations had time."""
self._cancel_event_driven_release.append(
async_call_later(
hass, EVENT_DRIVEN_CLAIM_SECONDS, self._async_release_event_driven
)
)
@hass_callback
def _async_release_event_driven(self, _now: datetime | None = None) -> None:
"""Release startup registrations no integration registered again."""
for address, release in self._event_driven_early.items():
release()
self._event_driven_disappeared.pop(address, None)
self._event_driven_early.clear()
for pending in self._event_driven_restore.values():
pending.cancel_heard()
self._event_driven_restore.clear()
@hass_callback
def _async_event_driven_heard(
self, address: str, service_info: BluetoothServiceInfoBleak
) -> None:
"""Stop restoring a history once a new advertisement replaced it."""
if (pending := self._event_driven_restore.get(address)) is None:
return
# Event-driven from startup, so the manager recorded it
pending.all_pending = False
if service_info.connectable:
pending.connectable_pending = False
if not pending.connectable_pending:
del self._event_driven_restore[address]
pending.cancel_heard()
@hass_callback
def _async_forget_event_driven(
self, address: str, token: object, unregister: Callable[[], None]
) -> None:
"""Remove one event-driven registration of an address."""
if (tokens := self._event_driven_addresses.get(address)) is None or (
token not in tokens
):
return
tokens.remove(token)
if not tokens:
del self._event_driven_addresses[address]
unregister()
@hass_callback
def async_clear_address_from_match_history(self, address: str) -> None:
"""Clear an address from the integration matcher history.
@@ -175,6 +278,13 @@ class HomeAssistantBluetoothManager(BluetoothManager):
@override
def _address_disappeared(self, address: str) -> None:
"""Dismiss all discoveries for the given address."""
# Called before the address leaves the history
if (
address in self._event_driven_addresses
or address in self._event_driven_early
or address in self._event_driven_disappeared
) and (service_info := self._all_history.get(address)):
self._event_driven_disappeared[address] = service_info
self._integration_matcher.async_clear_address(address)
self.hass.config_entries.flow.async_dismiss_discovery_flows(
BluetoothServiceInfoBleak,
@@ -188,6 +298,22 @@ class HomeAssistantBluetoothManager(BluetoothManager):
self._all_history, self._connectable_history = async_load_history_from_system(
self._bluetooth_adapters, self.storage
)
self._history_loaded_time = monotonic_time_coarse()
for address, accepted in self.storage.async_get_event_driven_history().items():
self._event_driven_early[address] = super().async_register_event_driven(
address
)
self._event_driven_restore[address] = _PendingRestore(
accepted,
connectable_pending=accepted.connectable,
cancel_heard=self.async_register_advertisement_callback(
partial(self._async_event_driven_heard, address), address
),
)
if self._event_driven_early:
self._cancel_event_driven_release.append(
async_at_started(self.hass, self._async_schedule_event_driven_release)
)
self._cancel_logging_listener = self.hass.bus.async_listen(
EVENT_LOGGING_CHANGED, self._async_logging_changed
)
@@ -291,7 +417,22 @@ class HomeAssistantBluetoothManager(BluetoothManager):
def async_stop(self, event: Event | None = None) -> None:
"""Stop the Bluetooth integration at shutdown."""
_LOGGER.debug("Stopping bluetooth manager")
for cancel in self._cancel_event_driven_release:
cancel()
self._async_save_scanner_histories()
history: dict[str, BluetoothServiceInfoBleak] = {}
for address in self._event_driven_addresses.keys() | self._event_driven_early:
if service_info := self._all_history.get(
address
) or self._event_driven_disappeared.get(address):
history[address] = service_info
# Untouched, the history still holds the older startup entry
history.update(
(address, pending.accepted)
for address, pending in self._event_driven_restore.items()
if pending.all_pending
)
self.storage.async_set_event_driven_history(history)
super().async_stop()
if self._cancel_logging_listener:
self._cancel_logging_listener()
@@ -1,6 +1,10 @@
"""Storage for remote scanners."""
from typing import TypedDict
from habluetooth import (
FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS,
BluetoothServiceInfoBleak,
DiscoveredDeviceAdvertisementData,
DiscoveredDeviceAdvertisementDataDict,
DiscoveryStorageType,
@@ -14,9 +18,76 @@ from homeassistant.helpers.storage import Store
REMOTE_SCANNER_STORAGE_VERSION = 1
REMOTE_SCANNER_STORAGE_KEY = "bluetooth.remote_scanners"
EVENT_DRIVEN_STORAGE_VERSION = 1
EVENT_DRIVEN_STORAGE_KEY = "bluetooth.event_driven_history"
SCANNER_SAVE_DELAY = 5
class EventDrivenSourceDict(TypedDict):
"""Where the stored advertisement of an event-driven address came from."""
source: str
connectable: bool
class EventDrivenHistoryDict(TypedDict):
"""The accepted advertisements of event-driven addresses."""
sources: dict[str, EventDrivenSourceDict]
advertisements: DiscoveredDeviceAdvertisementDataDict
def _event_driven_history_to_dict(
history: dict[str, BluetoothServiceInfoBleak],
) -> EventDrivenHistoryDict:
"""Serialize the accepted advertisements of event-driven addresses."""
return EventDrivenHistoryDict(
sources={
address: EventDrivenSourceDict(
source=info.source, connectable=info.connectable
)
for address, info in history.items()
},
advertisements=discovered_device_advertisement_data_to_dict(
DiscoveredDeviceAdvertisementData(
connectable=False,
expire_seconds=FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS,
discovered_device_advertisement_datas={
address: (info.device, info.advertisement)
for address, info in history.items()
},
discovered_device_timestamps={
address: info.time for address, info in history.items()
},
)
),
)
def _event_driven_history_from_dict(
data: EventDrivenHistoryDict,
) -> dict[str, BluetoothServiceInfoBleak]:
"""Deserialize the accepted advertisements, which don't expire."""
if not (
loaded := discovered_device_advertisement_data_from_dict(data["advertisements"])
):
return {}
sources = data["sources"]
return {
address: BluetoothServiceInfoBleak.from_device_and_advertisement_data(
device,
advertisement_data,
sources[address]["source"],
loaded.discovered_device_timestamps[address],
sources[address]["connectable"],
)
for address, (
device,
advertisement_data,
) in loaded.discovered_device_advertisement_datas.items()
}
class BluetoothStorage:
"""Storage for remote scanners."""
@@ -26,11 +97,21 @@ class BluetoothStorage:
hass, REMOTE_SCANNER_STORAGE_VERSION, REMOTE_SCANNER_STORAGE_KEY
)
self._data: DiscoveryStorageType = {}
self._event_driven_store: Store[EventDrivenHistoryDict] = Store(
hass, EVENT_DRIVEN_STORAGE_VERSION, EVENT_DRIVEN_STORAGE_KEY
)
self._event_driven_history: dict[str, BluetoothServiceInfoBleak] = {}
async def async_setup(self) -> None:
"""Set up the storage."""
self._data = await self._store.async_load() or {}
expire_stale_scanner_discovered_device_advertisement_data(self._data)
if event_driven_data := await self._event_driven_store.async_load():
self._event_driven_history = _event_driven_history_from_dict(
event_driven_data
)
# Outdated after a crash, a clean stop saves it again
await self._event_driven_store.async_remove()
def scanners(self) -> list[str]:
"""Get all scanners."""
@@ -70,3 +151,16 @@ class BluetoothStorage:
"""Set discovered devices by scanner."""
self._data[scanner] = discovered_device_advertisement_data_to_dict(data)
self._store.async_delay_save(self._async_get_data, SCANNER_SAVE_DELAY)
@callback
def async_get_event_driven_history(self) -> dict[str, BluetoothServiceInfoBleak]:
"""Get the stored accepted advertisements of event-driven addresses."""
return self._event_driven_history
@callback
def async_set_event_driven_history(
self, history: dict[str, BluetoothServiceInfoBleak]
) -> None:
"""Store the accepted advertisements of event-driven addresses."""
data = _event_driven_history_to_dict(history)
self._event_driven_store.async_delay_save(lambda: data, SCANNER_SAVE_DELAY)
+689 -2
View File
@@ -8,7 +8,13 @@ from unittest.mock import call, patch
from bleak.backends.scanner import AdvertisementData, BLEDevice
from bluetooth_adapters import AdvertisementHistory
from freezegun import freeze_time
from habluetooth import BluetoothScanningMode, HaScanner
from habluetooth import (
BluetoothScanningMode,
DiscoveredDeviceAdvertisementData,
HaScanner,
discovered_device_advertisement_data_from_dict,
discovered_device_advertisement_data_to_dict,
)
# pylint: disable-next=no-name-in-module
from habluetooth.advertisement_tracker import TRACKER_BUFFERING_WOBBLE_SECONDS
@@ -47,10 +53,12 @@ from homeassistant.components.bluetooth import (
BluetoothServiceInfo,
BluetoothServiceInfoBleak,
HaBluetoothConnector,
async_advertisement_restored,
async_ble_device_from_address,
async_get_fallback_availability_interval,
async_get_learned_advertising_interval,
async_register_advertisement_callback,
async_register_event_driven,
async_scanner_count,
async_set_fallback_availability_interval,
async_track_unavailable,
@@ -60,7 +68,10 @@ from homeassistant.components.bluetooth.const import (
SOURCE_LOCAL,
UNAVAILABLE_TRACK_SECONDS,
)
from homeassistant.components.bluetooth.manager import HomeAssistantBluetoothManager
from homeassistant.components.bluetooth.manager import (
EVENT_DRIVEN_CLAIM_SECONDS,
HomeAssistantBluetoothManager,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import issue_registry as ir
from homeassistant.helpers.discovery_flow import DiscoveryKey
@@ -2102,6 +2113,682 @@ async def test_async_register_advertisement_callback(hass: HomeAssistant) -> Non
assert len(seen) == 2
EVENT_DRIVEN_ADDRESS = "44:44:33:11:23:46"
OPEN = b"\x01"
CLOSED = b"\x02"
def _event_advertisement(payload: bytes, rssi: int) -> AdvertisementData:
"""Return an advertisement of the event-driven device."""
return generate_advertisement_data(
local_name="event", service_data={"fcd2": payload}, rssi=rssi
)
def _event_driven_history() -> tuple[str, bytes]:
"""Return the source and payload the manager holds for the device."""
service_info = bluetooth.async_last_service_info(
_get_manager().hass, EVENT_DRIVEN_ADDRESS, False
)
assert service_info is not None
return service_info.source, service_info.service_data["fcd2"]
@pytest.mark.usefixtures(
"enable_bluetooth", "register_hci0_scanner", "register_hci1_scanner"
)
async def test_async_register_event_driven(hass: HomeAssistant) -> None:
"""A new payload of an event-driven device is taken from a weaker scanner."""
device = generate_ble_device(EVENT_DRIVEN_ADDRESS, "event")
start = MONOTONIC_TIME()
inject_advertisement_with_time_and_source(
hass, device, _event_advertisement(OPEN, -50), start, HCI0_SOURCE_ADDRESS
)
inject_advertisement_with_time_and_source(
hass, device, _event_advertisement(OPEN, -90), start + 0.1, HCI1_SOURCE_ADDRESS
)
inject_advertisement_with_time_and_source(
hass, device, _event_advertisement(CLOSED, -90), start + 5, HCI1_SOURCE_ADDRESS
)
assert _event_driven_history() == (HCI0_SOURCE_ADDRESS, OPEN)
cancel = async_register_event_driven(hass, EVENT_DRIVEN_ADDRESS)
inject_advertisement_with_time_and_source(
hass, device, _event_advertisement(CLOSED, -90), start + 20, HCI1_SOURCE_ADDRESS
)
assert _event_driven_history() == (HCI1_SOURCE_ADDRESS, CLOSED)
cancel()
inject_advertisement_with_time_and_source(
hass, device, _event_advertisement(OPEN, -50), start + 25, HCI0_SOURCE_ADDRESS
)
inject_advertisement_with_time_and_source(
hass, device, _event_advertisement(CLOSED, -90), start + 45, HCI1_SOURCE_ADDRESS
)
assert _event_driven_history() == (HCI0_SOURCE_ADDRESS, OPEN)
@pytest.mark.usefixtures(
"enable_bluetooth", "register_hci0_scanner", "register_hci1_scanner"
)
async def test_event_driven_release_twice_keeps_other_registration(
hass: HomeAssistant,
) -> None:
"""Releasing a registration twice doesn't release another one of the address."""
device = generate_ble_device(EVENT_DRIVEN_ADDRESS, "event")
start = MONOTONIC_TIME()
inject_advertisement_with_time_and_source(
hass, device, _event_advertisement(OPEN, -50), start, HCI0_SOURCE_ADDRESS
)
inject_advertisement_with_time_and_source(
hass, device, _event_advertisement(OPEN, -90), start + 0.1, HCI1_SOURCE_ADDRESS
)
cancel = async_register_event_driven(hass, EVENT_DRIVEN_ADDRESS)
async_register_event_driven(hass, EVENT_DRIVEN_ADDRESS)
cancel()
cancel()
inject_advertisement_with_time_and_source(
hass, device, _event_advertisement(CLOSED, -90), start + 20, HCI1_SOURCE_ADDRESS
)
assert _event_driven_history() == (HCI1_SOURCE_ADDRESS, CLOSED)
@pytest.mark.usefixtures(
"enable_bluetooth", "register_hci0_scanner", "register_hci1_scanner"
)
async def test_event_driven_history_saved_at_stop(
hass: HomeAssistant, hass_storage: dict[str, Any]
) -> None:
"""The accepted advertisement of a registered address is saved at stop."""
device = generate_ble_device(EVENT_DRIVEN_ADDRESS, "event")
start = MONOTONIC_TIME()
async_register_event_driven(hass, EVENT_DRIVEN_ADDRESS)
inject_advertisement_with_time_and_source(
hass, device, _event_advertisement(OPEN, -50), start, HCI0_SOURCE_ADDRESS
)
inject_advertisement_with_time_and_source(
hass, device, _event_advertisement(CLOSED, -90), start + 5, HCI1_SOURCE_ADDRESS
)
inject_advertisement_with_time_and_source(
hass,
generate_ble_device("44:44:33:11:23:99", "other"),
_event_advertisement(OPEN, -50),
start,
HCI0_SOURCE_ADDRESS,
)
await hass.async_stop()
data = hass_storage[storage.EVENT_DRIVEN_STORAGE_KEY]["data"]
assert data["sources"] == {
EVENT_DRIVEN_ADDRESS: {"source": HCI1_SOURCE_ADDRESS, "connectable": True}
}
assert list(data["advertisements"]["discovered_device_advertisement_datas"]) == [
EVENT_DRIVEN_ADDRESS
]
@pytest.mark.usefixtures(
"enable_bluetooth", "register_hci0_scanner", "register_hci1_scanner"
)
async def test_event_driven_history_saved_after_disappearing(
hass: HomeAssistant, hass_storage: dict[str, Any]
) -> None:
"""An accepted advertisement that left the history is still saved at stop."""
start = MONOTONIC_TIME()
async_register_event_driven(hass, EVENT_DRIVEN_ADDRESS)
inject_advertisement_with_time_and_source(
hass,
generate_ble_device(EVENT_DRIVEN_ADDRESS, "event"),
_event_advertisement(CLOSED, -90),
start,
HCI1_SOURCE_ADDRESS,
)
with patch_bluetooth_time(start + FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS + 1):
async_fire_time_changed(
hass, dt_util.utcnow() + timedelta(seconds=UNAVAILABLE_TRACK_SECONDS)
)
await hass.async_block_till_done()
assert bluetooth.async_last_service_info(hass, EVENT_DRIVEN_ADDRESS, False) is None
await hass.async_stop()
data = hass_storage[storage.EVENT_DRIVEN_STORAGE_KEY]["data"]
assert data["sources"] == {
EVENT_DRIVEN_ADDRESS: {"source": HCI1_SOURCE_ADDRESS, "connectable": True}
}
@pytest.mark.usefixtures(
"enable_bluetooth", "register_hci0_scanner", "register_hci1_scanner"
)
async def test_event_driven_history_saved_after_disappearing_and_reload(
hass: HomeAssistant, hass_storage: dict[str, Any]
) -> None:
"""A registration ending and starting again keeps the last advertisement."""
start = MONOTONIC_TIME()
cancel = async_register_event_driven(hass, EVENT_DRIVEN_ADDRESS)
inject_advertisement_with_time_and_source(
hass,
generate_ble_device(EVENT_DRIVEN_ADDRESS, "event"),
_event_advertisement(CLOSED, -90),
start,
HCI1_SOURCE_ADDRESS,
)
with patch_bluetooth_time(start + FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS + 1):
async_fire_time_changed(
hass, dt_util.utcnow() + timedelta(seconds=UNAVAILABLE_TRACK_SECONDS)
)
await hass.async_block_till_done()
cancel()
async_register_event_driven(hass, EVENT_DRIVEN_ADDRESS)
await hass.async_stop()
data = hass_storage[storage.EVENT_DRIVEN_STORAGE_KEY]["data"]
assert data["sources"] == {
EVENT_DRIVEN_ADDRESS: {"source": HCI1_SOURCE_ADDRESS, "connectable": True}
}
def _saved_event_driven(hass_storage: dict[str, Any]) -> tuple[str, bytes]:
"""Return the source and payload saved for the device at stop."""
data = hass_storage[storage.EVENT_DRIVEN_STORAGE_KEY]["data"]
advertisements = discovered_device_advertisement_data_from_dict(
data["advertisements"]
)
assert advertisements is not None
_, advertisement = advertisements.discovered_device_advertisement_datas[
EVENT_DRIVEN_ADDRESS
]
return (
data["sources"][EVENT_DRIVEN_ADDRESS]["source"],
advertisement.service_data["fcd2"],
)
@pytest.mark.usefixtures(
"enable_bluetooth", "register_hci0_scanner", "register_hci1_scanner"
)
async def test_event_driven_history_keeps_newer_after_unregistered_expiry(
hass: HomeAssistant, hass_storage: dict[str, Any]
) -> None:
"""A newer advertisement that expired while unregistered replaces the kept one."""
device = generate_ble_device(EVENT_DRIVEN_ADDRESS, "event")
start = MONOTONIC_TIME()
cancel = async_register_event_driven(hass, EVENT_DRIVEN_ADDRESS)
inject_advertisement_with_time_and_source(
hass, device, _event_advertisement(OPEN, -90), start, HCI1_SOURCE_ADDRESS
)
await _expire_history(hass)
cancel()
inject_advertisement_with_time_and_source(
hass,
device,
_event_advertisement(CLOSED, -90),
MONOTONIC_TIME(),
HCI1_SOURCE_ADDRESS,
)
await _expire_history(hass)
async_register_event_driven(hass, EVENT_DRIVEN_ADDRESS)
await hass.async_stop()
assert _saved_event_driven(hass_storage) == (HCI1_SOURCE_ADDRESS, CLOSED)
def _stored_advertisement(
payload: bytes, rssi: int, heard: float, connectable: bool
) -> dict[str, Any]:
"""Return a stored advertisement of the event-driven device."""
return discovered_device_advertisement_data_to_dict(
DiscoveredDeviceAdvertisementData(
connectable=connectable,
expire_seconds=FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS,
discovered_device_advertisement_datas={
EVENT_DRIVEN_ADDRESS: (
generate_ble_device(EVENT_DRIVEN_ADDRESS, "event"),
_event_advertisement(payload, rssi),
)
},
discovered_device_timestamps={EVENT_DRIVEN_ADDRESS: heard},
)
)
@pytest.fixture
async def restarted_bluetooth(
hass: HomeAssistant,
hass_storage: dict[str, Any],
request: pytest.FixtureRequest,
) -> None:
"""Start Bluetooth from stored scanner, local and accepted advertisements."""
now = MONOTONIC_TIME()
scanners, local, accepted = request.param
hass_storage[storage.REMOTE_SCANNER_STORAGE_KEY] = {
"version": storage.REMOTE_SCANNER_STORAGE_VERSION,
"minor_version": 1,
"key": storage.REMOTE_SCANNER_STORAGE_KEY,
"data": {
scanner: _stored_advertisement(payload, rssi, now - age, connectable)
for scanner, (payload, rssi, age, connectable) in scanners.items()
},
}
accepted_source, accepted_payload, accepted_age, accepted_connectable = accepted
hass_storage[storage.EVENT_DRIVEN_STORAGE_KEY] = {
"version": storage.EVENT_DRIVEN_STORAGE_VERSION,
"minor_version": 1,
"key": storage.EVENT_DRIVEN_STORAGE_KEY,
"data": {
"sources": {
EVENT_DRIVEN_ADDRESS: {
"source": accepted_source,
"connectable": accepted_connectable,
}
},
"advertisements": _stored_advertisement(
accepted_payload, -90, now - accepted_age, accepted_connectable
),
},
}
local_history = {
address: AdvertisementHistory(
generate_ble_device(address, "event"),
_event_advertisement(payload, rssi),
"hci0",
)
for address, (payload, rssi) in local.items()
}
with patch("bluetooth_adapters.systems.linux.LinuxAdapters.history", local_history):
assert await async_setup_component(hass, bluetooth.DOMAIN, {})
await hass.async_block_till_done()
OLDER_STRONG_REMOTE = (
{
"strong-proxy": (OPEN, -50, 60, False),
"weak-proxy": (CLOSED, -90, 10, False),
},
{},
("weak-proxy", CLOSED, 10, False),
)
@pytest.mark.usefixtures("one_adapter", "disable_new_discovery_flows")
@pytest.mark.parametrize(
("restarted_bluetooth", "expected"),
[
pytest.param(
OLDER_STRONG_REMOTE, ("weak-proxy", CLOSED), id="older_strong_remote"
),
pytest.param(
(
{"weak-proxy": (CLOSED, -90, 10, False)},
{EVENT_DRIVEN_ADDRESS: (OPEN, -50)},
("weak-proxy", CLOSED, 10, False),
),
("weak-proxy", CLOSED),
id="older_strong_local",
),
pytest.param(
(
{
"strong-proxy": (CLOSED, -50, 20, False),
"weak-proxy": (OPEN, -90, 10, False),
},
{},
("strong-proxy", CLOSED, 20, False),
),
("strong-proxy", CLOSED),
id="later_stale_copy",
),
pytest.param(
(
{},
{EVENT_DRIVEN_ADDRESS: (OPEN, -50)},
(
"weak-proxy",
CLOSED,
FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS * 4,
False,
),
),
("weak-proxy", CLOSED),
id="accepted_before_long_downtime",
),
pytest.param(
({}, {}, ("weak-proxy", CLOSED, 10, False)),
("weak-proxy", CLOSED),
id="no_startup_entry",
),
],
indirect=["restarted_bluetooth"],
)
@pytest.mark.usefixtures("restarted_bluetooth")
async def test_register_event_driven_restores_accepted(
hass: HomeAssistant, expected: tuple[str, bytes]
) -> None:
"""Registering after a restart starts from the advertisement accepted before it."""
cancel = async_register_event_driven(hass, EVENT_DRIVEN_ADDRESS)
assert _event_driven_history() == expected
cancel()
@pytest.mark.usefixtures("one_adapter", "disable_new_discovery_flows")
@pytest.mark.parametrize(
"restarted_bluetooth",
[
(
{
"strong-proxy": (OPEN, -50, 60, True),
"weak-proxy": (CLOSED, -90, 10, True),
},
{},
("weak-proxy", CLOSED, 10, True),
)
],
indirect=True,
)
@pytest.mark.usefixtures("restarted_bluetooth")
async def test_register_event_driven_restores_connectable(
hass: HomeAssistant,
) -> None:
"""A connectable accepted advertisement also restores the connectable history."""
cancel = async_register_event_driven(hass, EVENT_DRIVEN_ADDRESS)
service_info = bluetooth.async_last_service_info(hass, EVENT_DRIVEN_ADDRESS, True)
assert service_info is not None
assert (service_info.source, service_info.service_data["fcd2"]) == (
"weak-proxy",
CLOSED,
)
cancel()
@pytest.mark.usefixtures("one_adapter", "disable_new_discovery_flows")
@pytest.mark.parametrize("restarted_bluetooth", [OLDER_STRONG_REMOTE], indirect=True)
@pytest.mark.usefixtures("restarted_bluetooth")
async def test_register_event_driven_keeps_live_advertisement(
hass: HomeAssistant,
) -> None:
"""An advertisement heard after startup is kept over the stored one."""
inject_advertisement_with_time_and_source(
hass,
generate_ble_device(EVENT_DRIVEN_ADDRESS, "event"),
_event_advertisement(OPEN, -50),
MONOTONIC_TIME(),
"strong-proxy",
)
cancel = async_register_event_driven(hass, EVENT_DRIVEN_ADDRESS)
assert _event_driven_history() == ("strong-proxy", OPEN)
cancel()
async def _expire_history(hass: HomeAssistant) -> None:
"""Let the manager drop advertisements older than the fallback interval."""
with patch_bluetooth_time(
MONOTONIC_TIME() + FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS + 1
):
async_fire_time_changed(
hass, dt_util.utcnow() + timedelta(seconds=UNAVAILABLE_TRACK_SECONDS)
)
await hass.async_block_till_done()
@pytest.mark.usefixtures("one_adapter", "disable_new_discovery_flows")
@pytest.mark.parametrize("restarted_bluetooth", [OLDER_STRONG_REMOTE], indirect=True)
@pytest.mark.usefixtures("restarted_bluetooth")
async def test_register_event_driven_after_startup_entry_expired(
hass: HomeAssistant,
) -> None:
"""A late registration restores the accepted advertisement if nothing was heard."""
await _expire_history(hass)
assert bluetooth.async_last_service_info(hass, EVENT_DRIVEN_ADDRESS, False) is None
cancel = async_register_event_driven(hass, EVENT_DRIVEN_ADDRESS)
assert _event_driven_history() == ("weak-proxy", CLOSED)
cancel()
@pytest.mark.usefixtures("one_adapter", "disable_new_discovery_flows")
@pytest.mark.parametrize(
"restarted_bluetooth", [({}, {}, ("weak-proxy", CLOSED, 10, False))], indirect=True
)
@pytest.mark.usefixtures("restarted_bluetooth")
async def test_register_event_driven_after_live_advertisement_expired(
hass: HomeAssistant,
) -> None:
"""An advertisement heard after startup drops the stored one, even once expired."""
inject_advertisement_with_time_and_source(
hass,
generate_ble_device(EVENT_DRIVEN_ADDRESS, "event"),
_event_advertisement(OPEN, -50),
MONOTONIC_TIME(),
"strong-proxy",
)
await _expire_history(hass)
cancel = async_register_event_driven(hass, EVENT_DRIVEN_ADDRESS)
assert bluetooth.async_last_service_info(hass, EVENT_DRIVEN_ADDRESS, False) is None
cancel()
@pytest.mark.usefixtures("one_adapter", "disable_new_discovery_flows")
@pytest.mark.parametrize("restarted_bluetooth", [OLDER_STRONG_REMOTE], indirect=True)
@pytest.mark.usefixtures("restarted_bluetooth")
async def test_new_advertisement_before_registration_kept(
hass: HomeAssistant,
) -> None:
"""A weaker scanner's new advertisement before registration replaces the old."""
cancels = [
bluetooth.async_register_scanner(hass, FakeScanner(source, source))
for source in ("strong-proxy", "weak-proxy")
]
inject_advertisement_with_time_and_source(
hass,
generate_ble_device(EVENT_DRIVEN_ADDRESS, "event"),
_event_advertisement(b"\x03", -90),
MONOTONIC_TIME(),
"weak-proxy",
)
cancel = async_register_event_driven(hass, EVENT_DRIVEN_ADDRESS)
assert _event_driven_history() == ("weak-proxy", b"\x03")
cancel()
for cancel_scanner in cancels:
cancel_scanner()
@pytest.mark.usefixtures("one_adapter", "disable_new_discovery_flows")
@pytest.mark.parametrize(
"restarted_bluetooth",
[
(
{
"strong-proxy": (OPEN, -50, 60, True),
"weak-proxy": (CLOSED, -90, 10, True),
},
{},
("weak-proxy", CLOSED, 10, True),
)
],
indirect=True,
)
@pytest.mark.usefixtures("restarted_bluetooth")
async def test_non_connectable_advertisement_keeps_connectable_restore(
hass: HomeAssistant,
) -> None:
"""A non-connectable advertisement leaves the connectable history to restore."""
inject_advertisement_with_time_and_source_connectable(
hass,
generate_ble_device(EVENT_DRIVEN_ADDRESS, "event"),
_event_advertisement(b"\x03", -90),
MONOTONIC_TIME(),
"weak-proxy",
False,
)
cancel = async_register_event_driven(hass, EVENT_DRIVEN_ADDRESS)
assert _event_driven_history() == ("weak-proxy", b"\x03")
service_info = bluetooth.async_last_service_info(hass, EVENT_DRIVEN_ADDRESS, True)
assert service_info is not None
assert service_info.service_data["fcd2"] == CLOSED
cancel()
@pytest.mark.usefixtures("one_adapter", "disable_new_discovery_flows")
@pytest.mark.parametrize("restarted_bluetooth", [OLDER_STRONG_REMOTE], indirect=True)
@pytest.mark.usefixtures("restarted_bluetooth")
async def test_pending_accepted_advertisement_saved_again(
hass: HomeAssistant, hass_storage: dict[str, Any]
) -> None:
"""An accepted advertisement still waiting for registration is saved at stop."""
await hass.async_stop()
data = hass_storage[storage.EVENT_DRIVEN_STORAGE_KEY]["data"]
assert data["sources"] == {
EVENT_DRIVEN_ADDRESS: {"source": "weak-proxy", "connectable": False}
}
@pytest.mark.usefixtures("one_adapter", "disable_new_discovery_flows")
@pytest.mark.parametrize("restarted_bluetooth", [OLDER_STRONG_REMOTE], indirect=True)
@pytest.mark.usefixtures("restarted_bluetooth")
async def test_startup_registered_address_saved_after_heard(
hass: HomeAssistant, hass_storage: dict[str, Any]
) -> None:
"""An address only registered from startup keeps what it was heard with."""
inject_advertisement_with_time_and_source(
hass,
generate_ble_device(EVENT_DRIVEN_ADDRESS, "event"),
_event_advertisement(b"\x03", -90),
MONOTONIC_TIME(),
"weak-proxy",
)
await _expire_history(hass)
await hass.async_stop()
assert _saved_event_driven(hass_storage) == ("weak-proxy", b"\x03")
@pytest.mark.usefixtures("one_adapter", "disable_new_discovery_flows")
@pytest.mark.parametrize("restarted_bluetooth", [OLDER_STRONG_REMOTE], indirect=True)
@pytest.mark.usefixtures("restarted_bluetooth")
async def test_async_advertisement_restored(hass: HomeAssistant) -> None:
"""Restored advertisements are told apart from advertisements heard since."""
startup = bluetooth.async_last_service_info(hass, EVENT_DRIVEN_ADDRESS, False)
assert startup is not None
assert async_advertisement_restored(hass, startup)
cancel = async_register_event_driven(hass, EVENT_DRIVEN_ADDRESS)
accepted = bluetooth.async_last_service_info(hass, EVENT_DRIVEN_ADDRESS, False)
assert accepted is not None
assert async_advertisement_restored(hass, accepted)
inject_advertisement_with_time_and_source(
hass,
generate_ble_device(EVENT_DRIVEN_ADDRESS, "event"),
_event_advertisement(b"\x03", -90),
MONOTONIC_TIME() + 1,
"weak-proxy",
)
live = bluetooth.async_last_service_info(hass, EVENT_DRIVEN_ADDRESS, False)
assert live is not None
assert not async_advertisement_restored(hass, live)
cancel()
@pytest.mark.usefixtures("one_adapter", "disable_new_discovery_flows")
@pytest.mark.parametrize("restarted_bluetooth", [OLDER_STRONG_REMOTE], indirect=True)
@pytest.mark.usefixtures("restarted_bluetooth")
async def test_unclaimed_startup_registration_released(
hass: HomeAssistant, hass_storage: dict[str, Any]
) -> None:
"""An address no integration registers again is released and not saved."""
async_fire_time_changed(
hass, dt_util.utcnow() + timedelta(seconds=EVENT_DRIVEN_CLAIM_SECONDS + 1)
)
await hass.async_block_till_done()
cancel = async_register_event_driven(hass, EVENT_DRIVEN_ADDRESS)
assert _event_driven_history() == ("strong-proxy", OPEN)
cancel()
await hass.async_stop()
assert hass_storage[storage.EVENT_DRIVEN_STORAGE_KEY]["data"]["sources"] == {}
@pytest.mark.usefixtures("one_adapter", "disable_new_discovery_flows")
@pytest.mark.parametrize("restarted_bluetooth", [OLDER_STRONG_REMOTE], indirect=True)
@pytest.mark.usefixtures("restarted_bluetooth")
async def test_released_address_forgets_expired_advertisement(
hass: HomeAssistant, hass_storage: dict[str, Any]
) -> None:
"""Releasing an unclaimed address also drops its kept expired advertisement."""
inject_advertisement_with_time_and_source(
hass,
generate_ble_device(EVENT_DRIVEN_ADDRESS, "event"),
_event_advertisement(b"\x03", -90),
MONOTONIC_TIME(),
"weak-proxy",
)
await _expire_history(hass)
async_fire_time_changed(
hass, dt_util.utcnow() + timedelta(seconds=EVENT_DRIVEN_CLAIM_SECONDS + 1)
)
await hass.async_block_till_done()
async_register_event_driven(hass, EVENT_DRIVEN_ADDRESS)
await hass.async_stop()
assert hass_storage[storage.EVENT_DRIVEN_STORAGE_KEY]["data"]["sources"] == {}
@pytest.mark.usefixtures("one_adapter", "disable_new_discovery_flows")
@pytest.mark.parametrize("restarted_bluetooth", [OLDER_STRONG_REMOTE], indirect=True)
@pytest.mark.usefixtures("restarted_bluetooth")
async def test_event_driven_history_removed_after_loading(
hass_storage: dict[str, Any],
) -> None:
"""The stored accepted advertisements are removed once loaded."""
assert storage.EVENT_DRIVEN_STORAGE_KEY not in hass_storage
@pytest.mark.usefixtures("one_adapter", "disable_new_discovery_flows")
async def test_corrupt_event_driven_history_restores_nothing(
hass: HomeAssistant, hass_storage: dict[str, Any]
) -> None:
"""Bluetooth starts without restoring accepted advertisements it can't read."""
hass_storage[storage.EVENT_DRIVEN_STORAGE_KEY] = {
"version": storage.EVENT_DRIVEN_STORAGE_VERSION,
"minor_version": 1,
"key": storage.EVENT_DRIVEN_STORAGE_KEY,
"data": {
"sources": {
EVENT_DRIVEN_ADDRESS: {"source": "weak-proxy", "connectable": False}
},
"advertisements": {},
},
}
assert await async_setup_component(hass, bluetooth.DOMAIN, {})
await hass.async_block_till_done()
async_register_event_driven(hass, EVENT_DRIVEN_ADDRESS)
assert bluetooth.async_last_service_info(hass, EVENT_DRIVEN_ADDRESS, False) is None
@pytest.mark.usefixtures("enable_bluetooth")
async def test_address_disappeared_keeps_dismiss_protected_flows(
hass: HomeAssistant,