Skip to content

Commit

Permalink
Avoid duplicate search for existing config entries in homekit_control…
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Jan 13, 2024
1 parent 7c98c1e commit 0458bd6
Showing 1 changed file with 4 additions and 17 deletions.
21 changes: 4 additions & 17 deletions homeassistant/components/homekit_controller/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

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

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -284,9 +273,7 @@ async def async_step_zeroconf(
# 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()

Expand All @@ -295,7 +282,7 @@ async def async_step_zeroconf(
assert self.controller

pairing = self.controller.load_pairing(
existing.data["AccessoryPairingID"], dict(existing.data)
existing_entry.data["AccessoryPairingID"], dict(existing_entry.data)
)

try:
Expand All @@ -310,7 +297,7 @@ async def async_step_zeroconf(
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(
(
Expand Down

0 comments on commit 0458bd6

Please sign in to comment.