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 <nick@koston.org>

* Fix type hints

---------

Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
Erik Montnemery 2023-11-01 00:18:21 +01:00 committed by GitHub
parent 1a6184a9aa
commit e880ad7bda
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 5 deletions

View File

@ -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)