Speed up creating automations (#93776)

Creating the inner coroutine for each automation to be run at started
was expensive when the user had 1000s of automations
pull/93296/merge
J. Nick Koston 2023-05-29 22:51:35 -05:00 committed by GitHub
parent c129b4d0c2
commit c25ccb90a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 9 deletions

View File

@ -578,6 +578,14 @@ class AutomationEntity(ToggleEntity, RestoreEntity):
await super().async_will_remove_from_hass()
await self.async_disable()
async def _async_enable_automation(self, event: Event) -> None:
"""Start automation on startup."""
# Don't do anything if no longer enabled or already attached
if not self._is_enabled or self._async_detach_triggers is not None:
return
self._async_detach_triggers = await self._async_attach_triggers(True)
async def async_enable(self) -> None:
"""Enable this automation entity.
@ -594,16 +602,8 @@ class AutomationEntity(ToggleEntity, RestoreEntity):
self.async_write_ha_state()
return
async def async_enable_automation(event: Event) -> None:
"""Start automation on startup."""
# Don't do anything if no longer enabled or already attached
if not self._is_enabled or self._async_detach_triggers is not None:
return
self._async_detach_triggers = await self._async_attach_triggers(True)
self.hass.bus.async_listen_once(
EVENT_HOMEASSISTANT_STARTED, async_enable_automation
EVENT_HOMEASSISTANT_STARTED, self._async_enable_automation
)
self.async_write_ha_state()