core/tests/components/automation/test_init.py

319 lines
10 KiB
Python
Raw Normal View History

2016-03-09 09:25:50 +00:00
"""The tests for the automation component."""
2015-08-11 06:11:46 +00:00
import unittest
from homeassistant.bootstrap import _setup_component
2015-08-11 06:11:46 +00:00
import homeassistant.components.automation as automation
2015-09-15 05:05:40 +00:00
from homeassistant.const import ATTR_ENTITY_ID
2015-08-11 06:11:46 +00:00
2016-02-14 23:08:23 +00:00
from tests.common import get_test_home_assistant
2015-08-11 06:11:46 +00:00
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
def setUp(self): # pylint: disable=invalid-name
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()
self.hass.config.components.append('group')
2015-08-11 06:11:46 +00:00
self.calls = []
def record_call(service):
self.calls.append(service)
self.hass.services.register('test', 'automation', record_call)
def tearDown(self): # pylint: disable=invalid-name
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."""
assert not _setup_component(self.hass, automation.DOMAIN, {
2015-09-19 15:43:56 +00:00
automation.DOMAIN: {
'trigger': {
'platform': 'event',
'event_type': 'test_event',
},
'action': {
'service': 'test.automation',
'data': 100,
}
}
})
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."""
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',
'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
}
})
self.hass.bus.fire('test_event')
self.hass.pool.block_till_done()
self.assertEqual(1, len(self.calls))
self.assertEqual('event - test_event', self.calls[0].data['some'])
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."""
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')
self.hass.pool.block_till_done()
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))
def test_service_specify_entity_id_list(self):
2016-03-09 09:25:50 +00:00
"""Test service data."""
assert _setup_component(self.hass, automation.DOMAIN, {
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')
self.hass.pool.block_till_done()
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."""
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
}
}
})
self.hass.bus.fire('test_event')
self.hass.pool.block_till_done()
self.assertEqual(1, len(self.calls))
2015-09-15 05:05:40 +00:00
self.hass.states.set('test.entity', 'hello')
self.hass.pool.block_till_done()
self.assertEqual(2, len(self.calls))
def test_two_conditions_with_and(self):
2016-03-09 09:25:50 +00:00
"""Test two and conditions."""
entity_id = 'test.entity'
assert _setup_component(self.hass, automation.DOMAIN, {
automation.DOMAIN: {
'trigger': [
{
'platform': 'event',
'event_type': 'test_event',
},
],
'condition': [
{
'platform': 'state',
'entity_id': entity_id,
'state': '100'
},
{
'platform': 'numeric_state',
'entity_id': entity_id,
'below': 150
}
],
'action': {
2015-09-19 15:43:56 +00:00
'service': 'test.automation',
}
}
})
self.hass.states.set(entity_id, 100)
self.hass.bus.fire('test_event')
self.hass.pool.block_till_done()
self.assertEqual(1, len(self.calls))
self.hass.states.set(entity_id, 101)
self.hass.bus.fire('test_event')
self.hass.pool.block_till_done()
self.assertEqual(1, len(self.calls))
self.hass.states.set(entity_id, 151)
self.hass.bus.fire('test_event')
self.hass.pool.block_till_done()
self.assertEqual(1, len(self.calls))
def test_two_conditions_with_or(self):
2016-03-09 09:25:50 +00:00
"""Test two or conditions."""
entity_id = 'test.entity'
assert _setup_component(self.hass, automation.DOMAIN, {
automation.DOMAIN: {
'trigger': [
{
'platform': 'event',
'event_type': 'test_event',
},
],
'condition_type': 'OR',
'condition': [
{
'platform': 'state',
'entity_id': entity_id,
'state': '200'
},
{
'platform': 'numeric_state',
'entity_id': entity_id,
'below': 150
}
],
'action': {
2015-09-19 15:43:56 +00:00
'service': 'test.automation',
}
}
})
self.hass.states.set(entity_id, 200)
self.hass.bus.fire('test_event')
self.hass.pool.block_till_done()
self.assertEqual(1, len(self.calls))
self.hass.states.set(entity_id, 100)
self.hass.bus.fire('test_event')
self.hass.pool.block_till_done()
self.assertEqual(2, len(self.calls))
self.hass.states.set(entity_id, 250)
self.hass.bus.fire('test_event')
self.hass.pool.block_till_done()
self.assertEqual(2, len(self.calls))
2015-09-15 15:56:06 +00:00
def test_using_trigger_as_condition(self):
2016-03-09 09:25:50 +00:00
"""Test triggers as condition."""
2015-09-15 15:56:06 +00:00
entity_id = 'test.entity'
assert _setup_component(self.hass, automation.DOMAIN, {
2015-09-15 15:56:06 +00:00
automation.DOMAIN: {
'trigger': [
{
'platform': 'state',
'entity_id': entity_id,
'state': '100'
2015-09-15 15:56:06 +00:00
},
{
'platform': 'numeric_state',
'entity_id': entity_id,
'below': 150
}
],
'condition': 'use_trigger_values',
'action': {
2015-09-19 15:43:56 +00:00
'service': 'test.automation',
2015-09-15 15:56:06 +00:00
}
}
})
self.hass.states.set(entity_id, 100)
self.hass.pool.block_till_done()
self.assertEqual(2, len(self.calls))
2015-09-15 15:56:06 +00:00
self.hass.states.set(entity_id, 120)
self.hass.pool.block_till_done()
self.assertEqual(2, len(self.calls))
2015-09-15 15:56:06 +00:00
self.hass.states.set(entity_id, 151)
self.hass.pool.block_till_done()
self.assertEqual(2, len(self.calls))
2015-09-15 15:56:06 +00:00
def test_using_trigger_as_condition_with_invalid_condition(self):
2016-03-09 09:25:50 +00:00
"""Event is not a valid condition."""
2015-09-15 15:56:06 +00:00
entity_id = 'test.entity'
self.hass.states.set(entity_id, 100)
assert _setup_component(self.hass, automation.DOMAIN, {
2015-09-15 15:56:06 +00:00
automation.DOMAIN: {
'trigger': [
{
'platform': 'event',
'event_type': 'test_event',
},
{
'platform': 'numeric_state',
'entity_id': entity_id,
'below': 150
}
],
'condition': 'use_trigger_values',
'action': {
2015-09-19 15:43:56 +00:00
'service': 'test.automation',
2015-09-15 15:56:06 +00:00
}
}
})
self.hass.bus.fire('test_event')
self.hass.pool.block_till_done()
self.assertEqual(1, len(self.calls))
def test_automation_list_setting(self):
2016-03-09 09:25:50 +00:00
"""Event is not a valid condition."""
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-20 04:02:28 +00:00
'action': {
'service': 'test.automation',
}
2015-09-20 04:02:28 +00:00
}, {
'trigger': {
'platform': 'event',
'event_type': 'test_event_2',
},
'action': {
'service': 'test.automation',
}
}]
}))
self.hass.bus.fire('test_event')
self.hass.pool.block_till_done()
self.assertEqual(1, len(self.calls))
self.hass.bus.fire('test_event_2')
self.hass.pool.block_till_done()
self.assertEqual(2, len(self.calls))