From eecc51c92d480ff105b027c79818a99487aa4d1a Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Mon, 10 Aug 2015 22:21:34 -0700 Subject: [PATCH] Add tests for automation component --- .../components/automation/__init__.py | 3 +- tests/components/automation/__init__.py | 0 tests/components/automation/test_event.py | 72 ++++++++++ tests/components/automation/test_state.py | 133 ++++++++++++++++++ tests/components/automation/test_time.py | 97 +++++++++++++ 5 files changed, 303 insertions(+), 2 deletions(-) create mode 100644 tests/components/automation/__init__.py create mode 100644 tests/components/automation/test_event.py create mode 100644 tests/components/automation/test_state.py create mode 100644 tests/components/automation/test_time.py diff --git a/homeassistant/components/automation/__init__.py b/homeassistant/components/automation/__init__.py index c7fa1c12d4b..23d1c6f02d3 100644 --- a/homeassistant/components/automation/__init__.py +++ b/homeassistant/components/automation/__init__.py @@ -56,8 +56,7 @@ def _get_action(hass, config): service_data = config.get(CONF_SERVICE_DATA, {}) if not isinstance(service_data, dict): - _LOGGER.error( - "%s should be a serialized JSON object", CONF_SERVICE_DATA) + _LOGGER.error("%s should be a dictionary", CONF_SERVICE_DATA) service_data = {} if CONF_SERVICE_ENTITY_ID in config: diff --git a/tests/components/automation/__init__.py b/tests/components/automation/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/components/automation/test_event.py b/tests/components/automation/test_event.py new file mode 100644 index 00000000000..9c8acda86fd --- /dev/null +++ b/tests/components/automation/test_event.py @@ -0,0 +1,72 @@ +""" +tests.test_component_demo +~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Tests demo component. +""" +import unittest + +import homeassistant as ha +import homeassistant.loader as loader +import homeassistant.components.automation as automation +import homeassistant.components.automation.event as event +from homeassistant.const import CONF_PLATFORM + + +class TestAutomationEvent(unittest.TestCase): + """ Test the event automation. """ + + def setUp(self): # pylint: disable=invalid-name + self.hass = ha.HomeAssistant() + loader.prepare(self.hass) + 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() + + def test_if_fires_on_event(self): + automation.setup(self.hass, { + automation.DOMAIN: { + CONF_PLATFORM: 'event', + event.CONF_EVENT_TYPE: 'test_event', + automation.CONF_SERVICE: 'test.automation' + } + }) + + 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): + automation.setup(self.hass, { + automation.DOMAIN: { + CONF_PLATFORM: 'event', + event.CONF_EVENT_TYPE: 'test_event', + event.CONF_EVENT_DATA: {'some_attr': 'some_value'}, + automation.CONF_SERVICE: 'test.automation' + } + }) + + self.hass.bus.fire('test_event', {'some_attr': 'some_value'}) + self.hass.pool.block_till_done() + self.assertEqual(1, len(self.calls)) + + def test_if_not_fires_if_event_data_not_matches(self): + automation.setup(self.hass, { + automation.DOMAIN: { + CONF_PLATFORM: 'event', + event.CONF_EVENT_TYPE: 'test_event', + event.CONF_EVENT_DATA: {'some_attr': 'some_value'}, + automation.CONF_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)) diff --git a/tests/components/automation/test_state.py b/tests/components/automation/test_state.py new file mode 100644 index 00000000000..a9c74555b79 --- /dev/null +++ b/tests/components/automation/test_state.py @@ -0,0 +1,133 @@ +""" +tests.test_component_demo +~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Tests demo component. +""" +import unittest + +import homeassistant as ha +import homeassistant.loader as loader +import homeassistant.components.automation as automation +import homeassistant.components.automation.state as state +from homeassistant.const import CONF_PLATFORM + + +class TestAutomationState(unittest.TestCase): + """ Test the event automation. """ + + def setUp(self): # pylint: disable=invalid-name + self.hass = ha.HomeAssistant() + loader.prepare(self.hass) + self.hass.states.set('test.entity', 'hello') + 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() + + def test_if_fires_on_entity_change(self): + automation.setup(self.hass, { + automation.DOMAIN: { + CONF_PLATFORM: 'state', + state.CONF_ENTITY_ID: 'test.entity', + automation.CONF_SERVICE: 'test.automation' + } + }) + + self.hass.states.set('test.entity', 'world') + self.hass.pool.block_till_done() + self.assertEqual(1, len(self.calls)) + + def test_if_fires_on_entity_change_with_from_filter(self): + automation.setup(self.hass, { + automation.DOMAIN: { + CONF_PLATFORM: 'state', + state.CONF_ENTITY_ID: 'test.entity', + state.CONF_FROM: 'hello', + automation.CONF_SERVICE: 'test.automation' + } + }) + + self.hass.states.set('test.entity', 'world') + self.hass.pool.block_till_done() + self.assertEqual(1, len(self.calls)) + + def test_if_fires_on_entity_change_with_to_filter(self): + automation.setup(self.hass, { + automation.DOMAIN: { + CONF_PLATFORM: 'state', + state.CONF_ENTITY_ID: 'test.entity', + state.CONF_TO: 'world', + automation.CONF_SERVICE: 'test.automation' + } + }) + + self.hass.states.set('test.entity', 'world') + self.hass.pool.block_till_done() + self.assertEqual(1, len(self.calls)) + + def test_if_fires_on_entity_change_with_both_filters(self): + automation.setup(self.hass, { + automation.DOMAIN: { + CONF_PLATFORM: 'state', + state.CONF_ENTITY_ID: 'test.entity', + state.CONF_FROM: 'hello', + state.CONF_TO: 'world', + automation.CONF_SERVICE: 'test.automation' + } + }) + + self.hass.states.set('test.entity', 'world') + self.hass.pool.block_till_done() + self.assertEqual(1, len(self.calls)) + + def test_if_not_fires_if_to_filter_not_match(self): + automation.setup(self.hass, { + automation.DOMAIN: { + CONF_PLATFORM: 'state', + state.CONF_ENTITY_ID: 'test.entity', + state.CONF_FROM: 'hello', + state.CONF_TO: 'world', + automation.CONF_SERVICE: 'test.automation' + } + }) + + self.hass.states.set('test.entity', 'moon') + self.hass.pool.block_till_done() + self.assertEqual(0, len(self.calls)) + + def test_if_not_fires_if_from_filter_not_match(self): + self.hass.states.set('test.entity', 'bye') + + automation.setup(self.hass, { + automation.DOMAIN: { + CONF_PLATFORM: 'state', + state.CONF_ENTITY_ID: 'test.entity', + state.CONF_FROM: 'hello', + state.CONF_TO: 'world', + automation.CONF_SERVICE: 'test.automation' + } + }) + + self.hass.states.set('test.entity', 'world') + self.hass.pool.block_till_done() + self.assertEqual(0, len(self.calls)) + + def test_if_not_fires_if_entity_not_match(self): + automation.setup(self.hass, { + automation.DOMAIN: { + CONF_PLATFORM: 'state', + state.CONF_ENTITY_ID: 'test.another_entity', + automation.CONF_SERVICE: 'test.automation' + } + }) + + self.hass.states.set('test.entity', 'world') + self.hass.pool.block_till_done() + self.assertEqual(0, len(self.calls)) diff --git a/tests/components/automation/test_time.py b/tests/components/automation/test_time.py new file mode 100644 index 00000000000..6dac2708f11 --- /dev/null +++ b/tests/components/automation/test_time.py @@ -0,0 +1,97 @@ +""" +tests.test_component_demo +~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Tests demo component. +""" +import unittest + +import homeassistant as ha +import homeassistant.loader as loader +import homeassistant.util.dt as dt_util +import homeassistant.components.automation as automation +import homeassistant.components.automation.time as time +from homeassistant.const import CONF_PLATFORM + +from tests.common import fire_time_changed + + +class TestAutomationTime(unittest.TestCase): + """ Test the event automation. """ + + def setUp(self): # pylint: disable=invalid-name + self.hass = ha.HomeAssistant() + loader.prepare(self.hass) + 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() + + def test_if_fires_when_hour_matches(self): + automation.setup(self.hass, { + automation.DOMAIN: { + CONF_PLATFORM: 'time', + time.CONF_HOURS: 0, + automation.CONF_SERVICE: 'test.automation' + } + }) + + fire_time_changed(self.hass, dt_util.utcnow().replace(hour=0)) + + self.hass.states.set('test.entity', 'world') + self.hass.pool.block_till_done() + self.assertEqual(1, len(self.calls)) + + def test_if_fires_when_minute_matches(self): + automation.setup(self.hass, { + automation.DOMAIN: { + CONF_PLATFORM: 'time', + time.CONF_MINUTES: 0, + automation.CONF_SERVICE: 'test.automation' + } + }) + + fire_time_changed(self.hass, dt_util.utcnow().replace(minute=0)) + + self.hass.states.set('test.entity', 'world') + self.hass.pool.block_till_done() + self.assertEqual(1, len(self.calls)) + + def test_if_fires_when_second_matches(self): + automation.setup(self.hass, { + automation.DOMAIN: { + CONF_PLATFORM: 'time', + time.CONF_SECONDS: 0, + automation.CONF_SERVICE: 'test.automation' + } + }) + + fire_time_changed(self.hass, dt_util.utcnow().replace(second=0)) + + self.hass.states.set('test.entity', 'world') + self.hass.pool.block_till_done() + self.assertEqual(1, len(self.calls)) + + def test_if_fires_when_all_matches(self): + automation.setup(self.hass, { + automation.DOMAIN: { + CONF_PLATFORM: 'time', + time.CONF_HOURS: 0, + time.CONF_MINUTES: 0, + time.CONF_SECONDS: 0, + automation.CONF_SERVICE: 'test.automation' + } + }) + + fire_time_changed(self.hass, dt_util.utcnow().replace( + hour=0, minute=0, second=0)) + + self.hass.states.set('test.entity', 'world') + self.hass.pool.block_till_done() + self.assertEqual(1, len(self.calls))