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 codepull/138206/merge
parent
aec6868af1
commit
b280874dc0
|
@ -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],
|
||||
|
|
Loading…
Reference in New Issue