Ensure ESPHome cleanups Bluetooth scanner data upon removal (#135470)

* Add bluetooth API to remove scanners that are no longer used

- Cleanup the advertisment history right away when a scanner is removed

In the future we will do some additional cleanup

* coverage

* finish tests

* Ensure ESPHome cleanups Bluetooth scanner data upon removal

needs https://github.com/home-assistant/core/pull/135408
This commit is contained in:
J. Nick Koston 2025-01-12 17:41:49 -10:00 committed by GitHub
parent 2e5e2c50dd
commit 4e5bf5ac22
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 27 additions and 1 deletions

View File

@ -5,6 +5,7 @@ from __future__ import annotations
from aioesphomeapi import APIClient
from homeassistant.components import ffmpeg, zeroconf
from homeassistant.components.bluetooth import async_remove_scanner
from homeassistant.const import (
CONF_HOST,
CONF_PASSWORD,
@ -86,4 +87,6 @@ async def async_unload_entry(hass: HomeAssistant, entry: ESPHomeConfigEntry) ->
async def async_remove_entry(hass: HomeAssistant, entry: ESPHomeConfigEntry) -> None:
"""Remove an esphome config entry."""
if mac_address := entry.unique_id:
async_remove_scanner(hass, mac_address.upper())
await DomainData.get(hass).get_or_create_store(hass, entry).async_remove()

View File

@ -24,7 +24,7 @@ from aioesphomeapi import (
from awesomeversion import AwesomeVersion
import voluptuous as vol
from homeassistant.components import tag, zeroconf
from homeassistant.components import bluetooth, tag, zeroconf
from homeassistant.const import (
ATTR_DEVICE_ID,
CONF_MODE,
@ -425,6 +425,8 @@ class ESPHomeManager:
entry_data.disconnect_callbacks.add(
async_connect_scanner(hass, entry_data, cli, device_info)
)
else:
bluetooth.async_remove_scanner(hass, device_info.mac_address)
if device_info.voice_assistant_feature_flags_compat(api_version) and (
Platform.ASSIST_SATELLITE not in entry_data.loaded_platforms

View File

@ -1,5 +1,7 @@
"""Test the ESPHome bluetooth integration."""
from unittest.mock import patch
from homeassistant.components import bluetooth
from homeassistant.core import HomeAssistant
@ -44,3 +46,22 @@ async def test_bluetooth_connect_with_legacy_adv(
await hass.async_block_till_done()
scanner = bluetooth.async_scanner_by_source(hass, "11:22:33:44:55:AA")
assert scanner.scanning is True
async def test_bluetooth_cleanup_on_remove_entry(
hass: HomeAssistant, mock_bluetooth_entry_with_raw_adv: MockESPHomeDevice
) -> None:
"""Test bluetooth is cleaned up on entry removal."""
scanner = bluetooth.async_scanner_by_source(hass, "11:22:33:44:55:AA")
assert scanner.connectable is True
await hass.config_entries.async_unload(
mock_bluetooth_entry_with_raw_adv.entry.entry_id
)
with patch("homeassistant.components.esphome.async_remove_scanner") as remove_mock:
await hass.config_entries.async_remove(
mock_bluetooth_entry_with_raw_adv.entry.entry_id
)
await hass.async_block_till_done()
remove_mock.assert_called_once_with(hass, scanner.source)