From e880ad7bda012beba55064cf53f1c375ee1d0427 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Wed, 1 Nov 2023 00:18:21 +0100 Subject: [PATCH] Improve reload of legacy groups (#102925) * Improve reload of legacy groups * Simplify reload * Get rid of inner function * Fix logic when there are no group.group entities * Update homeassistant/components/group/__init__.py Co-authored-by: J. Nick Koston * Fix type hints --------- Co-authored-by: J. Nick Koston --- homeassistant/components/group/__init__.py | 27 ++++++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/group/__init__.py b/homeassistant/components/group/__init__.py index 1092bc5834b..ae246041db9 100644 --- a/homeassistant/components/group/__init__.py +++ b/homeassistant/components/group/__init__.py @@ -293,14 +293,31 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: await _async_process_config(hass, config) async def reload_service_handler(service: ServiceCall) -> None: - """Remove all user-defined groups and load new ones from config.""" - auto = [e for e in component.entities if e.created_by_service] + """Group reload handler. - if (conf := await component.async_prepare_reload()) is None: + - Remove group.group entities not created by service calls and set them up again + - Reload xxx.group platforms + """ + if (conf := await component.async_prepare_reload(skip_reset=True)) is None: return - await _async_process_config(hass, conf) - await component.async_add_entities(auto) + # Simplified + modified version of EntityPlatform.async_reset: + # - group.group never retries setup + # - group.group never polls + # - We don't need to reset EntityPlatform._setup_complete + # - Only remove entities which were not created by service calls + tasks = [ + entity.async_remove() + for entity in component.entities + if entity.entity_id.startswith("group.") and not entity.created_by_service + ] + + if tasks: + await asyncio.gather(*tasks) + + component.config = None + + await _async_process_config(hass, conf) await async_reload_integration_platforms(hass, DOMAIN, PLATFORMS)