diff --git a/homeassistant/components/bluetooth/manager.py b/homeassistant/components/bluetooth/manager.py index 1523f41bf1f..5d0a8edabd9 100644 --- a/homeassistant/components/bluetooth/manager.py +++ b/homeassistant/components/bluetooth/manager.py @@ -216,13 +216,7 @@ class BluetoothManager: 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, - ) + self._async_trigger_matching_discovery(service_info) @hass_callback def async_stop(self, event: Event) -> None: @@ -649,10 +643,27 @@ class BluetoothManager: """Return the last service info for an address.""" return self._get_history_by_type(connectable).get(address) + def _async_trigger_matching_discovery( + self, service_info: BluetoothServiceInfoBleak + ) -> None: + """Trigger discovery for matching domains.""" + 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_rediscover_address(self, address: str) -> None: """Trigger discovery of devices which have already been seen.""" self._integration_matcher.async_clear_address(address) + if service_info := self._connectable_history.get(address): + self._async_trigger_matching_discovery(service_info) + return + if service_info := self._all_history.get(address): + self._async_trigger_matching_discovery(service_info) def _get_scanners_by_type(self, connectable: bool) -> list[BaseHaScanner]: """Return the scanners by type.""" diff --git a/tests/components/bluetooth/test_init.py b/tests/components/bluetooth/test_init.py index 7a0a7de8442..016da6fc135 100644 --- a/tests/components/bluetooth/test_init.py +++ b/tests/components/bluetooth/test_init.py @@ -980,7 +980,7 @@ async def test_rediscovery(hass, mock_bleak_scanner_start, enable_bluetooth): inject_advertisement(hass, switchbot_device, switchbot_adv_2) await hass.async_block_till_done() - assert len(mock_config_flow.mock_calls) == 2 + assert len(mock_config_flow.mock_calls) == 3 assert mock_config_flow.mock_calls[1][1][0] == "switchbot"