Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions homeassistant/components/automation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
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
Expand Down Expand Up @@ -266,15 +267,31 @@ def is_on(self) -> bool:
@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."""
Expand Down
37 changes: 35 additions & 2 deletions tests/components/automation/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
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

Expand Down Expand Up @@ -568,6 +569,38 @@ def test_reload_config_handles_load_fails(self):
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):
Expand Down