diff --git a/homeassistant/components/bluetooth/manager.py b/homeassistant/components/bluetooth/manager.py index 33de20312c0..be107158010 100644 --- a/homeassistant/components/bluetooth/manager.py +++ b/homeassistant/components/bluetooth/manager.py @@ -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: diff --git a/tests/components/bluetooth/conftest.py b/tests/components/bluetooth/conftest.py index ffa353fd0c4..59c5cc822df 100644 --- a/tests/components/bluetooth/conftest.py +++ b/tests/components/bluetooth/conftest.py @@ -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 diff --git a/tests/components/bluetooth/test_base_scanner.py b/tests/components/bluetooth/test_base_scanner.py index 935b35b5863..5c6bbccd443 100644 --- a/tests/components/bluetooth/test_base_scanner.py +++ b/tests/components/bluetooth/test_base_scanner.py @@ -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( diff --git a/tests/components/bluetooth/test_manager.py b/tests/components/bluetooth/test_manager.py index d0ae23a5226..12e48313332 100644 --- a/tests/components/bluetooth/test_manager.py +++ b/tests/components/bluetooth/test_manager.py @@ -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(