mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Add tests for automation component
This commit is contained in:
parent
6e98e55f6a
commit
eecc51c92d
@ -56,8 +56,7 @@ def _get_action(hass, config):
|
|||||||
service_data = config.get(CONF_SERVICE_DATA, {})
|
service_data = config.get(CONF_SERVICE_DATA, {})
|
||||||
|
|
||||||
if not isinstance(service_data, dict):
|
if not isinstance(service_data, dict):
|
||||||
_LOGGER.error(
|
_LOGGER.error("%s should be a dictionary", CONF_SERVICE_DATA)
|
||||||
"%s should be a serialized JSON object", CONF_SERVICE_DATA)
|
|
||||||
service_data = {}
|
service_data = {}
|
||||||
|
|
||||||
if CONF_SERVICE_ENTITY_ID in config:
|
if CONF_SERVICE_ENTITY_ID in config:
|
||||||
|
0
tests/components/automation/__init__.py
Normal file
0
tests/components/automation/__init__.py
Normal file
72
tests/components/automation/test_event.py
Normal file
72
tests/components/automation/test_event.py
Normal file
@ -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))
|
133
tests/components/automation/test_state.py
Normal file
133
tests/components/automation/test_state.py
Normal file
@ -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))
|
97
tests/components/automation/test_time.py
Normal file
97
tests/components/automation/test_time.py
Normal file
@ -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))
|
Loading…
x
Reference in New Issue
Block a user