diff --git a/homeassistant/components/homekit_controller/config_flow.py b/homeassistant/components/homekit_controller/config_flow.py index 592a2301294..7f0f288400d 100644 --- a/homeassistant/components/homekit_controller/config_flow.py +++ b/homeassistant/components/homekit_controller/config_flow.py @@ -272,8 +272,14 @@ class HomekitControllerFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): # Device isn't paired with us or anyone else. # But we have a 'complete' config entry for it - that is probably - # invalid. Remove it automatically. - if not paired and existing_entry: + # invalid. Remove it automatically if it has an accessory pairing id + # (which means it was paired with us at some point) and was not + # ignored by the user. + if ( + not paired + and existing_entry + and (accessory_pairing_id := existing_entry.data.get("AccessoryPairingID")) + ): if self.controller is None: await self._async_setup_controller() @@ -282,7 +288,7 @@ class HomekitControllerFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): assert self.controller pairing = self.controller.load_pairing( - existing_entry.data["AccessoryPairingID"], dict(existing_entry.data) + accessory_pairing_id, dict(existing_entry.data) ) try: diff --git a/tests/components/homekit_controller/test_config_flow.py b/tests/components/homekit_controller/test_config_flow.py index 3412e41aa17..8da6290d914 100644 --- a/tests/components/homekit_controller/test_config_flow.py +++ b/tests/components/homekit_controller/test_config_flow.py @@ -12,7 +12,7 @@ from aiohomekit.model.services import ServicesTypes from bleak.exc import BleakError import pytest -from homeassistant import config_entries +from homeassistant import config_entries, data_entry_flow from homeassistant.components import zeroconf from homeassistant.components.homekit_controller import config_flow from homeassistant.components.homekit_controller.const import KNOWN_DEVICES @@ -485,6 +485,44 @@ async def test_discovery_invalid_config_entry(hass: HomeAssistant, controller) - assert result["type"] == "form" +async def test_discovery_ignored_config_entry(hass: HomeAssistant, controller) -> None: + """There is already a config entry but it is ignored.""" + pairing = await controller.add_paired_device(Accessories(), "00:00:00:00:00:00") + + MockConfigEntry( + domain="homekit_controller", + data={}, + unique_id="00:00:00:00:00:00", + source=config_entries.SOURCE_IGNORE, + ).add_to_hass(hass) + + # We just added a mock config entry so it must be visible in hass + assert len(hass.config_entries.async_entries()) == 1 + + device = setup_mock_accessory(controller) + discovery_info = get_device_discovery_info(device) + + # Device is discovered + with patch.object( + pairing, + "list_accessories_and_characteristics", + side_effect=AuthenticationError("Invalid pairing keys"), + ): + result = await hass.config_entries.flow.async_init( + "homekit_controller", + context={"source": config_entries.SOURCE_ZEROCONF}, + data=discovery_info, + ) + + # Entry is still ignored + config_entry_count = len(hass.config_entries.async_entries()) + assert config_entry_count == 1 + + # We should abort since there is no accessory id in the data + assert result["type"] == data_entry_flow.FlowResultType.ABORT + assert result["reason"] == "already_configured" + + async def test_discovery_already_configured(hass: HomeAssistant, controller) -> None: """Already configured.""" entry = MockConfigEntry(