hass-core/homeassistant/components/bluetooth/base_scanner.py
J. Nick Koston e890671192
Relocate Bluetooth manager to habluetooth library (#105110)
* Relocate Bluetooth manager to habluetooth library

* Relocate Bluetooth manager to habluetooth library

* Relocate Bluetooth manager to habluetooth library

* fixes

* fix patching time

* fix more tests

* fix more tests

* split

* Bump habluetooth to 0.7.0

changelog: https://github.com/Bluetooth-Devices/habluetooth/compare/v0.6.1...v0.7.0

This is the big change that will move the manager so the HA
PR that will follow this will be a bit larger than the rest of them
since the manager is connected to everything

* fix types

* fix types

* fix types

* fix patch targets

* fix flakey logbook tests (will need another PR)

* mock shutdown

* bump again

* value can be a float now

* Revert "value can be a float now"

This reverts commit b7e7127143.

* float
2023-12-11 15:42:00 -05:00

95 lines
3 KiB
Python

"""Base classes for HA Bluetooth scanners for bluetooth."""
from __future__ import annotations
from collections.abc import Callable
from typing import Any
from bluetooth_adapters import DiscoveredDeviceAdvertisementData
from habluetooth import BaseHaRemoteScanner, HaBluetoothConnector
from home_assistant_bluetooth import BluetoothServiceInfoBleak
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
from homeassistant.core import (
CALLBACK_TYPE,
Event,
HomeAssistant,
callback as hass_callback,
)
from . import models
class HomeAssistantRemoteScanner(BaseHaRemoteScanner):
"""Home Assistant remote BLE scanner.
This is the only object that should know about
the hass object.
"""
__slots__ = (
"hass",
"_storage",
"_cancel_stop",
)
def __init__(
self,
hass: HomeAssistant,
scanner_id: str,
name: str,
new_info_callback: Callable[[BluetoothServiceInfoBleak], None],
connector: HaBluetoothConnector | None,
connectable: bool,
) -> None:
"""Initialize the scanner."""
self.hass = hass
assert models.MANAGER is not None
self._storage = models.MANAGER.storage
self._cancel_stop: CALLBACK_TYPE | None = None
super().__init__(scanner_id, name, new_info_callback, connector, connectable)
@hass_callback
def async_setup(self) -> CALLBACK_TYPE:
"""Set up the scanner."""
super().async_setup()
if history := self._storage.async_get_advertisement_history(self.source):
self._discovered_device_advertisement_datas = (
history.discovered_device_advertisement_datas
)
self._discovered_device_timestamps = history.discovered_device_timestamps
# Expire anything that is too old
self._async_expire_devices()
self._cancel_stop = self.hass.bus.async_listen(
EVENT_HOMEASSISTANT_STOP, self._async_save_history
)
return self._unsetup
@hass_callback
def _unsetup(self) -> None:
super()._unsetup()
self._async_save_history()
if self._cancel_stop:
self._cancel_stop()
self._cancel_stop = None
@hass_callback
def _async_save_history(self, event: Event | None = None) -> None:
"""Save the history."""
self._storage.async_set_advertisement_history(
self.source,
DiscoveredDeviceAdvertisementData(
self.connectable,
self._expire_seconds,
self._discovered_device_advertisement_datas,
self._discovered_device_timestamps,
),
)
async def async_diagnostics(self) -> dict[str, Any]:
"""Return diagnostic information about the scanner."""
diag = await super().async_diagnostics()
diag["storage"] = self._storage.async_get_advertisement_history_as_dict(
self.source
)
return diag