2022-11-20 14:44:28 +00:00
|
|
|
"""Base classes for HA Bluetooth scanners for bluetooth."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-12-08 16:50:36 +00:00
|
|
|
from abc import ABC, abstractmethod
|
2022-11-22 02:23:07 +00:00
|
|
|
from collections.abc import Callable, Generator
|
|
|
|
from contextlib import contextmanager
|
2022-11-20 14:44:28 +00:00
|
|
|
import datetime
|
|
|
|
from datetime import timedelta
|
|
|
|
from typing import Any, Final
|
|
|
|
|
|
|
|
from bleak.backends.device import BLEDevice
|
|
|
|
from bleak.backends.scanner import AdvertisementData
|
|
|
|
from bleak_retry_connector import NO_RSSI_VALUE
|
2022-11-22 02:23:07 +00:00
|
|
|
from bluetooth_adapters import adapter_human_name
|
2022-11-20 14:44:28 +00:00
|
|
|
from home_assistant_bluetooth import BluetoothServiceInfoBleak
|
|
|
|
|
|
|
|
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback as hass_callback
|
|
|
|
from homeassistant.helpers.event import async_track_time_interval
|
|
|
|
from homeassistant.util.dt import monotonic_time_coarse
|
|
|
|
|
|
|
|
from .const import (
|
|
|
|
CONNECTABLE_FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS,
|
|
|
|
FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS,
|
|
|
|
)
|
|
|
|
from .models import HaBluetoothConnector
|
|
|
|
|
|
|
|
MONOTONIC_TIME: Final = monotonic_time_coarse
|
|
|
|
|
|
|
|
|
2022-12-08 16:50:36 +00:00
|
|
|
class BaseHaScanner(ABC):
|
2022-11-20 14:44:28 +00:00
|
|
|
"""Base class for Ha Scanners."""
|
|
|
|
|
2022-11-22 02:23:07 +00:00
|
|
|
__slots__ = ("hass", "source", "_connecting", "name", "scanning")
|
|
|
|
|
|
|
|
def __init__(self, hass: HomeAssistant, source: str, adapter: str) -> None:
|
2022-11-20 14:44:28 +00:00
|
|
|
"""Initialize the scanner."""
|
|
|
|
self.hass = hass
|
|
|
|
self.source = source
|
2022-11-22 02:23:07 +00:00
|
|
|
self._connecting = 0
|
|
|
|
self.name = adapter_human_name(adapter, source) if adapter != source else source
|
|
|
|
self.scanning = True
|
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
def connecting(self) -> Generator[None, None, None]:
|
|
|
|
"""Context manager to track connecting state."""
|
|
|
|
self._connecting += 1
|
|
|
|
self.scanning = not self._connecting
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
self._connecting -= 1
|
|
|
|
self.scanning = not self._connecting
|
2022-11-20 14:44:28 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
@abstractmethod
|
|
|
|
def discovered_devices(self) -> list[BLEDevice]:
|
|
|
|
"""Return a list of discovered devices."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
@abstractmethod
|
|
|
|
def discovered_devices_and_advertisement_data(
|
|
|
|
self,
|
|
|
|
) -> dict[str, tuple[BLEDevice, AdvertisementData]]:
|
|
|
|
"""Return a list of discovered devices and their advertisement data."""
|
|
|
|
|
|
|
|
async def async_diagnostics(self) -> dict[str, Any]:
|
|
|
|
"""Return diagnostic information about the scanner."""
|
|
|
|
return {
|
|
|
|
"type": self.__class__.__name__,
|
2022-11-27 19:59:37 +00:00
|
|
|
"discovered_devices_and_advertisement_data": [
|
2022-11-20 14:44:28 +00:00
|
|
|
{
|
2022-11-27 19:59:37 +00:00
|
|
|
"name": device_adv[0].name,
|
|
|
|
"address": device_adv[0].address,
|
|
|
|
"rssi": device_adv[0].rssi,
|
|
|
|
"advertisement_data": device_adv[1],
|
2022-11-29 04:54:53 +00:00
|
|
|
"details": device_adv[0].details,
|
2022-11-20 14:44:28 +00:00
|
|
|
}
|
2022-11-27 19:59:37 +00:00
|
|
|
for device_adv in self.discovered_devices_and_advertisement_data.values()
|
2022-11-20 14:44:28 +00:00
|
|
|
],
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class BaseHaRemoteScanner(BaseHaScanner):
|
|
|
|
"""Base class for a Home Assistant remote BLE scanner."""
|
|
|
|
|
2022-11-22 02:23:07 +00:00
|
|
|
__slots__ = (
|
|
|
|
"_new_info_callback",
|
|
|
|
"_discovered_device_advertisement_datas",
|
|
|
|
"_discovered_device_timestamps",
|
|
|
|
"_connector",
|
|
|
|
"_connectable",
|
|
|
|
"_details",
|
|
|
|
"_expire_seconds",
|
|
|
|
)
|
|
|
|
|
2022-11-20 14:44:28 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
hass: HomeAssistant,
|
|
|
|
scanner_id: str,
|
2022-11-22 02:23:07 +00:00
|
|
|
name: str,
|
2022-11-20 14:44:28 +00:00
|
|
|
new_info_callback: Callable[[BluetoothServiceInfoBleak], None],
|
|
|
|
connector: HaBluetoothConnector,
|
|
|
|
connectable: bool,
|
|
|
|
) -> None:
|
|
|
|
"""Initialize the scanner."""
|
2022-11-22 02:23:07 +00:00
|
|
|
super().__init__(hass, scanner_id, name)
|
2022-11-20 14:44:28 +00:00
|
|
|
self._new_info_callback = new_info_callback
|
|
|
|
self._discovered_device_advertisement_datas: dict[
|
|
|
|
str, tuple[BLEDevice, AdvertisementData]
|
|
|
|
] = {}
|
|
|
|
self._discovered_device_timestamps: dict[str, float] = {}
|
|
|
|
self._connector = connector
|
|
|
|
self._connectable = connectable
|
|
|
|
self._details: dict[str, str | HaBluetoothConnector] = {"source": scanner_id}
|
|
|
|
self._expire_seconds = FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS
|
|
|
|
if connectable:
|
|
|
|
self._details["connector"] = connector
|
|
|
|
self._expire_seconds = (
|
|
|
|
CONNECTABLE_FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS
|
|
|
|
)
|
|
|
|
|
|
|
|
@hass_callback
|
|
|
|
def async_setup(self) -> CALLBACK_TYPE:
|
|
|
|
"""Set up the scanner."""
|
|
|
|
return async_track_time_interval(
|
|
|
|
self.hass, self._async_expire_devices, timedelta(seconds=30)
|
|
|
|
)
|
|
|
|
|
|
|
|
def _async_expire_devices(self, _datetime: datetime.datetime) -> None:
|
|
|
|
"""Expire old devices."""
|
|
|
|
now = MONOTONIC_TIME()
|
|
|
|
expired = [
|
|
|
|
address
|
|
|
|
for address, timestamp in self._discovered_device_timestamps.items()
|
|
|
|
if now - timestamp > self._expire_seconds
|
|
|
|
]
|
|
|
|
for address in expired:
|
|
|
|
del self._discovered_device_advertisement_datas[address]
|
|
|
|
del self._discovered_device_timestamps[address]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def discovered_devices(self) -> list[BLEDevice]:
|
|
|
|
"""Return a list of discovered devices."""
|
|
|
|
return [
|
|
|
|
device_advertisement_data[0]
|
|
|
|
for device_advertisement_data in self._discovered_device_advertisement_datas.values()
|
|
|
|
]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def discovered_devices_and_advertisement_data(
|
|
|
|
self,
|
|
|
|
) -> dict[str, tuple[BLEDevice, AdvertisementData]]:
|
|
|
|
"""Return a list of discovered devices and advertisement data."""
|
|
|
|
return self._discovered_device_advertisement_datas
|
|
|
|
|
|
|
|
@hass_callback
|
|
|
|
def _async_on_advertisement(
|
|
|
|
self,
|
|
|
|
address: str,
|
|
|
|
rssi: int,
|
|
|
|
local_name: str | None,
|
|
|
|
service_uuids: list[str],
|
|
|
|
service_data: dict[str, bytes],
|
|
|
|
manufacturer_data: dict[int, bytes],
|
|
|
|
tx_power: int | None,
|
2022-11-29 04:54:53 +00:00
|
|
|
details: dict[Any, Any],
|
2022-11-20 14:44:28 +00:00
|
|
|
) -> None:
|
|
|
|
"""Call the registered callback."""
|
|
|
|
now = MONOTONIC_TIME()
|
|
|
|
if prev_discovery := self._discovered_device_advertisement_datas.get(address):
|
|
|
|
# Merge the new data with the old data
|
|
|
|
# to function the same as BlueZ which
|
|
|
|
# merges the dicts on PropertiesChanged
|
|
|
|
prev_device = prev_discovery[0]
|
|
|
|
prev_advertisement = prev_discovery[1]
|
|
|
|
if (
|
|
|
|
local_name
|
|
|
|
and prev_device.name
|
|
|
|
and len(prev_device.name) > len(local_name)
|
|
|
|
):
|
|
|
|
local_name = prev_device.name
|
|
|
|
if prev_advertisement.service_uuids:
|
|
|
|
service_uuids = list(
|
|
|
|
set(service_uuids + prev_advertisement.service_uuids)
|
|
|
|
)
|
|
|
|
if prev_advertisement.service_data:
|
|
|
|
service_data = {**prev_advertisement.service_data, **service_data}
|
|
|
|
if prev_advertisement.manufacturer_data:
|
|
|
|
manufacturer_data = {
|
|
|
|
**prev_advertisement.manufacturer_data,
|
|
|
|
**manufacturer_data,
|
|
|
|
}
|
|
|
|
|
|
|
|
advertisement_data = AdvertisementData(
|
|
|
|
local_name=None if local_name == "" else local_name,
|
|
|
|
manufacturer_data=manufacturer_data,
|
|
|
|
service_data=service_data,
|
|
|
|
service_uuids=service_uuids,
|
|
|
|
rssi=rssi,
|
|
|
|
tx_power=NO_RSSI_VALUE if tx_power is None else tx_power,
|
|
|
|
platform_data=(),
|
|
|
|
)
|
|
|
|
device = BLEDevice( # type: ignore[no-untyped-call]
|
|
|
|
address=address,
|
|
|
|
name=local_name,
|
2022-11-29 04:54:53 +00:00
|
|
|
details=self._details | details,
|
2022-11-20 14:44:28 +00:00
|
|
|
rssi=rssi, # deprecated, will be removed in newer bleak
|
|
|
|
)
|
|
|
|
self._discovered_device_advertisement_datas[address] = (
|
|
|
|
device,
|
|
|
|
advertisement_data,
|
|
|
|
)
|
|
|
|
self._discovered_device_timestamps[address] = now
|
|
|
|
self._new_info_callback(
|
|
|
|
BluetoothServiceInfoBleak(
|
|
|
|
name=advertisement_data.local_name or device.name or device.address,
|
|
|
|
address=device.address,
|
|
|
|
rssi=rssi,
|
|
|
|
manufacturer_data=advertisement_data.manufacturer_data,
|
|
|
|
service_data=advertisement_data.service_data,
|
|
|
|
service_uuids=advertisement_data.service_uuids,
|
|
|
|
source=self.source,
|
|
|
|
device=device,
|
|
|
|
advertisement=advertisement_data,
|
|
|
|
connectable=self._connectable,
|
|
|
|
time=now,
|
|
|
|
)
|
|
|
|
)
|
2022-12-09 15:55:10 +00:00
|
|
|
|
|
|
|
async def async_diagnostics(self) -> dict[str, Any]:
|
|
|
|
"""Return diagnostic information about the scanner."""
|
|
|
|
return await super().async_diagnostics() | {
|
|
|
|
"type": self.__class__.__name__,
|
|
|
|
"discovered_devices_and_advertisement_data": [
|
|
|
|
{
|
|
|
|
"name": device_adv[0].name,
|
|
|
|
"address": device_adv[0].address,
|
|
|
|
"rssi": device_adv[0].rssi,
|
|
|
|
"advertisement_data": device_adv[1],
|
|
|
|
"details": device_adv[0].details,
|
|
|
|
}
|
|
|
|
for device_adv in self.discovered_devices_and_advertisement_data.values()
|
|
|
|
],
|
|
|
|
}
|