diff --git a/homeassistant/components/homekit_controller/config_flow.py b/homeassistant/components/homekit_controller/config_flow.py index 38a41617c6a..fcf83918fda 100644 --- a/homeassistant/components/homekit_controller/config_flow.py +++ b/homeassistant/components/homekit_controller/config_flow.py @@ -18,8 +18,6 @@ from .const import DOMAIN, KNOWN_DEVICES HOMEKIT_DIR = ".homekit" HOMEKIT_BRIDGE_DOMAIN = "homekit" -HOMEKIT_BRIDGE_SERIAL_NUMBER = "homekit.bridge" -HOMEKIT_BRIDGE_MODEL = "Home Assistant HomeKit Bridge" HOMEKIT_IGNORE = [ # eufy Indoor Cam 2K and 2K Pan & Tilt @@ -181,8 +179,8 @@ class HomekitControllerFlowHandler(config_entries.ConfigFlow): return self.async_abort(reason="no_devices") - async def _hkid_is_homekit_bridge(self, hkid): - """Determine if the device is a homekit bridge.""" + async def _hkid_is_homekit(self, hkid): + """Determine if the device is a homekit bridge or accessory.""" dev_reg = await async_get_device_registry(self.hass) device = dev_reg.async_get_device( identifiers=set(), connections={(CONNECTION_NETWORK_MAC, hkid)} @@ -190,7 +188,13 @@ class HomekitControllerFlowHandler(config_entries.ConfigFlow): if device is None: return False - return device.model == HOMEKIT_BRIDGE_MODEL + + for entry_id in device.config_entries: + entry = self.hass.config_entries.async_get_entry(entry_id) + if entry and entry.domain == HOMEKIT_BRIDGE_DOMAIN: + return True + + return False async def async_step_zeroconf(self, discovery_info): """Handle a discovered HomeKit accessory. @@ -266,8 +270,8 @@ class HomekitControllerFlowHandler(config_entries.ConfigFlow): if model in HOMEKIT_IGNORE: return self.async_abort(reason="ignored_model") - # If this is a HomeKit bridge exported by *this* HA instance ignore it. - if await self._hkid_is_homekit_bridge(hkid): + # If this is a HomeKit bridge/accessory exported by *this* HA instance ignore it. + if await self._hkid_is_homekit(hkid): return self.async_abort(reason="ignored_model") self.name = name diff --git a/tests/components/homekit_controller/test_config_flow.py b/tests/components/homekit_controller/test_config_flow.py index 9cc785f85fb..42903a53062 100644 --- a/tests/components/homekit_controller/test_config_flow.py +++ b/tests/components/homekit_controller/test_config_flow.py @@ -269,25 +269,18 @@ async def test_discovery_ignored_model(hass, controller): async def test_discovery_ignored_hk_bridge(hass, controller): - """Already paired.""" + """Ensure we ignore homekit bridges and accessories created by the homekit integration.""" device = setup_mock_accessory(controller) discovery_info = get_device_discovery_info(device) config_entry = MockConfigEntry(domain=config_flow.HOMEKIT_BRIDGE_DOMAIN, data={}) + config_entry.add_to_hass(hass) formatted_mac = device_registry.format_mac("AA:BB:CC:DD:EE:FF") dev_reg = mock_device_registry(hass) dev_reg.async_get_or_create( config_entry_id=config_entry.entry_id, - identifiers={ - ( - config_flow.HOMEKIT_BRIDGE_DOMAIN, - config_entry.entry_id, - config_flow.HOMEKIT_BRIDGE_SERIAL_NUMBER, - ) - }, connections={(device_registry.CONNECTION_NETWORK_MAC, formatted_mac)}, - model=config_flow.HOMEKIT_BRIDGE_MODEL, ) discovery_info["properties"]["id"] = "AA:BB:CC:DD:EE:FF" @@ -300,6 +293,30 @@ async def test_discovery_ignored_hk_bridge(hass, controller): assert result["reason"] == "ignored_model" +async def test_discovery_does_not_ignore_non_homekit(hass, controller): + """Do not ignore devices that are not from the homekit integration.""" + device = setup_mock_accessory(controller) + discovery_info = get_device_discovery_info(device) + + config_entry = MockConfigEntry(domain="not_homekit", data={}) + config_entry.add_to_hass(hass) + formatted_mac = device_registry.format_mac("AA:BB:CC:DD:EE:FF") + + dev_reg = mock_device_registry(hass) + dev_reg.async_get_or_create( + config_entry_id=config_entry.entry_id, + connections={(device_registry.CONNECTION_NETWORK_MAC, formatted_mac)}, + ) + + discovery_info["properties"]["id"] = "AA:BB:CC:DD:EE:FF" + + # Device is discovered + result = await hass.config_entries.flow.async_init( + "homekit_controller", context={"source": "zeroconf"}, data=discovery_info + ) + assert result["type"] == "form" + + async def test_discovery_invalid_config_entry(hass, controller): """There is already a config entry for the pairing id but it's invalid.""" MockConfigEntry(