core/tests/components/esphome/test_bluetooth.py
J. Nick Koston 4e5bf5ac22
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
2025-01-12 22:41:49 -05:00

68 lines
2.6 KiB
Python

"""Test the ESPHome bluetooth integration."""
from unittest.mock import patch
from homeassistant.components import bluetooth
from homeassistant.core import HomeAssistant
from .conftest import MockESPHomeDevice
async def test_bluetooth_connect_with_raw_adv(
hass: HomeAssistant, mock_bluetooth_entry_with_raw_adv: MockESPHomeDevice
) -> None:
"""Test bluetooth connect with raw advertisements."""
scanner = bluetooth.async_scanner_by_source(hass, "11:22:33:44:55:AA")
assert scanner is not None
assert scanner.connectable is True
assert scanner.scanning is True
assert scanner.connector.can_connect() is False # no connection slots
await mock_bluetooth_entry_with_raw_adv.mock_disconnect(True)
await hass.async_block_till_done()
scanner = bluetooth.async_scanner_by_source(hass, "11:22:33:44:55:AA")
assert scanner is None
await mock_bluetooth_entry_with_raw_adv.mock_connect()
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_connect_with_legacy_adv(
hass: HomeAssistant, mock_bluetooth_entry_with_legacy_adv: MockESPHomeDevice
) -> None:
"""Test bluetooth connect with legacy advertisements."""
scanner = bluetooth.async_scanner_by_source(hass, "11:22:33:44:55:AA")
assert scanner is not None
assert scanner.connectable is True
assert scanner.scanning is True
assert scanner.connector.can_connect() is False # no connection slots
await mock_bluetooth_entry_with_legacy_adv.mock_disconnect(True)
await hass.async_block_till_done()
scanner = bluetooth.async_scanner_by_source(hass, "11:22:33:44:55:AA")
assert scanner is None
await mock_bluetooth_entry_with_legacy_adv.mock_connect()
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)