Automation: initial state > restore state (#6911)
* Automation: initial state > restore state * Clean up code * Ensure MQTT defaults are used. * Ensure failed platforms always return None * Automation: write state to state machine after start
This commit is contained in:
parent
3895979e39
commit
23645da74c
4 changed files with 235 additions and 103 deletions
|
@ -14,7 +14,7 @@ import homeassistant.util.dt as dt_util
|
|||
|
||||
from tests.common import (
|
||||
assert_setup_component, get_test_home_assistant, fire_time_changed,
|
||||
mock_component, mock_service, mock_restore_cache)
|
||||
mock_service, mock_restore_cache)
|
||||
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
|
@ -24,7 +24,6 @@ class TestAutomation(unittest.TestCase):
|
|||
def setUp(self):
|
||||
"""Setup things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
mock_component(self.hass, 'group')
|
||||
self.calls = mock_service(self.hass, 'test', 'automation')
|
||||
|
||||
def tearDown(self):
|
||||
|
@ -156,46 +155,6 @@ class TestAutomation(unittest.TestCase):
|
|||
self.assertEqual(['hello.world'],
|
||||
self.calls[0].data.get(ATTR_ENTITY_ID))
|
||||
|
||||
def test_service_initial_value_off(self):
|
||||
"""Test initial value off."""
|
||||
entity_id = 'automation.hello'
|
||||
|
||||
assert setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'alias': 'hello',
|
||||
'initial_state': 'off',
|
||||
'trigger': {
|
||||
'platform': 'event',
|
||||
'event_type': 'test_event',
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation',
|
||||
'entity_id': ['hello.world', 'hello.world2']
|
||||
}
|
||||
}
|
||||
})
|
||||
assert not automation.is_on(self.hass, entity_id)
|
||||
|
||||
def test_service_initial_value_on(self):
|
||||
"""Test initial value on."""
|
||||
entity_id = 'automation.hello'
|
||||
|
||||
assert setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'alias': 'hello',
|
||||
'initial_state': 'on',
|
||||
'trigger': {
|
||||
'platform': 'event',
|
||||
'event_type': 'test_event',
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation',
|
||||
'entity_id': ['hello.world', 'hello.world2']
|
||||
}
|
||||
}
|
||||
})
|
||||
assert automation.is_on(self.hass, entity_id)
|
||||
|
||||
def test_service_specify_entity_id_list(self):
|
||||
"""Test service data."""
|
||||
assert setup_component(self.hass, automation.DOMAIN, {
|
||||
|
@ -569,38 +528,6 @@ 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):
|
||||
|
@ -653,3 +580,209 @@ def test_automation_restore_state(hass):
|
|||
yield from hass.async_block_till_done()
|
||||
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_initial_value_off(hass):
|
||||
"""Test initial value off."""
|
||||
calls = mock_service(hass, 'test', 'automation')
|
||||
|
||||
res = yield from async_setup_component(hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'alias': 'hello',
|
||||
'initial_state': 'off',
|
||||
'trigger': {
|
||||
'platform': 'event',
|
||||
'event_type': 'test_event',
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation',
|
||||
'entity_id': 'hello.world'
|
||||
}
|
||||
}
|
||||
})
|
||||
assert res
|
||||
assert not automation.is_on(hass, 'automation.hello')
|
||||
|
||||
hass.bus.async_fire('test_event')
|
||||
yield from hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_initial_value_on(hass):
|
||||
"""Test initial value on."""
|
||||
calls = mock_service(hass, 'test', 'automation')
|
||||
|
||||
res = yield from async_setup_component(hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'alias': 'hello',
|
||||
'initial_state': 'on',
|
||||
'trigger': {
|
||||
'platform': 'event',
|
||||
'event_type': 'test_event',
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation',
|
||||
'entity_id': ['hello.world', 'hello.world2']
|
||||
}
|
||||
}
|
||||
})
|
||||
assert res
|
||||
assert automation.is_on(hass, 'automation.hello')
|
||||
|
||||
hass.bus.async_fire('test_event')
|
||||
yield from hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_initial_value_off_but_restore_on(hass):
|
||||
"""Test initial value off and restored state is turned on."""
|
||||
calls = mock_service(hass, 'test', 'automation')
|
||||
mock_restore_cache(hass, (
|
||||
State('automation.hello', STATE_ON),
|
||||
))
|
||||
|
||||
res = yield from async_setup_component(hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'alias': 'hello',
|
||||
'initial_state': 'off',
|
||||
'trigger': {
|
||||
'platform': 'event',
|
||||
'event_type': 'test_event',
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation',
|
||||
'entity_id': 'hello.world'
|
||||
}
|
||||
}
|
||||
})
|
||||
assert res
|
||||
assert not automation.is_on(hass, 'automation.hello')
|
||||
|
||||
hass.bus.async_fire('test_event')
|
||||
yield from hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_initial_value_on_but_restore_off(hass):
|
||||
"""Test initial value on and restored state is turned off."""
|
||||
calls = mock_service(hass, 'test', 'automation')
|
||||
mock_restore_cache(hass, (
|
||||
State('automation.hello', STATE_OFF),
|
||||
))
|
||||
|
||||
res = yield from async_setup_component(hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'alias': 'hello',
|
||||
'initial_state': 'on',
|
||||
'trigger': {
|
||||
'platform': 'event',
|
||||
'event_type': 'test_event',
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation',
|
||||
'entity_id': 'hello.world'
|
||||
}
|
||||
}
|
||||
})
|
||||
assert res
|
||||
assert automation.is_on(hass, 'automation.hello')
|
||||
|
||||
hass.bus.async_fire('test_event')
|
||||
yield from hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_no_initial_value_and_restore_off(hass):
|
||||
"""Test initial value off and restored state is turned on."""
|
||||
calls = mock_service(hass, 'test', 'automation')
|
||||
mock_restore_cache(hass, (
|
||||
State('automation.hello', STATE_OFF),
|
||||
))
|
||||
|
||||
res = yield from async_setup_component(hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'alias': 'hello',
|
||||
'trigger': {
|
||||
'platform': 'event',
|
||||
'event_type': 'test_event',
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation',
|
||||
'entity_id': 'hello.world'
|
||||
}
|
||||
}
|
||||
})
|
||||
assert res
|
||||
assert not automation.is_on(hass, 'automation.hello')
|
||||
|
||||
hass.bus.async_fire('test_event')
|
||||
yield from hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_automation_is_on_if_no_initial_state_or_restore(hass):
|
||||
"""Test initial value is on when no initial state or restored state."""
|
||||
calls = mock_service(hass, 'test', 'automation')
|
||||
|
||||
res = yield from async_setup_component(hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'alias': 'hello',
|
||||
'trigger': {
|
||||
'platform': 'event',
|
||||
'event_type': 'test_event',
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation',
|
||||
'entity_id': 'hello.world'
|
||||
}
|
||||
}
|
||||
})
|
||||
assert res
|
||||
assert automation.is_on(hass, 'automation.hello')
|
||||
|
||||
hass.bus.async_fire('test_event')
|
||||
yield from hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_automation_not_trigger_on_bootstrap(hass):
|
||||
"""Test if automation is not trigger on bootstrap."""
|
||||
hass.state = CoreState.not_running
|
||||
calls = mock_service(hass, 'test', 'automation')
|
||||
|
||||
res = yield from async_setup_component(hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'alias': 'hello',
|
||||
'trigger': {
|
||||
'platform': 'event',
|
||||
'event_type': 'test_event',
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation',
|
||||
'entity_id': 'hello.world'
|
||||
}
|
||||
}
|
||||
})
|
||||
assert res
|
||||
assert not automation.is_on(hass, 'automation.hello')
|
||||
|
||||
hass.bus.async_fire('test_event')
|
||||
yield from hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
|
||||
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
|
||||
yield from hass.async_block_till_done()
|
||||
assert automation.is_on(hass, 'automation.hello')
|
||||
|
||||
hass.bus.async_fire('test_event')
|
||||
yield from hass.async_block_till_done()
|
||||
|
||||
assert len(calls) == 1
|
||||
assert ['hello.world'] == calls[0].data.get(ATTR_ENTITY_ID)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue