core/tests/components/automation/test_event.py

92 lines
3.0 KiB
Python
Raw Normal View History

2016-03-09 09:25:50 +00:00
"""The tests for the Event automation."""
2015-08-11 05:21:34 +00:00
import unittest
from homeassistant.bootstrap import _setup_component
2015-08-11 05:21:34 +00:00
import homeassistant.components.automation as automation
2016-02-14 23:08:23 +00:00
from tests.common import get_test_home_assistant
2015-08-11 05:21:34 +00:00
class TestAutomationEvent(unittest.TestCase):
2016-03-09 09:25:50 +00:00
"""Test the event automation."""
2015-08-11 05:21:34 +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 05:21:34 +00:00
self.calls = []
def record_call(service):
2016-03-09 09:25:50 +00:00
"""Helper for recording the call."""
2015-08-11 05:21:34 +00:00
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 05:21:34 +00:00
self.hass.stop()
def test_if_fires_on_event(self):
2016-03-09 09:25:50 +00:00
"""Test the firing of events."""
assert _setup_component(self.hass, automation.DOMAIN, {
2015-08-11 05:21:34 +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',
2015-09-15 05:05:40 +00:00
}
2015-08-11 05:21:34 +00:00
}
})
2015-08-11 05:21:34 +00:00
self.hass.bus.fire('test_event')
self.hass.pool.block_till_done()
self.assertEqual(1, len(self.calls))
automation.turn_off(self.hass)
self.hass.pool.block_till_done()
self.hass.bus.fire('test_event')
self.hass.pool.block_till_done()
self.assertEqual(1, len(self.calls))
2015-08-11 05:21:34 +00:00
def test_if_fires_on_event_with_data(self):
2016-03-09 09:25:50 +00:00
"""Test the firing of events with data."""
assert _setup_component(self.hass, automation.DOMAIN, {
2015-08-11 05:21:34 +00:00
automation.DOMAIN: {
2015-09-15 05:05:40 +00:00
'trigger': {
'platform': 'event',
'event_type': 'test_event',
'event_data': {'some_attr': 'some_value'}
},
'action': {
2015-09-19 15:43:56 +00:00
'service': 'test.automation',
2015-09-15 05:05:40 +00:00
}
2015-08-11 05:21:34 +00:00
}
})
2015-08-11 05:21:34 +00:00
2015-09-19 15:27:34 +00:00
self.hass.bus.fire('test_event', {'some_attr': 'some_value',
'another': 'value'})
2015-08-11 05:21:34 +00:00
self.hass.pool.block_till_done()
self.assertEqual(1, len(self.calls))
def test_if_not_fires_if_event_data_not_matches(self):
2016-03-09 09:25:50 +00:00
"""Test firing of event if no match."""
assert _setup_component(self.hass, automation.DOMAIN, {
2015-08-11 05:21:34 +00:00
automation.DOMAIN: {
2015-09-15 05:05:40 +00:00
'trigger': {
'platform': 'event',
'event_type': 'test_event',
'event_data': {'some_attr': 'some_value'}
},
'action': {
2015-09-19 15:43:56 +00:00
'service': 'test.automation',
2015-09-15 05:05:40 +00:00
}
2015-08-11 05:21:34 +00:00
}
})
2015-08-11 05:21:34 +00:00
self.hass.bus.fire('test_event', {'some_attr': 'some_other_value'})
self.hass.pool.block_till_done()
self.assertEqual(0, len(self.calls))