Offload platform specific bluetooth code to bluetooth-adapters (#82196)

* Offload platform specific bluetooth code to bluetooth-adapters

* adjust

* fix some more patch targets

* more test fixes

* almost there

* may not be setup yet

* more fixes

* fixes

* fix test

* fix merge
This commit is contained in:
J. Nick Koston 2022-11-17 13:34:19 -06:00 committed by GitHub
parent d0efdd750f
commit 47c66dbed4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 158 additions and 262 deletions

View file

@ -10,6 +10,12 @@ from typing import TYPE_CHECKING, Any, Final
from bleak.backends.scanner import AdvertisementDataCallback
from bleak_retry_connector import NO_RSSI_VALUE, RSSI_SWITCH_THRESHOLD
from bluetooth_adapters import (
ADAPTER_ADDRESS,
ADAPTER_PASSIVE_SCAN,
AdapterDetails,
BluetoothAdapters,
)
from homeassistant import config_entries
from homeassistant.core import (
@ -24,11 +30,8 @@ from homeassistant.util.dt import monotonic_time_coarse
from .advertisement_tracker import AdvertisementTracker
from .const import (
ADAPTER_ADDRESS,
ADAPTER_PASSIVE_SCAN,
FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS,
UNAVAILABLE_TRACK_SECONDS,
AdapterDetails,
)
from .match import (
ADDRESS,
@ -47,7 +50,7 @@ from .models import (
BluetoothServiceInfoBleak,
)
from .usage import install_multiple_bleak_catcher, uninstall_multiple_bleak_catcher
from .util import async_get_bluetooth_adapters, async_load_history_from_system
from .util import async_load_history_from_system
if TYPE_CHECKING:
from bleak.backends.device import BLEDevice
@ -102,6 +105,7 @@ class BluetoothManager:
self,
hass: HomeAssistant,
integration_matcher: IntegrationMatcher,
bluetooth_adapters: BluetoothAdapters,
) -> None:
"""Init bluetooth manager."""
self.hass = hass
@ -127,6 +131,7 @@ class BluetoothManager:
self._connectable_scanners: list[BaseHaScanner] = []
self._adapters: dict[str, AdapterDetails] = {}
self._sources: set[str] = set()
self._bluetooth_adapters = bluetooth_adapters
@property
def supports_passive_scan(self) -> bool:
@ -172,21 +177,25 @@ class BluetoothManager:
self, cached: bool = True
) -> dict[str, AdapterDetails]:
"""Get bluetooth adapters."""
if not cached or not self._adapters:
self._adapters = await async_get_bluetooth_adapters()
if not self._adapters or not cached:
if not cached:
await self._bluetooth_adapters.refresh()
self._adapters = self._bluetooth_adapters.adapters
return self._adapters
async def async_get_adapter_from_address(self, address: str) -> str | None:
"""Get adapter from address."""
if adapter := self._find_adapter_by_address(address):
return adapter
self._adapters = await async_get_bluetooth_adapters()
await self._bluetooth_adapters.refresh()
self._adapters = self._bluetooth_adapters.adapters
return self._find_adapter_by_address(address)
async def async_setup(self) -> None:
"""Set up the bluetooth manager."""
await self._bluetooth_adapters.refresh()
install_multiple_bleak_catcher()
history = await async_load_history_from_system()
history = async_load_history_from_system(self._bluetooth_adapters)
# Everything is connectable so it fall into both
# buckets since the host system can only provide
# connectable devices