mirror of
https://github.com/home-assistant/core.git
synced 2026-04-25 19:29:04 +01:00
Add support for scanners that do not provide connectable devices (#77132)
This commit is contained in:
@@ -6,7 +6,12 @@ from unittest.mock import patch
|
||||
|
||||
from bleak.backends.scanner import AdvertisementData, BLEDevice
|
||||
|
||||
from homeassistant.components.bluetooth import DOMAIN, SOURCE_LOCAL, models
|
||||
from homeassistant.components.bluetooth import (
|
||||
DOMAIN,
|
||||
SOURCE_LOCAL,
|
||||
async_get_advertisement_callback,
|
||||
models,
|
||||
)
|
||||
from homeassistant.components.bluetooth.const import DEFAULT_ADDRESS
|
||||
from homeassistant.components.bluetooth.manager import BluetoothManager
|
||||
from homeassistant.core import HomeAssistant
|
||||
@@ -20,38 +25,84 @@ def _get_manager() -> BluetoothManager:
|
||||
return models.MANAGER
|
||||
|
||||
|
||||
def inject_advertisement(device: BLEDevice, adv: AdvertisementData) -> None:
|
||||
def inject_advertisement(
|
||||
hass: HomeAssistant, device: BLEDevice, adv: AdvertisementData
|
||||
) -> None:
|
||||
"""Inject an advertisement into the manager."""
|
||||
return inject_advertisement_with_source(device, adv, SOURCE_LOCAL)
|
||||
return inject_advertisement_with_source(hass, device, adv, SOURCE_LOCAL)
|
||||
|
||||
|
||||
def inject_advertisement_with_source(
|
||||
device: BLEDevice, adv: AdvertisementData, source: str
|
||||
hass: HomeAssistant, device: BLEDevice, adv: AdvertisementData, source: str
|
||||
) -> None:
|
||||
"""Inject an advertisement into the manager from a specific source."""
|
||||
inject_advertisement_with_time_and_source(device, adv, time.monotonic(), source)
|
||||
inject_advertisement_with_time_and_source(
|
||||
hass, device, adv, time.monotonic(), source
|
||||
)
|
||||
|
||||
|
||||
def inject_advertisement_with_time_and_source(
|
||||
device: BLEDevice, adv: AdvertisementData, time: float, source: str
|
||||
hass: HomeAssistant,
|
||||
device: BLEDevice,
|
||||
adv: AdvertisementData,
|
||||
time: float,
|
||||
source: str,
|
||||
) -> None:
|
||||
"""Inject an advertisement into the manager from a specific source at a time."""
|
||||
return _get_manager().scanner_adv_received(device, adv, time, source)
|
||||
inject_advertisement_with_time_and_source_connectable(
|
||||
hass, device, adv, time, source, True
|
||||
)
|
||||
|
||||
|
||||
def inject_advertisement_with_time_and_source_connectable(
|
||||
hass: HomeAssistant,
|
||||
device: BLEDevice,
|
||||
adv: AdvertisementData,
|
||||
time: float,
|
||||
source: str,
|
||||
connectable: bool,
|
||||
) -> None:
|
||||
"""Inject an advertisement into the manager from a specific source at a time and connectable status."""
|
||||
async_get_advertisement_callback(hass)(
|
||||
models.BluetoothServiceInfoBleak(
|
||||
name=adv.local_name or device.name or device.address,
|
||||
address=device.address,
|
||||
rssi=device.rssi,
|
||||
manufacturer_data=adv.manufacturer_data,
|
||||
service_data=adv.service_data,
|
||||
service_uuids=adv.service_uuids,
|
||||
source=source,
|
||||
device=device,
|
||||
advertisement=adv,
|
||||
connectable=connectable,
|
||||
time=time,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def patch_all_discovered_devices(mock_discovered: list[BLEDevice]) -> None:
|
||||
"""Mock all the discovered devices from all the scanners."""
|
||||
manager = _get_manager()
|
||||
return patch.object(
|
||||
manager, "async_all_discovered_devices", return_value=mock_discovered
|
||||
_get_manager(), "async_all_discovered_devices", return_value=mock_discovered
|
||||
)
|
||||
|
||||
|
||||
def patch_history(mock_history: dict[str, models.BluetoothServiceInfoBleak]) -> None:
|
||||
"""Patch the history."""
|
||||
return patch.dict(_get_manager()._history, mock_history)
|
||||
|
||||
|
||||
def patch_connectable_history(
|
||||
mock_history: dict[str, models.BluetoothServiceInfoBleak]
|
||||
) -> None:
|
||||
"""Patch the connectable history."""
|
||||
return patch.dict(_get_manager()._connectable_history, mock_history)
|
||||
|
||||
|
||||
def patch_discovered_devices(mock_discovered: list[BLEDevice]) -> None:
|
||||
"""Mock the combined best path to discovered devices from all the scanners."""
|
||||
manager = _get_manager()
|
||||
return patch.object(
|
||||
manager, "async_discovered_devices", return_value=mock_discovered
|
||||
_get_manager(), "async_discovered_devices", return_value=mock_discovered
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user