Add Home Assistant Started event (#34657)

This commit is contained in:
Paulus Schoutsen committed 2020-04-24 14:20:55 -07:00
1 parent f3f2aa807b
commit 214d6f5cf5
8 files changed
+68 -25

No files matched your search

@@ -14,7 +14,7 @@ from homeassistant.const import (
CONF_PLATFORM,
CONF_ZONE,
EVENT_AUTOMATION_TRIGGERED,
EVENT_HOMEASSISTANT_START,
EVENT_HOMEASSISTANT_STARTED,
SERVICE_RELOAD,
SERVICE_TOGGLE,
SERVICE_TURN_OFF,
@@ -408,7 +408,7 @@ class AutomationEntity(ToggleEntity, RestoreEntity):
# HomeAssistant is starting up
if self.hass.state != CoreState.not_running:
self._async_detach_triggers = await self._async_attach_triggers()
self._async_detach_triggers = await self._async_attach_triggers(False)
self.async_write_ha_state()
return
@@ -418,10 +418,10 @@ class AutomationEntity(ToggleEntity, RestoreEntity):
if not self._is_enabled or self._async_detach_triggers is not None:
return
self._async_detach_triggers = await self._async_attach_triggers()
self._async_detach_triggers = await self._async_attach_triggers(True)
self.hass.bus.async_listen_once(
EVENT_HOMEASSISTANT_START, async_enable_automation
EVENT_HOMEASSISTANT_STARTED, async_enable_automation
)
self.async_write_ha_state()
@@ -438,15 +438,17 @@ class AutomationEntity(ToggleEntity, RestoreEntity):
self.async_write_ha_state()
async def _async_attach_triggers(self):
async def _async_attach_triggers(
self, home_assistant_start: bool
) -> Optional[Callable[[], None]]:
"""Set up the triggers."""
removes = []
info = {"name": self._name}
info = {"name": self._name, "home_assistant_start": home_assistant_start}
for conf in self._trigger_config:
platform = importlib.import_module(f".{conf[CONF_PLATFORM]}", __name__)
remove = await platform.async_attach_trigger(
remove = await platform.async_attach_trigger( # type: ignore
self.hass, conf, self.async_trigger, info
)
@@ -4,7 +4,7 @@ import logging
import voluptuous as vol
from homeassistant.const import CONF_EVENT, CONF_PLATFORM, EVENT_HOMEASSISTANT_STOP
from homeassistant.core import CoreState, callback
from homeassistant.core import callback
# mypy: allow-untyped-defs
@@ -40,7 +40,7 @@ async def async_attach_trigger(hass, config, action, automation_info):
# Automation are enabled while hass is starting up, fire right away
# Check state because a config reload shouldn't trigger it.
if hass.state == CoreState.starting:
if automation_info["home_assistant_start"]:
hass.async_run_job(
action({"trigger": {"platform": "homeassistant", "event": event}})
)
@@ -6,7 +6,12 @@ from hass_nabucasa import cloud_api
from hass_nabucasa.google_report_state import ErrorResponse
from homeassistant.components.google_assistant.helpers import AbstractConfig
from homeassistant.const import CLOUD_NEVER_EXPOSED_ENTITIES, HTTP_OK
from homeassistant.const import (
CLOUD_NEVER_EXPOSED_ENTITIES,
EVENT_HOMEASSISTANT_STARTED,
HTTP_OK,
)
from homeassistant.core import CoreState, callback
from homeassistant.helpers import entity_registry
from .const import (
@@ -32,6 +37,7 @@ class CloudGoogleConfig(AbstractConfig):
self._cloud = cloud
self._cur_entity_prefs = self._prefs.google_entity_configs
self._sync_entities_lock = asyncio.Lock()
self._sync_on_started = False
@property
def enabled(self):
@@ -169,6 +175,21 @@ class CloudGoogleConfig(AbstractConfig):
entity_id = event.data["entity_id"]
# Schedule a sync if a change was made to an entity that Google knows about
if self._should_expose_entity_id(entity_id):
if not self._should_expose_entity_id(entity_id):
return
if self.hass.state == CoreState.running:
self.async_schedule_google_sync_all()
return
if self._sync_on_started:
return
self._sync_on_started = True
@callback
async def sync_google(_):
"""Sync entities to Google."""
await self.async_sync_entities_all()
self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STARTED, sync_google)