Cancel Bluetooth scanner expire devices timer on unload (#179379)

This commit is contained in:
Bill Church
2026-08-17 13:05:11 -05:00
committed by GitHub
parent dbb57acc9a
commit e8f1f1460e
2 changed files with 29 additions and 1 deletions
@@ -407,7 +407,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
if mode is BluetoothScanningMode.AUTO and not details.get(ADAPTER_PASSIVE_SCAN):
mode = BluetoothScanningMode.ACTIVE
scanner = HaScanner(mode, adapter, address)
scanner.async_setup()
entry.async_on_unload(scanner.async_setup())
if entry.title == address:
hass.config_entries.async_update_entry(
entry, title=adapter_title(adapter, details)
+28
View File
@@ -58,6 +58,7 @@ from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import issue_registry as ir
from homeassistant.setup import async_setup_component
from homeassistant.util import dt as dt_util
from homeassistant.util.async_ import get_scheduled_timer_handles
from . import (
FakeRemoteScanner,
@@ -3274,6 +3275,33 @@ async def test_default_address_config_entries_removed_linux(
assert not hass.config_entries.async_entries(bluetooth.DOMAIN)
@pytest.mark.usefixtures("one_adapter", "mock_bleak_scanner_start")
async def test_unload_cancels_expire_devices_timer(hass: HomeAssistant) -> None:
"""Test unloading an adapter cancels the scanner expire devices timer."""
def _expire_devices_timers() -> list[asyncio.TimerHandle]:
return [
handle
for handle in get_scheduled_timer_handles(hass.loop)
if not handle.cancelled()
and "_async_expire_devices_schedule_next" in repr(handle)
]
entry = MockConfigEntry(
domain=bluetooth.DOMAIN, data={}, unique_id="00:00:00:00:00:01"
)
entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
# The timer must exist first, otherwise the assertion below is vacuous
assert _expire_devices_timers()
assert await hass.config_entries.async_unload(entry.entry_id)
await hass.async_block_till_done()
assert not _expire_devices_timers()
@pytest.mark.usefixtures("one_adapter")
async def test_can_unsetup_bluetooth_single_adapter_linux(
hass: HomeAssistant, mock_bleak_scanner_start: MagicMock