From b280874dc022af653bf96f6e9678fe445506e234 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 5 Mar 2025 22:54:48 -1000 Subject: [PATCH] Small cleanups for HomeKit (#139889) * Small cleanups for HomeKit - Add some missing typing - Break out some duplicate code * Small cleanups for HomeKit - Add some missing typing - Break out some duplicate code --- homeassistant/components/homekit/__init__.py | 97 ++++++++++---------- 1 file changed, 46 insertions(+), 51 deletions(-) diff --git a/homeassistant/components/homekit/__init__.py b/homeassistant/components/homekit/__init__.py index 97fb17d7db5..9bd5711832c 100644 --- a/homeassistant/components/homekit/__init__.py +++ b/homeassistant/components/homekit/__init__.py @@ -221,6 +221,34 @@ UNPAIR_SERVICE_SCHEMA = vol.All( ) +@callback +def _async_update_entries_from_yaml( + hass: HomeAssistant, config: ConfigType, start_import_flow: bool +) -> None: + current_entries = hass.config_entries.async_entries(DOMAIN) + entries_by_name, entries_by_port = _async_get_imported_entries_indices( + current_entries + ) + hk_config: list[dict[str, Any]] = config[DOMAIN] + + for index, conf in enumerate(hk_config): + if _async_update_config_entry_from_yaml( + hass, entries_by_name, entries_by_port, conf + ): + continue + + if start_import_flow: + conf[CONF_ENTRY_INDEX] = index + hass.async_create_task( + hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_IMPORT}, + data=conf, + ), + eager_start=True, + ) + + def _async_all_homekit_instances(hass: HomeAssistant) -> list[HomeKit]: """All active HomeKit instances.""" hk_data: HomeKitEntryData | None @@ -258,31 +286,10 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: await hass.async_add_executor_job(get_loader) _async_register_events_and_services(hass) - if DOMAIN not in config: return True - current_entries = hass.config_entries.async_entries(DOMAIN) - entries_by_name, entries_by_port = _async_get_imported_entries_indices( - current_entries - ) - - for index, conf in enumerate(config[DOMAIN]): - if _async_update_config_entry_from_yaml( - hass, entries_by_name, entries_by_port, conf - ): - continue - - conf[CONF_ENTRY_INDEX] = index - hass.async_create_task( - hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_IMPORT}, - data=conf, - ), - eager_start=True, - ) - + _async_update_entries_from_yaml(hass, config, start_import_flow=True) return True @@ -326,13 +333,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: HomeKitConfigEntry) -> b conf = entry.data options = entry.options - name = conf[CONF_NAME] - port = conf[CONF_PORT] - _LOGGER.debug("Begin setup HomeKit for %s", name) - + name: str = conf[CONF_NAME] + port: int = conf[CONF_PORT] # ip_address and advertise_ip are yaml only - ip_address = conf.get(CONF_IP_ADDRESS, _DEFAULT_BIND) - advertise_ips: list[str] = conf.get( + ip_address: str | list[str] | None = conf.get(CONF_IP_ADDRESS, _DEFAULT_BIND) + advertise_ips: list[str] + advertise_ips = conf.get( CONF_ADVERTISE_IP ) or await network.async_get_announce_addresses(hass) @@ -344,13 +350,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: HomeKitConfigEntry) -> b # with users who have not migrated yet we do not do exclude # these entities by default as we cannot migrate automatically # since it requires a re-pairing. - exclude_accessory_mode = conf.get( + exclude_accessory_mode: bool = conf.get( CONF_EXCLUDE_ACCESSORY_MODE, DEFAULT_EXCLUDE_ACCESSORY_MODE ) - homekit_mode = options.get(CONF_HOMEKIT_MODE, DEFAULT_HOMEKIT_MODE) - entity_config = options.get(CONF_ENTITY_CONFIG, {}).copy() - entity_filter = FILTER_SCHEMA(options.get(CONF_FILTER, {})) - devices = options.get(CONF_DEVICES, []) + homekit_mode: str = options.get(CONF_HOMEKIT_MODE, DEFAULT_HOMEKIT_MODE) + entity_config: dict[str, Any] = options.get(CONF_ENTITY_CONFIG, {}).copy() + entity_filter: EntityFilter = FILTER_SCHEMA(options.get(CONF_FILTER, {})) + devices: list[str] = options.get(CONF_DEVICES, []) homekit = HomeKit( hass, @@ -500,26 +506,15 @@ def _async_register_events_and_services(hass: HomeAssistant) -> None: async def _handle_homekit_reload(service: ServiceCall) -> None: """Handle start HomeKit service call.""" config = await async_integration_yaml_config(hass, DOMAIN) - if not config or DOMAIN not in config: return - - current_entries = hass.config_entries.async_entries(DOMAIN) - entries_by_name, entries_by_port = _async_get_imported_entries_indices( - current_entries - ) - - for conf in config[DOMAIN]: - _async_update_config_entry_from_yaml( - hass, entries_by_name, entries_by_port, conf + _async_update_entries_from_yaml(hass, config, start_import_flow=False) + await asyncio.gather( + *( + create_eager_task(hass.config_entries.async_reload(entry.entry_id)) + for entry in hass.config_entries.async_entries(DOMAIN) ) - - reload_tasks = [ - create_eager_task(hass.config_entries.async_reload(entry.entry_id)) - for entry in current_entries - ] - - await asyncio.gather(*reload_tasks) + ) async_register_admin_service( hass, @@ -537,7 +532,7 @@ class HomeKit: hass: HomeAssistant, name: str, port: int, - ip_address: str | None, + ip_address: list[str] | str | None, entity_filter: EntityFilter, exclude_accessory_mode: bool, entity_config: dict[str, Any],