Fix Bluetooth discoveries missing between restarts (#86808)

* Fix Bluetooth discoveries missing between restarts

* do not load other integrations

* coverage
This commit is contained in:
J. Nick Koston 2023-01-27 17:16:16 -10:00 committed by GitHub
parent b69576d6de
commit d0c7f42559
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 4 deletions

View file

@ -209,6 +209,20 @@ class BluetoothManager:
self._bluetooth_adapters, self.storage
)
self.async_setup_unavailable_tracking()
seen: set[str] = set()
for address, service_info in itertools.chain(
self._connectable_history.items(), self._all_history.items()
):
if address in seen:
continue
seen.add(address)
for domain in self._integration_matcher.match_domains(service_info):
discovery_flow.async_create_flow(
self.hass,
domain,
{"source": config_entries.SOURCE_BLUETOOTH},
service_info,
)
@hass_callback
def async_stop(self, event: Event) -> None:

View file

@ -186,3 +186,18 @@ def one_adapter_old_bluez():
},
):
yield
@pytest.fixture(name="disable_new_discovery_flows")
def disable_new_discovery_flows_fixture():
"""Fixture that disables new discovery flows.
We want to disable new discovery flows as we are testing the
BluetoothManager and not the discovery flows. This fixture
will patch the discovery_flow.async_create_flow method to
ensure we do not load other integrations.
"""
with patch(
"homeassistant.components.bluetooth.manager.discovery_flow.async_create_flow"
) as mock_create_flow:
yield mock_create_flow

View file

@ -345,7 +345,9 @@ async def test_base_scanner_connecting_behavior(hass, enable_bluetooth):
unsetup()
async def test_restore_history_remote_adapter(hass, hass_storage):
async def test_restore_history_remote_adapter(
hass, hass_storage, disable_new_discovery_flows
):
"""Test we can restore history for a remote adapter."""
data = hass_storage[storage.REMOTE_SCANNER_STORAGE_KEY] = json_loads(

View file

@ -282,7 +282,9 @@ async def test_switching_adapters_based_on_stale(
)
async def test_restore_history_from_dbus(hass, one_adapter):
async def test_restore_history_from_dbus(
hass, one_adapter, disable_new_discovery_flows
):
"""Test we can restore history from dbus."""
address = "AA:BB:CC:CC:CC:FF"
@ -304,7 +306,7 @@ async def test_restore_history_from_dbus(hass, one_adapter):
async def test_restore_history_from_dbus_and_remote_adapters(
hass, one_adapter, hass_storage
hass, one_adapter, hass_storage, disable_new_discovery_flows
):
"""Test we can restore history from dbus along with remote adapters."""
address = "AA:BB:CC:CC:CC:FF"
@ -337,10 +339,11 @@ async def test_restore_history_from_dbus_and_remote_adapters(
assert (
bluetooth.async_ble_device_from_address(hass, "EB:0B:36:35:6F:A4") is not None
)
assert disable_new_discovery_flows.call_count > 1
async def test_restore_history_from_dbus_and_corrupted_remote_adapters(
hass, one_adapter, hass_storage
hass, one_adapter, hass_storage, disable_new_discovery_flows
):
"""Test we can restore history from dbus when the remote adapters data is corrupted."""
address = "AA:BB:CC:CC:CC:FF"
@ -371,6 +374,7 @@ async def test_restore_history_from_dbus_and_corrupted_remote_adapters(
assert bluetooth.async_ble_device_from_address(hass, address) is not None
assert bluetooth.async_ble_device_from_address(hass, "EB:0B:36:35:6F:A4") is None
assert disable_new_discovery_flows.call_count >= 1
async def test_switching_adapters_based_on_rssi_connectable_to_non_connectable(