core/tests/components/automation/test_event.py

124 lines
3.9 KiB
Python
Raw Normal View History

2015-08-11 05:21:34 +00:00
"""
2015-09-15 07:02:46 +00:00
tests.components.automation.test_event
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2015-08-11 05:21:34 +00:00
2015-09-15 07:02:46 +00:00
Tests event automation.
2015-08-11 05:21:34 +00:00
"""
import unittest
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):
""" Test the event automation. """
def setUp(self): # pylint: disable=invalid-name
2016-02-14 23:08:23 +00:00
self.hass = get_test_home_assistant()
2015-08-11 05:21:34 +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
""" Stop down stuff we started. """
self.hass.stop()
2015-09-15 05:05:40 +00:00
def test_old_config_if_fires_on_event(self):
self.assertTrue(automation.setup(self.hass, {
automation.DOMAIN: {
'platform': 'event',
'event_type': 'test_event',
'execute_service': 'test.automation'
}
}))
self.hass.bus.fire('test_event')
self.hass.pool.block_till_done()
self.assertEqual(1, len(self.calls))
def test_old_config_if_fires_on_event_with_data(self):
self.assertTrue(automation.setup(self.hass, {
2015-08-11 06:11:46 +00:00
automation.DOMAIN: {
2015-09-15 05:05:40 +00:00
'platform': 'event',
'event_type': 'test_event',
'event_data': {'some_attr': 'some_value'},
'execute_service': 'test.automation'
2015-08-11 06:11:46 +00:00
}
}))
2015-09-15 05:05:40 +00:00
self.hass.bus.fire('test_event', {'some_attr': 'some_value'})
self.hass.pool.block_till_done()
self.assertEqual(1, len(self.calls))
def test_old_config_if_not_fires_if_event_data_not_matches(self):
self.assertTrue(automation.setup(self.hass, {
automation.DOMAIN: {
'platform': 'event',
'event_type': 'test_event',
'event_data': {'some_attr': 'some_value'},
'execute_service': 'test.automation'
}
}))
self.hass.bus.fire('test_event', {'some_attr': 'some_other_value'})
self.hass.pool.block_till_done()
self.assertEqual(0, len(self.calls))
2015-08-11 05:21:34 +00:00
def test_if_fires_on_event(self):
2015-08-11 06:11:46 +00:00
self.assertTrue(automation.setup(self.hass, {
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 06:11:46 +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))
def test_if_fires_on_event_with_data(self):
2015-08-11 06:11:46 +00:00
self.assertTrue(automation.setup(self.hass, {
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 06:11:46 +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):
2015-08-11 06:11:46 +00:00
self.assertTrue(automation.setup(self.hass, {
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 06:11:46 +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))