2016-03-09 09:25:50 +00:00
|
|
|
"""The tests for the automation component."""
|
2017-03-04 23:19:01 +00:00
|
|
|
import asyncio
|
2016-11-29 16:45:04 +00:00
|
|
|
from datetime import timedelta
|
2017-03-04 23:19:01 +00:00
|
|
|
import unittest
|
2016-08-26 06:25:57 +00:00
|
|
|
from unittest.mock import patch
|
2015-08-11 06:11:46 +00:00
|
|
|
|
2017-03-24 22:52:14 +00:00
|
|
|
from homeassistant.core import State, CoreState
|
2017-03-05 09:41:54 +00:00
|
|
|
from homeassistant.setup import setup_component, async_setup_component
|
2015-08-11 06:11:46 +00:00
|
|
|
import homeassistant.components.automation as automation
|
2017-03-24 22:52:14 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_ENTITY_ID, STATE_ON, STATE_OFF, EVENT_HOMEASSISTANT_START)
|
2016-09-04 15:15:52 +00:00
|
|
|
from homeassistant.exceptions import HomeAssistantError
|
2016-08-26 06:25:57 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
2015-08-11 06:11:46 +00:00
|
|
|
|
2017-03-04 23:19:01 +00:00
|
|
|
from tests.common import (
|
|
|
|
assert_setup_component, get_test_home_assistant, fire_time_changed,
|
|
|
|
mock_component, mock_service, mock_restore_cache)
|
2016-02-14 23:08:23 +00:00
|
|
|
|
2015-08-11 06:11:46 +00:00
|
|
|
|
2016-12-02 05:45:19 +00:00
|
|
|
# pylint: disable=invalid-name
|
2015-09-19 15:43:56 +00:00
|
|
|
class TestAutomation(unittest.TestCase):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the event automation."""
|
2015-08-11 06:11:46 +00:00
|
|
|
|
2016-10-08 18:27:35 +00:00
|
|
|
def setUp(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Setup things to be run when tests are started."""
|
2016-02-14 23:08:23 +00:00
|
|
|
self.hass = get_test_home_assistant()
|
2017-03-01 04:33:19 +00:00
|
|
|
mock_component(self.hass, 'group')
|
2017-03-04 23:19:01 +00:00
|
|
|
self.calls = mock_service(self.hass, 'test', 'automation')
|
2015-08-11 06:11:46 +00:00
|
|
|
|
2016-12-02 05:45:19 +00:00
|
|
|
def tearDown(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Stop everything that was started."""
|
2015-08-11 06:11:46 +00:00
|
|
|
self.hass.stop()
|
|
|
|
|
2015-09-19 15:43:56 +00:00
|
|
|
def test_service_data_not_a_dict(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test service data not dict."""
|
2016-10-08 18:27:35 +00:00
|
|
|
with assert_setup_component(0):
|
|
|
|
assert not setup_component(self.hass, automation.DOMAIN, {
|
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'event',
|
|
|
|
'event_type': 'test_event',
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation',
|
|
|
|
'data': 100,
|
|
|
|
}
|
2015-09-19 15:43:56 +00:00
|
|
|
}
|
2016-10-08 18:27:35 +00:00
|
|
|
})
|
2015-09-19 15:43:56 +00:00
|
|
|
|
2015-08-11 06:11:46 +00:00
|
|
|
def test_service_specify_data(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test service data."""
|
2016-10-01 08:22:13 +00:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2015-08-11 06:11:46 +00:00
|
|
|
automation.DOMAIN: {
|
2016-08-26 06:25:57 +00:00
|
|
|
'alias': 'hello',
|
2015-09-15 05:05:40 +00:00
|
|
|
'trigger': {
|
|
|
|
'platform': 'event',
|
|
|
|
'event_type': 'test_event',
|
|
|
|
},
|
|
|
|
'action': {
|
2015-09-19 15:43:56 +00:00
|
|
|
'service': 'test.automation',
|
2016-04-21 20:59:42 +00:00
|
|
|
'data_template': {
|
|
|
|
'some': '{{ trigger.platform }} - '
|
|
|
|
'{{ trigger.event.event_type }}'
|
|
|
|
},
|
2015-09-15 05:05:40 +00:00
|
|
|
}
|
2015-08-11 06:11:46 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2016-08-26 06:25:57 +00:00
|
|
|
time = dt_util.utcnow()
|
|
|
|
|
|
|
|
with patch('homeassistant.components.automation.utcnow',
|
|
|
|
return_value=time):
|
|
|
|
self.hass.bus.fire('test_event')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-08-26 06:25:57 +00:00
|
|
|
assert len(self.calls) == 1
|
2016-10-08 18:27:35 +00:00
|
|
|
assert self.calls[0].data['some'] == 'event - test_event'
|
2016-08-26 06:25:57 +00:00
|
|
|
state = self.hass.states.get('automation.hello')
|
|
|
|
assert state is not None
|
|
|
|
assert state.attributes.get('last_triggered') == time
|
2015-08-11 06:11:46 +00:00
|
|
|
|
2016-10-05 04:20:48 +00:00
|
|
|
state = self.hass.states.get('group.all_automations')
|
|
|
|
assert state is not None
|
|
|
|
assert state.attributes.get('entity_id') == ('automation.hello',)
|
|
|
|
|
2016-11-29 16:45:04 +00:00
|
|
|
def test_action_delay(self):
|
|
|
|
"""Test action delay."""
|
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
|
|
|
automation.DOMAIN: {
|
|
|
|
'alias': 'hello',
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'event',
|
|
|
|
'event_type': 'test_event',
|
|
|
|
},
|
|
|
|
'action': [
|
|
|
|
{
|
|
|
|
'service': 'test.automation',
|
|
|
|
'data_template': {
|
|
|
|
'some': '{{ trigger.platform }} - '
|
|
|
|
'{{ trigger.event.event_type }}'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{'delay': {'minutes': '10'}},
|
|
|
|
{
|
|
|
|
'service': 'test.automation',
|
|
|
|
'data_template': {
|
|
|
|
'some': '{{ trigger.platform }} - '
|
|
|
|
'{{ trigger.event.event_type }}'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
time = dt_util.utcnow()
|
|
|
|
|
|
|
|
with patch('homeassistant.components.automation.utcnow',
|
|
|
|
return_value=time):
|
|
|
|
self.hass.bus.fire('test_event')
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
|
|
|
assert len(self.calls) == 1
|
|
|
|
assert self.calls[0].data['some'] == 'event - test_event'
|
|
|
|
|
|
|
|
future = dt_util.utcnow() + timedelta(minutes=10)
|
|
|
|
fire_time_changed(self.hass, future)
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
|
|
|
assert len(self.calls) == 2
|
|
|
|
assert self.calls[1].data['some'] == 'event - test_event'
|
|
|
|
|
|
|
|
state = self.hass.states.get('automation.hello')
|
|
|
|
assert state is not None
|
|
|
|
assert state.attributes.get('last_triggered') == time
|
|
|
|
state = self.hass.states.get('group.all_automations')
|
|
|
|
assert state is not None
|
|
|
|
assert state.attributes.get('entity_id') == ('automation.hello',)
|
|
|
|
|
2015-08-11 06:11:46 +00:00
|
|
|
def test_service_specify_entity_id(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test service data."""
|
2016-10-01 08:22:13 +00:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2015-08-11 06:11:46 +00:00
|
|
|
automation.DOMAIN: {
|
2015-09-15 05:05:40 +00:00
|
|
|
'trigger': {
|
|
|
|
'platform': 'event',
|
|
|
|
'event_type': 'test_event',
|
|
|
|
},
|
|
|
|
'action': {
|
2015-09-19 15:43:56 +00:00
|
|
|
'service': 'test.automation',
|
|
|
|
'entity_id': 'hello.world'
|
2015-09-15 05:05:40 +00:00
|
|
|
}
|
2015-08-11 06:11:46 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
self.hass.bus.fire('test_event')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-08-11 06:11:46 +00:00
|
|
|
self.assertEqual(1, len(self.calls))
|
2015-09-15 05:05:40 +00:00
|
|
|
self.assertEqual(['hello.world'],
|
|
|
|
self.calls[0].data.get(ATTR_ENTITY_ID))
|
2015-08-25 04:50:20 +00:00
|
|
|
|
2016-10-04 06:41:08 +00:00
|
|
|
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)
|
|
|
|
|
2015-08-25 04:50:20 +00:00
|
|
|
def test_service_specify_entity_id_list(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test service data."""
|
2016-10-01 08:22:13 +00:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2015-08-25 04:50:20 +00:00
|
|
|
automation.DOMAIN: {
|
2015-09-15 05:05:40 +00:00
|
|
|
'trigger': {
|
|
|
|
'platform': 'event',
|
|
|
|
'event_type': 'test_event',
|
|
|
|
},
|
|
|
|
'action': {
|
2015-09-19 15:43:56 +00:00
|
|
|
'service': 'test.automation',
|
|
|
|
'entity_id': ['hello.world', 'hello.world2']
|
2015-09-15 05:05:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
self.hass.bus.fire('test_event')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-09-15 05:05:40 +00:00
|
|
|
self.assertEqual(1, len(self.calls))
|
|
|
|
self.assertEqual(['hello.world', 'hello.world2'],
|
|
|
|
self.calls[0].data.get(ATTR_ENTITY_ID))
|
|
|
|
|
|
|
|
def test_two_triggers(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test triggers."""
|
2016-10-01 08:22:13 +00:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2015-09-15 05:05:40 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': [
|
|
|
|
{
|
|
|
|
'platform': 'event',
|
|
|
|
'event_type': 'test_event',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'platform': 'state',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
}
|
|
|
|
],
|
|
|
|
'action': {
|
2015-09-19 15:43:56 +00:00
|
|
|
'service': 'test.automation',
|
2015-09-15 05:05:40 +00:00
|
|
|
}
|
2015-08-25 04:50:20 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
self.hass.bus.fire('test_event')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-08-25 04:50:20 +00:00
|
|
|
self.assertEqual(1, len(self.calls))
|
2015-09-15 05:05:40 +00:00
|
|
|
self.hass.states.set('test.entity', 'hello')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-09-15 05:05:40 +00:00
|
|
|
self.assertEqual(2, len(self.calls))
|
2015-09-15 05:51:28 +00:00
|
|
|
|
2016-10-04 05:39:27 +00:00
|
|
|
def test_trigger_service_ignoring_condition(self):
|
|
|
|
"""Test triggers."""
|
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': [
|
|
|
|
{
|
|
|
|
'platform': 'event',
|
|
|
|
'event_type': 'test_event',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
'condition': {
|
|
|
|
'condition': 'state',
|
|
|
|
'entity_id': 'non.existing',
|
|
|
|
'state': 'beer',
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
self.hass.bus.fire('test_event')
|
|
|
|
self.hass.block_till_done()
|
|
|
|
assert len(self.calls) == 0
|
|
|
|
|
|
|
|
self.hass.services.call('automation', 'trigger', blocking=True)
|
|
|
|
self.hass.block_till_done()
|
|
|
|
assert len(self.calls) == 1
|
|
|
|
|
2015-09-15 05:51:28 +00:00
|
|
|
def test_two_conditions_with_and(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test two and conditions."""
|
2015-09-15 05:51:28 +00:00
|
|
|
entity_id = 'test.entity'
|
2016-10-01 08:22:13 +00:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2015-09-15 05:51:28 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': [
|
|
|
|
{
|
|
|
|
'platform': 'event',
|
|
|
|
'event_type': 'test_event',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
'condition': [
|
|
|
|
{
|
2016-04-28 10:03:57 +00:00
|
|
|
'condition': 'state',
|
2015-09-15 05:51:28 +00:00
|
|
|
'entity_id': entity_id,
|
2016-04-04 19:18:58 +00:00
|
|
|
'state': '100'
|
2015-09-15 05:51:28 +00:00
|
|
|
},
|
|
|
|
{
|
2016-04-28 10:03:57 +00:00
|
|
|
'condition': 'numeric_state',
|
2015-09-15 05:51:28 +00:00
|
|
|
'entity_id': entity_id,
|
|
|
|
'below': 150
|
|
|
|
}
|
|
|
|
],
|
|
|
|
'action': {
|
2015-09-19 15:43:56 +00:00
|
|
|
'service': 'test.automation',
|
2015-09-15 05:51:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
self.hass.states.set(entity_id, 100)
|
|
|
|
self.hass.bus.fire('test_event')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-09-15 05:51:28 +00:00
|
|
|
self.assertEqual(1, len(self.calls))
|
|
|
|
|
|
|
|
self.hass.states.set(entity_id, 101)
|
|
|
|
self.hass.bus.fire('test_event')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-09-15 05:51:28 +00:00
|
|
|
self.assertEqual(1, len(self.calls))
|
|
|
|
|
|
|
|
self.hass.states.set(entity_id, 151)
|
|
|
|
self.hass.bus.fire('test_event')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-09-15 05:51:28 +00:00
|
|
|
self.assertEqual(1, len(self.calls))
|
|
|
|
|
2015-09-19 19:13:09 +00:00
|
|
|
def test_automation_list_setting(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Event is not a valid condition."""
|
2016-10-01 08:22:13 +00:00
|
|
|
self.assertTrue(setup_component(self.hass, automation.DOMAIN, {
|
2015-09-20 04:02:28 +00:00
|
|
|
automation.DOMAIN: [{
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'event',
|
|
|
|
'event_type': 'test_event',
|
2015-09-19 19:13:09 +00:00
|
|
|
},
|
2015-09-20 04:02:28 +00:00
|
|
|
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation',
|
2015-09-19 19:13:09 +00:00
|
|
|
}
|
2015-09-20 04:02:28 +00:00
|
|
|
}, {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'event',
|
|
|
|
'event_type': 'test_event_2',
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation',
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
}))
|
2015-09-19 19:13:09 +00:00
|
|
|
|
|
|
|
self.hass.bus.fire('test_event')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-09-19 19:13:09 +00:00
|
|
|
self.assertEqual(1, len(self.calls))
|
|
|
|
|
|
|
|
self.hass.bus.fire('test_event_2')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-09-19 19:13:09 +00:00
|
|
|
self.assertEqual(2, len(self.calls))
|
2016-04-22 02:36:14 +00:00
|
|
|
|
|
|
|
def test_automation_calling_two_actions(self):
|
|
|
|
"""Test if we can call two actions from automation definition."""
|
2016-10-01 08:22:13 +00:00
|
|
|
self.assertTrue(setup_component(self.hass, automation.DOMAIN, {
|
2016-04-22 02:36:14 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'event',
|
|
|
|
'event_type': 'test_event',
|
|
|
|
},
|
|
|
|
|
|
|
|
'action': [{
|
|
|
|
'service': 'test.automation',
|
|
|
|
'data': {'position': 0},
|
|
|
|
}, {
|
|
|
|
'service': 'test.automation',
|
|
|
|
'data': {'position': 1},
|
|
|
|
}],
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
|
|
|
self.hass.bus.fire('test_event')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-04-22 02:36:14 +00:00
|
|
|
|
|
|
|
assert len(self.calls) == 2
|
|
|
|
assert self.calls[0].data['position'] == 0
|
|
|
|
assert self.calls[1].data['position'] == 1
|
2016-08-26 06:25:57 +00:00
|
|
|
|
|
|
|
def test_services(self):
|
2016-08-27 06:45:46 +00:00
|
|
|
"""Test the automation services for turning entities on/off."""
|
2016-08-26 06:25:57 +00:00
|
|
|
entity_id = 'automation.hello'
|
|
|
|
|
|
|
|
assert self.hass.states.get(entity_id) is None
|
|
|
|
assert not automation.is_on(self.hass, entity_id)
|
|
|
|
|
2016-10-01 08:22:13 +00:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2016-08-26 06:25:57 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'alias': 'hello',
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'event',
|
|
|
|
'event_type': 'test_event',
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
assert self.hass.states.get(entity_id) is not None
|
|
|
|
assert automation.is_on(self.hass, entity_id)
|
|
|
|
|
|
|
|
self.hass.bus.fire('test_event')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-08-26 06:25:57 +00:00
|
|
|
assert len(self.calls) == 1
|
|
|
|
|
|
|
|
automation.turn_off(self.hass, entity_id)
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-08-26 06:25:57 +00:00
|
|
|
|
|
|
|
assert not automation.is_on(self.hass, entity_id)
|
|
|
|
self.hass.bus.fire('test_event')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-08-26 06:25:57 +00:00
|
|
|
assert len(self.calls) == 1
|
|
|
|
|
|
|
|
automation.toggle(self.hass, entity_id)
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-08-26 06:25:57 +00:00
|
|
|
|
|
|
|
assert automation.is_on(self.hass, entity_id)
|
|
|
|
self.hass.bus.fire('test_event')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-08-26 06:25:57 +00:00
|
|
|
assert len(self.calls) == 2
|
|
|
|
|
|
|
|
automation.trigger(self.hass, entity_id)
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-08-26 06:25:57 +00:00
|
|
|
assert len(self.calls) == 3
|
|
|
|
|
|
|
|
automation.turn_off(self.hass, entity_id)
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-08-26 06:25:57 +00:00
|
|
|
automation.trigger(self.hass, entity_id)
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-08-26 06:25:57 +00:00
|
|
|
assert len(self.calls) == 4
|
|
|
|
|
|
|
|
automation.turn_on(self.hass, entity_id)
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-08-26 06:25:57 +00:00
|
|
|
assert automation.is_on(self.hass, entity_id)
|
2016-09-04 15:15:52 +00:00
|
|
|
|
2016-10-27 07:16:23 +00:00
|
|
|
@patch('homeassistant.config.load_yaml_config_file', autospec=True,
|
|
|
|
return_value={
|
2016-12-02 05:45:19 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'alias': 'bye',
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'event',
|
|
|
|
'event_type': 'test_event2',
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation',
|
|
|
|
'data_template': {
|
|
|
|
'event': '{{ trigger.event.event_type }}'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2016-09-04 15:15:52 +00:00
|
|
|
def test_reload_config_service(self, mock_load_yaml):
|
|
|
|
"""Test the reload config service."""
|
2016-10-01 08:22:13 +00:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2016-09-04 15:15:52 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'alias': 'hello',
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'event',
|
|
|
|
'event_type': 'test_event',
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation',
|
|
|
|
'data_template': {
|
|
|
|
'event': '{{ trigger.event.event_type }}'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
assert self.hass.states.get('automation.hello') is not None
|
|
|
|
assert self.hass.states.get('automation.bye') is None
|
2016-09-07 13:59:16 +00:00
|
|
|
listeners = self.hass.bus.listeners
|
|
|
|
assert listeners.get('test_event') == 1
|
|
|
|
assert listeners.get('test_event2') is None
|
2016-09-04 15:15:52 +00:00
|
|
|
|
|
|
|
self.hass.bus.fire('test_event')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-09-04 15:15:52 +00:00
|
|
|
|
|
|
|
assert len(self.calls) == 1
|
|
|
|
assert self.calls[0].data.get('event') == 'test_event'
|
|
|
|
|
|
|
|
automation.reload(self.hass)
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-10-04 05:39:27 +00:00
|
|
|
# De-flake ?!
|
|
|
|
self.hass.block_till_done()
|
2016-09-04 15:15:52 +00:00
|
|
|
|
|
|
|
assert self.hass.states.get('automation.hello') is None
|
|
|
|
assert self.hass.states.get('automation.bye') is not None
|
2016-09-07 13:59:16 +00:00
|
|
|
listeners = self.hass.bus.listeners
|
|
|
|
assert listeners.get('test_event') is None
|
|
|
|
assert listeners.get('test_event2') == 1
|
2016-09-04 15:15:52 +00:00
|
|
|
|
|
|
|
self.hass.bus.fire('test_event')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-09-04 15:15:52 +00:00
|
|
|
assert len(self.calls) == 1
|
|
|
|
|
|
|
|
self.hass.bus.fire('test_event2')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-09-04 15:15:52 +00:00
|
|
|
assert len(self.calls) == 2
|
|
|
|
assert self.calls[1].data.get('event') == 'test_event2'
|
|
|
|
|
2016-10-27 07:16:23 +00:00
|
|
|
@patch('homeassistant.config.load_yaml_config_file', autospec=True,
|
|
|
|
return_value={automation.DOMAIN: 'not valid'})
|
2016-09-04 15:15:52 +00:00
|
|
|
def test_reload_config_when_invalid_config(self, mock_load_yaml):
|
|
|
|
"""Test the reload config service handling invalid config."""
|
2016-10-08 18:27:35 +00:00
|
|
|
with assert_setup_component(1):
|
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
|
|
|
automation.DOMAIN: {
|
|
|
|
'alias': 'hello',
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'event',
|
|
|
|
'event_type': 'test_event',
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation',
|
|
|
|
'data_template': {
|
|
|
|
'event': '{{ trigger.event.event_type }}'
|
|
|
|
}
|
2016-09-04 15:15:52 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-08 18:27:35 +00:00
|
|
|
})
|
2016-09-04 15:15:52 +00:00
|
|
|
assert self.hass.states.get('automation.hello') is not None
|
|
|
|
|
|
|
|
self.hass.bus.fire('test_event')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-09-04 15:15:52 +00:00
|
|
|
|
|
|
|
assert len(self.calls) == 1
|
|
|
|
assert self.calls[0].data.get('event') == 'test_event'
|
|
|
|
|
|
|
|
automation.reload(self.hass)
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-09-04 15:15:52 +00:00
|
|
|
|
2016-10-08 18:27:35 +00:00
|
|
|
assert self.hass.states.get('automation.hello') is None
|
2016-09-04 15:15:52 +00:00
|
|
|
|
|
|
|
self.hass.bus.fire('test_event')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-10-08 18:27:35 +00:00
|
|
|
assert len(self.calls) == 1
|
2016-09-04 15:15:52 +00:00
|
|
|
|
|
|
|
def test_reload_config_handles_load_fails(self):
|
|
|
|
"""Test the reload config service."""
|
2016-10-01 08:22:13 +00:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2016-09-04 15:15:52 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'alias': 'hello',
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'event',
|
|
|
|
'event_type': 'test_event',
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation',
|
|
|
|
'data_template': {
|
|
|
|
'event': '{{ trigger.event.event_type }}'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
assert self.hass.states.get('automation.hello') is not None
|
|
|
|
|
|
|
|
self.hass.bus.fire('test_event')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-09-04 15:15:52 +00:00
|
|
|
|
|
|
|
assert len(self.calls) == 1
|
|
|
|
assert self.calls[0].data.get('event') == 'test_event'
|
|
|
|
|
|
|
|
with patch('homeassistant.config.load_yaml_config_file',
|
|
|
|
side_effect=HomeAssistantError('bla')):
|
|
|
|
automation.reload(self.hass)
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-09-04 15:15:52 +00:00
|
|
|
|
|
|
|
assert self.hass.states.get('automation.hello') is not None
|
|
|
|
|
|
|
|
self.hass.bus.fire('test_event')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-09-04 15:15:52 +00:00
|
|
|
assert len(self.calls) == 2
|
2017-03-04 23:19:01 +00:00
|
|
|
|
2017-03-24 22:52:14 +00:00
|
|
|
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)
|
|
|
|
|
2017-03-04 23:19:01 +00:00
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_automation_restore_state(hass):
|
|
|
|
"""Ensure states are restored on startup."""
|
|
|
|
time = dt_util.utcnow()
|
|
|
|
|
|
|
|
mock_restore_cache(hass, (
|
|
|
|
State('automation.hello', STATE_ON),
|
|
|
|
State('automation.bye', STATE_OFF, {'last_triggered': time}),
|
|
|
|
))
|
|
|
|
|
|
|
|
config = {automation.DOMAIN: [{
|
|
|
|
'alias': 'hello',
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'event',
|
|
|
|
'event_type': 'test_event_hello',
|
|
|
|
},
|
|
|
|
'action': {'service': 'test.automation'}
|
|
|
|
}, {
|
|
|
|
'alias': 'bye',
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'event',
|
|
|
|
'event_type': 'test_event_bye',
|
|
|
|
},
|
|
|
|
'action': {'service': 'test.automation'}
|
|
|
|
}]}
|
|
|
|
|
|
|
|
assert (yield from async_setup_component(hass, automation.DOMAIN, config))
|
|
|
|
|
|
|
|
state = hass.states.get('automation.hello')
|
|
|
|
assert state
|
|
|
|
assert state.state == STATE_ON
|
|
|
|
|
|
|
|
state = hass.states.get('automation.bye')
|
|
|
|
assert state
|
|
|
|
assert state.state == STATE_OFF
|
|
|
|
assert state.attributes.get('last_triggered') == time
|
|
|
|
|
|
|
|
calls = mock_service(hass, 'test', 'automation')
|
|
|
|
|
|
|
|
assert automation.is_on(hass, 'automation.bye') is False
|
|
|
|
|
|
|
|
hass.bus.async_fire('test_event_bye')
|
|
|
|
yield from hass.async_block_till_done()
|
|
|
|
assert len(calls) == 0
|
|
|
|
|
|
|
|
assert automation.is_on(hass, 'automation.hello')
|
|
|
|
|
|
|
|
hass.bus.async_fire('test_event_hello')
|
|
|
|
yield from hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert len(calls) == 1
|