Skip to content

Commit

Permalink
Add bluetooth API to remove scanners that are no longer used (#135408)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Jan 13, 2025
1 parent 0a444de commit c9a7afe
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 0 deletions.
2 changes: 2 additions & 0 deletions homeassistant/components/bluetooth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
async_rediscover_address,
async_register_callback,
async_register_scanner,
async_remove_scanner,
async_scanner_by_source,
async_scanner_count,
async_scanner_devices_by_address,
Expand Down Expand Up @@ -109,6 +110,7 @@
"async_scanner_count",
"async_scanner_devices_by_address",
"async_get_advertisement_callback",
"async_remove_scanner",
"BaseHaScanner",
"HomeAssistantRemoteScanner",
"BluetoothCallbackMatcher",
Expand Down
6 changes: 6 additions & 0 deletions homeassistant/components/bluetooth/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ def async_register_scanner(
return _get_manager(hass).async_register_scanner(scanner, connection_slots)


@hass_callback
def async_remove_scanner(hass: HomeAssistant, source: str) -> None:
"""Permanently remove a BleakScanner by source address."""
return _get_manager(hass).async_remove_scanner(source)


@hass_callback
def async_get_advertisement_callback(
hass: HomeAssistant,
Expand Down
5 changes: 5 additions & 0 deletions homeassistant/components/bluetooth/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,11 @@ def async_register_scanner(
unregister = super().async_register_scanner(scanner, connection_slots)
return partial(self._async_unregister_scanner, scanner, unregister)

@hass_callback
def async_remove_scanner(self, source: str) -> None:
"""Remove a scanner."""
self.storage.async_remove_advertisement_history(source)

@hass_callback
def _handle_config_entry_removed(
self,
Expand Down
6 changes: 6 additions & 0 deletions homeassistant/components/bluetooth/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ def scanners(self) -> list[str]:
"""Get all scanners."""
return list(self._data.keys())

@callback
def async_remove_advertisement_history(self, scanner: str) -> None:
"""Remove discovered devices by scanner."""
if self._data.pop(scanner, None):
self._store.async_delay_save(self._async_get_data, SCANNER_SAVE_DELAY)

@callback
def async_get_advertisement_history(
self, scanner: str
Expand Down
2 changes: 2 additions & 0 deletions tests/components/bluetooth/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ def register_hci0_scanner(hass: HomeAssistant) -> Generator[None]:
cancel = bluetooth.async_register_scanner(hass, hci0_scanner)
yield
cancel()
bluetooth.async_remove_scanner(hass, hci0_scanner.source)


@pytest.fixture
Expand All @@ -328,3 +329,4 @@ def register_hci1_scanner(hass: HomeAssistant) -> Generator[None]:
cancel = bluetooth.async_register_scanner(hass, hci1_scanner)
yield
cancel()
bluetooth.async_remove_scanner(hass, hci1_scanner.source)
18 changes: 18 additions & 0 deletions tests/components/bluetooth/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
SOURCE_LOCAL,
UNAVAILABLE_TRACK_SECONDS,
)
from homeassistant.components.bluetooth.manager import HomeAssistantBluetoothManager
from homeassistant.components.bluetooth.match import (
ADDRESS,
CONNECTABLE,
Expand Down Expand Up @@ -3022,6 +3023,23 @@ async def test_scanner_count_connectable(hass: HomeAssistant) -> None:
cancel()


@pytest.mark.usefixtures("enable_bluetooth")
async def test_scanner_remove(hass: HomeAssistant) -> None:
"""Test permanently removing a scanner."""
scanner = FakeScanner("any", "any")
cancel = bluetooth.async_register_scanner(hass, scanner)
assert bluetooth.async_scanner_count(hass, connectable=True) == 1
device = generate_ble_device("44:44:33:11:23:45", "name")
adv = generate_advertisement_data(local_name="name", service_uuids=[])
inject_advertisement_with_time_and_source_connectable(
hass, device, adv, time.monotonic(), scanner.source, True
)
cancel()
bluetooth.async_remove_scanner(hass, scanner.source)
manager: HomeAssistantBluetoothManager = _get_manager()
assert not manager.storage.async_get_advertisement_history(scanner.source)


@pytest.mark.usefixtures("enable_bluetooth")
async def test_scanner_count(hass: HomeAssistant) -> None:
"""Test getting the connectable and non-connectable scanner count."""
Expand Down

0 comments on commit c9a7afe

Please sign in to comment.