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
This commit is contained in:
J. Nick Koston 2025-03-05 22:54:48 -10:00 committed by GitHub
parent aec6868af1
commit b280874dc0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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]: def _async_all_homekit_instances(hass: HomeAssistant) -> list[HomeKit]:
"""All active HomeKit instances.""" """All active HomeKit instances."""
hk_data: HomeKitEntryData | None 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) await hass.async_add_executor_job(get_loader)
_async_register_events_and_services(hass) _async_register_events_and_services(hass)
if DOMAIN not in config: if DOMAIN not in config:
return True return True
current_entries = hass.config_entries.async_entries(DOMAIN) _async_update_entries_from_yaml(hass, config, start_import_flow=True)
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,
)
return True return True
@ -326,13 +333,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: HomeKitConfigEntry) -> b
conf = entry.data conf = entry.data
options = entry.options options = entry.options
name = conf[CONF_NAME] name: str = conf[CONF_NAME]
port = conf[CONF_PORT] port: int = conf[CONF_PORT]
_LOGGER.debug("Begin setup HomeKit for %s", name)
# ip_address and advertise_ip are yaml only # ip_address and advertise_ip are yaml only
ip_address = conf.get(CONF_IP_ADDRESS, _DEFAULT_BIND) ip_address: str | list[str] | None = conf.get(CONF_IP_ADDRESS, _DEFAULT_BIND)
advertise_ips: list[str] = conf.get( advertise_ips: list[str]
advertise_ips = conf.get(
CONF_ADVERTISE_IP CONF_ADVERTISE_IP
) or await network.async_get_announce_addresses(hass) ) 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 # with users who have not migrated yet we do not do exclude
# these entities by default as we cannot migrate automatically # these entities by default as we cannot migrate automatically
# since it requires a re-pairing. # 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 CONF_EXCLUDE_ACCESSORY_MODE, DEFAULT_EXCLUDE_ACCESSORY_MODE
) )
homekit_mode = options.get(CONF_HOMEKIT_MODE, DEFAULT_HOMEKIT_MODE) homekit_mode: str = options.get(CONF_HOMEKIT_MODE, DEFAULT_HOMEKIT_MODE)
entity_config = options.get(CONF_ENTITY_CONFIG, {}).copy() entity_config: dict[str, Any] = options.get(CONF_ENTITY_CONFIG, {}).copy()
entity_filter = FILTER_SCHEMA(options.get(CONF_FILTER, {})) entity_filter: EntityFilter = FILTER_SCHEMA(options.get(CONF_FILTER, {}))
devices = options.get(CONF_DEVICES, []) devices: list[str] = options.get(CONF_DEVICES, [])
homekit = HomeKit( homekit = HomeKit(
hass, hass,
@ -500,26 +506,15 @@ def _async_register_events_and_services(hass: HomeAssistant) -> None:
async def _handle_homekit_reload(service: ServiceCall) -> None: async def _handle_homekit_reload(service: ServiceCall) -> None:
"""Handle start HomeKit service call.""" """Handle start HomeKit service call."""
config = await async_integration_yaml_config(hass, DOMAIN) config = await async_integration_yaml_config(hass, DOMAIN)
if not config or DOMAIN not in config: if not config or DOMAIN not in config:
return return
_async_update_entries_from_yaml(hass, config, start_import_flow=False)
current_entries = hass.config_entries.async_entries(DOMAIN) await asyncio.gather(
entries_by_name, entries_by_port = _async_get_imported_entries_indices( *(
current_entries create_eager_task(hass.config_entries.async_reload(entry.entry_id))
) for entry in hass.config_entries.async_entries(DOMAIN)
for conf in config[DOMAIN]:
_async_update_config_entry_from_yaml(
hass, entries_by_name, entries_by_port, conf
) )
)
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( async_register_admin_service(
hass, hass,
@ -537,7 +532,7 @@ class HomeKit:
hass: HomeAssistant, hass: HomeAssistant,
name: str, name: str,
port: int, port: int,
ip_address: str | None, ip_address: list[str] | str | None,
entity_filter: EntityFilter, entity_filter: EntityFilter,
exclude_accessory_mode: bool, exclude_accessory_mode: bool,
entity_config: dict[str, Any], entity_config: dict[str, Any],