From 0458bd68d9aad50f5e21700eeb1ce4cb3cfaa511 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 12 Jan 2024 22:11:42 -1000 Subject: [PATCH] Avoid duplicate search for existing config entries in homekit_controller (#107613) --- .../homekit_controller/config_flow.py | 21 ++++--------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/homeassistant/components/homekit_controller/config_flow.py b/homeassistant/components/homekit_controller/config_flow.py index 08444555aca..592a2301294 100644 --- a/homeassistant/components/homekit_controller/config_flow.py +++ b/homeassistant/components/homekit_controller/config_flow.py @@ -20,7 +20,7 @@ import voluptuous as vol from homeassistant import config_entries from homeassistant.components import zeroconf -from homeassistant.core import HomeAssistant, callback +from homeassistant.core import callback from homeassistant.data_entry_flow import AbortFlow, FlowResult from homeassistant.helpers import device_registry as dr @@ -79,17 +79,6 @@ def formatted_category(category: Categories) -> str: return str(category.name).replace("_", " ").title() -@callback -def find_existing_config_entry( - hass: HomeAssistant, upper_case_hkid: str -) -> config_entries.ConfigEntry | None: - """Return a set of the configured hosts.""" - for entry in hass.config_entries.async_entries(DOMAIN): - if entry.data.get("AccessoryPairingID") == upper_case_hkid: - return entry - return None - - def ensure_pin_format(pin: str, allow_insecure_setup_codes: Any = None) -> str: """Ensure a pin code is correctly formatted. @@ -284,9 +273,7 @@ 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 := find_existing_config_entry(self.hass, upper_case_hkid) - ): + if not paired and existing_entry: if self.controller is None: await self._async_setup_controller() @@ -295,7 +282,7 @@ class HomekitControllerFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): assert self.controller pairing = self.controller.load_pairing( - existing.data["AccessoryPairingID"], dict(existing.data) + existing_entry.data["AccessoryPairingID"], dict(existing_entry.data) ) try: @@ -310,7 +297,7 @@ class HomekitControllerFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): model, hkid, ) - await self.hass.config_entries.async_remove(existing.entry_id) + await self.hass.config_entries.async_remove(existing_entry.entry_id) else: _LOGGER.debug( (