diff --git a/homeassistant/components/automation/__init__.py b/homeassistant/components/automation/__init__.py index 96d5b0499d2..451a0c538b8 100644 --- a/homeassistant/components/automation/__init__.py +++ b/homeassistant/components/automation/__init__.py @@ -12,10 +12,11 @@ import os import voluptuous as vol from homeassistant.setup import async_prepare_setup_platform +from homeassistant.core import CoreState from homeassistant import config as conf_util from homeassistant.const import ( ATTR_ENTITY_ID, CONF_PLATFORM, STATE_ON, SERVICE_TURN_ON, SERVICE_TURN_OFF, - SERVICE_TOGGLE, SERVICE_RELOAD) + SERVICE_TOGGLE, SERVICE_RELOAD, EVENT_HOMEASSISTANT_START) from homeassistant.components import logbook from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import extract_domain_configs, script, condition @@ -266,15 +267,31 @@ class AutomationEntity(ToggleEntity): @asyncio.coroutine def async_added_to_hass(self) -> None: """Startup with initial state or previous state.""" + enable_automation = False + state = yield from async_get_last_state(self.hass, self.entity_id) if state is None: if self._initial_state: - yield from self.async_enable() + enable_automation = True else: self._last_triggered = state.attributes.get('last_triggered') if state.state == STATE_ON: + enable_automation = True + + # HomeAssistant is on bootstrap + if enable_automation and self.hass.state == CoreState.not_running: + @asyncio.coroutine + def async_enable_automation(event): + """Start automation on startup.""" yield from self.async_enable() + self.hass.bus.async_listen_once( + EVENT_HOMEASSISTANT_START, async_enable_automation) + + # HomeAssistant is running + elif enable_automation: + yield from self.async_enable() + @asyncio.coroutine def async_turn_on(self, **kwargs) -> None: """Turn the entity on and update the state.""" diff --git a/tests/components/automation/test_init.py b/tests/components/automation/test_init.py index 271aa58f7fb..629ee8e2975 100644 --- a/tests/components/automation/test_init.py +++ b/tests/components/automation/test_init.py @@ -4,10 +4,11 @@ from datetime import timedelta import unittest from unittest.mock import patch -from homeassistant.core import State +from homeassistant.core import State, CoreState from homeassistant.setup import setup_component, async_setup_component import homeassistant.components.automation as automation -from homeassistant.const import ATTR_ENTITY_ID, STATE_ON, STATE_OFF +from homeassistant.const import ( + ATTR_ENTITY_ID, STATE_ON, STATE_OFF, EVENT_HOMEASSISTANT_START) from homeassistant.exceptions import HomeAssistantError import homeassistant.util.dt as dt_util @@ -568,6 +569,38 @@ class TestAutomation(unittest.TestCase): self.hass.block_till_done() assert len(self.calls) == 2 + def test_automation_not_trigger_on_bootstrap(self): + """Test if automation is not trigger on bootstrap.""" + self.hass.state = CoreState.not_running + + assert setup_component(self.hass, automation.DOMAIN, { + automation.DOMAIN: { + 'trigger': { + 'platform': 'event', + 'event_type': 'test_event', + }, + 'action': { + 'service': 'test.automation', + 'entity_id': 'hello.world' + } + } + }) + + self.hass.bus.fire('test_event') + self.hass.block_till_done() + + assert len(self.calls) == 0 + + self.hass.bus.fire(EVENT_HOMEASSISTANT_START) + self.hass.block_till_done() + self.hass.states = CoreState.running + + self.hass.bus.fire('test_event') + self.hass.block_till_done() + + assert len(self.calls) == 1 + assert ['hello.world'] == self.calls[0].data.get(ATTR_ENTITY_ID) + @asyncio.coroutine def test_automation_restore_state(hass):