hass-core/tests/components/automation/test_event.py

85 lines
2.8 KiB
Python
Raw Normal View History

2016-03-09 10:25:50 +01:00
"""The tests for the Event automation."""
2015-08-10 22:21:34 -07:00
import unittest
from homeassistant.bootstrap import _setup_component
2015-08-10 22:21:34 -07:00
import homeassistant.components.automation as automation
2016-02-14 15:08:23 -08:00
from tests.common import get_test_home_assistant
2015-08-10 22:21:34 -07:00
class TestAutomationEvent(unittest.TestCase):
2016-03-09 10:25:50 +01:00
"""Test the event automation."""
2015-08-10 22:21:34 -07:00
def setUp(self): # pylint: disable=invalid-name
2016-03-09 10:25:50 +01:00
"""Setup things to be run when tests are started."""
2016-02-14 15:08:23 -08:00
self.hass = get_test_home_assistant()
self.hass.config.components.append('group')
2015-08-10 22:21:34 -07:00
self.calls = []
def record_call(service):
2016-03-09 10:25:50 +01:00
"""Helper for recording the call."""
2015-08-10 22:21:34 -07:00
self.calls.append(service)
self.hass.services.register('test', 'automation', record_call)
def tearDown(self): # pylint: disable=invalid-name
2016-03-09 10:25:50 +01:00
""""Stop everything that was started."""
2015-08-10 22:21:34 -07:00
self.hass.stop()
def test_if_fires_on_event(self):
2016-03-09 10:25:50 +01:00
"""Test the firing of events."""
assert _setup_component(self.hass, automation.DOMAIN, {
2015-08-10 22:21:34 -07:00
automation.DOMAIN: {
2015-09-14 22:05:40 -07:00
'trigger': {
'platform': 'event',
'event_type': 'test_event',
},
'action': {
2015-09-19 08:43:56 -07:00
'service': 'test.automation',
2015-09-14 22:05:40 -07:00
}
2015-08-10 22:21:34 -07:00
}
})
2015-08-10 22:21:34 -07:00
self.hass.bus.fire('test_event')
self.hass.pool.block_till_done()
self.assertEqual(1, len(self.calls))
def test_if_fires_on_event_with_data(self):
2016-03-09 10:25:50 +01:00
"""Test the firing of events with data."""
assert _setup_component(self.hass, automation.DOMAIN, {
2015-08-10 22:21:34 -07:00
automation.DOMAIN: {
2015-09-14 22:05:40 -07:00
'trigger': {
'platform': 'event',
'event_type': 'test_event',
'event_data': {'some_attr': 'some_value'}
},
'action': {
2015-09-19 08:43:56 -07:00
'service': 'test.automation',
2015-09-14 22:05:40 -07:00
}
2015-08-10 22:21:34 -07:00
}
})
2015-08-10 22:21:34 -07:00
2015-09-19 08:27:34 -07:00
self.hass.bus.fire('test_event', {'some_attr': 'some_value',
'another': 'value'})
2015-08-10 22:21:34 -07: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 10:25:50 +01:00
"""Test firing of event if no match."""
assert _setup_component(self.hass, automation.DOMAIN, {
2015-08-10 22:21:34 -07:00
automation.DOMAIN: {
2015-09-14 22:05:40 -07:00
'trigger': {
'platform': 'event',
'event_type': 'test_event',
'event_data': {'some_attr': 'some_value'}
},
'action': {
2015-09-19 08:43:56 -07:00
'service': 'test.automation',
2015-09-14 22:05:40 -07:00
}
2015-08-10 22:21:34 -07:00
}
})
2015-08-10 22:21:34 -07:00
self.hass.bus.fire('test_event', {'some_attr': 'some_other_value'})
self.hass.pool.block_till_done()
self.assertEqual(0, len(self.calls))