Bugfix automation fire on bootstrap (#6770)

* Bugfix automation fire on bootstrap

* Add test & fix bug

* fix lint
This commit is contained in:
Pascal Vizeli 2017-03-24 23:52:14 +01:00 committed by Paulus Schoutsen
parent cffc6c7bea
commit f4e9466394
2 changed files with 54 additions and 4 deletions

View File

@ -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."""

View File

@ -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):