Created
August 21, 2019 18:05
-
-
Save balloob/8a0497604e4bb2454922f751d8c8a78e to your computer and use it in GitHub Desktop.
Config Entry Reloader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class EntityRegistryDisabledHandler: | |
"""Handler to handle when entities related to config entries updating disabled_by.""" | |
RELOAD_AFTER_UPDATE_DELAY = 30 | |
def __init__(self, hass: HomeAssistant) -> None: | |
"""Initialize the handler.""" | |
self.hass = hass | |
self.registry: Optional[entity_registry.EntityRegistry] = None | |
self.changed: Set[str] = set() | |
self.remove_call_later: Optional[Callable[[], None]] = None | |
hass.bus.async_listen( | |
entity_registry.EVENT_ENTITY_REGISTRY_UPDATED, self._handle_entry_updated | |
) | |
async def _handle_entry_updated(self, event): | |
"""Handle entity registry entry update.""" | |
if event.data["action"] != "update": | |
return | |
if self.registry is None: | |
self.registry = await entity_registry.async_get_registry(self.hass) | |
entry = self.registry.async_get(event.data["entity_id"]) | |
if entry is None or entry.config_entry_id is None: | |
return | |
if entry.config_entry_id not in changed: | |
# TODO verify that config entry can be reloaded. | |
pass | |
self.changed.add(entry.config_entry_id) | |
if self.remove_call_later: | |
self.remove_call_later() | |
self.remove_call_later = self.hass.helpers.event.async_call_later( | |
self.RELOAD_AFTER_UPDATE_DELAY, self._handle_reload | |
) | |
async def _handle_reload(self, _now): | |
"""Handle a reload.""" | |
self.remove_call_later = None | |
to_reload = self.changed | |
self.changed = set() | |
await asyncio.gather( | |
*[self.hass.config_entries.async_reload(entry_id) for entry_id in to_reload] | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment