262 lines
8.3 KiB
Python
262 lines
8.3 KiB
Python
"""
|
|
tests.test_component_demo
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
Tests demo component.
|
|
"""
|
|
import unittest
|
|
|
|
import homeassistant.core as ha
|
|
import homeassistant.components.automation as automation
|
|
from homeassistant.const import ATTR_ENTITY_ID
|
|
|
|
|
|
class TestAutomationEvent(unittest.TestCase):
|
|
""" Test the event automation. """
|
|
|
|
def setUp(self): # pylint: disable=invalid-name
|
|
self.hass = ha.HomeAssistant()
|
|
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_service_data_not_a_dict(self):
|
|
automation.setup(self.hass, {
|
|
automation.DOMAIN: {
|
|
'platform': 'event',
|
|
'event_type': 'test_event',
|
|
'execute_service': 'test.automation',
|
|
'service_data': 100
|
|
}
|
|
})
|
|
|
|
self.hass.bus.fire('test_event')
|
|
self.hass.pool.block_till_done()
|
|
self.assertEqual(1, len(self.calls))
|
|
|
|
def test_old_config_service_specify_data(self):
|
|
automation.setup(self.hass, {
|
|
automation.DOMAIN: {
|
|
'platform': 'event',
|
|
'event_type': 'test_event',
|
|
'execute_service': 'test.automation',
|
|
'service_data': {'some': 'data'}
|
|
}
|
|
})
|
|
|
|
self.hass.bus.fire('test_event')
|
|
self.hass.pool.block_till_done()
|
|
self.assertEqual(1, len(self.calls))
|
|
self.assertEqual('data', self.calls[0].data['some'])
|
|
|
|
def test_old_config_service_specify_entity_id(self):
|
|
automation.setup(self.hass, {
|
|
automation.DOMAIN: {
|
|
'platform': 'event',
|
|
'event_type': 'test_event',
|
|
'execute_service': 'test.automation',
|
|
'service_entity_id': 'hello.world'
|
|
}
|
|
})
|
|
|
|
self.hass.bus.fire('test_event')
|
|
self.hass.pool.block_till_done()
|
|
self.assertEqual(1, len(self.calls))
|
|
self.assertEqual(['hello.world'],
|
|
self.calls[0].data.get(ATTR_ENTITY_ID))
|
|
|
|
def test_old_config_service_specify_entity_id_list(self):
|
|
automation.setup(self.hass, {
|
|
automation.DOMAIN: {
|
|
'platform': 'event',
|
|
'event_type': 'test_event',
|
|
'execute_service': 'test.automation',
|
|
'service_entity_id': ['hello.world', 'hello.world2']
|
|
}
|
|
})
|
|
|
|
self.hass.bus.fire('test_event')
|
|
self.hass.pool.block_till_done()
|
|
self.assertEqual(1, len(self.calls))
|
|
self.assertEqual(['hello.world', 'hello.world2'],
|
|
self.calls[0].data.get(ATTR_ENTITY_ID))
|
|
|
|
def test_service_specify_data(self):
|
|
automation.setup(self.hass, {
|
|
automation.DOMAIN: {
|
|
'trigger': {
|
|
'platform': 'event',
|
|
'event_type': 'test_event',
|
|
},
|
|
'action': {
|
|
'execute_service': 'test.automation',
|
|
'service_data': {'some': 'data'}
|
|
}
|
|
}
|
|
})
|
|
|
|
self.hass.bus.fire('test_event')
|
|
self.hass.pool.block_till_done()
|
|
self.assertEqual(1, len(self.calls))
|
|
self.assertEqual('data', self.calls[0].data['some'])
|
|
|
|
def test_service_specify_entity_id(self):
|
|
automation.setup(self.hass, {
|
|
automation.DOMAIN: {
|
|
'trigger': {
|
|
'platform': 'event',
|
|
'event_type': 'test_event',
|
|
},
|
|
'action': {
|
|
'execute_service': 'test.automation',
|
|
'service_entity_id': 'hello.world'
|
|
}
|
|
}
|
|
})
|
|
|
|
self.hass.bus.fire('test_event')
|
|
self.hass.pool.block_till_done()
|
|
self.assertEqual(1, len(self.calls))
|
|
self.assertEqual(['hello.world'],
|
|
self.calls[0].data.get(ATTR_ENTITY_ID))
|
|
|
|
def test_service_specify_entity_id_list(self):
|
|
automation.setup(self.hass, {
|
|
automation.DOMAIN: {
|
|
'trigger': {
|
|
'platform': 'event',
|
|
'event_type': 'test_event',
|
|
},
|
|
'action': {
|
|
'execute_service': 'test.automation',
|
|
'service_entity_id': ['hello.world', 'hello.world2']
|
|
}
|
|
}
|
|
})
|
|
|
|
self.hass.bus.fire('test_event')
|
|
self.hass.pool.block_till_done()
|
|
self.assertEqual(1, len(self.calls))
|
|
self.assertEqual(['hello.world', 'hello.world2'],
|
|
self.calls[0].data.get(ATTR_ENTITY_ID))
|
|
|
|
def test_two_triggers(self):
|
|
automation.setup(self.hass, {
|
|
automation.DOMAIN: {
|
|
'trigger': [
|
|
{
|
|
'platform': 'event',
|
|
'event_type': 'test_event',
|
|
},
|
|
{
|
|
'platform': 'state',
|
|
'entity_id': 'test.entity',
|
|
}
|
|
],
|
|
'action': {
|
|
'execute_service': 'test.automation',
|
|
}
|
|
}
|
|
})
|
|
|
|
self.hass.bus.fire('test_event')
|
|
self.hass.pool.block_till_done()
|
|
self.assertEqual(1, len(self.calls))
|
|
self.hass.states.set('test.entity', 'hello')
|
|
self.hass.pool.block_till_done()
|
|
self.assertEqual(2, len(self.calls))
|
|
|
|
def test_two_conditions_with_and(self):
|
|
entity_id = 'test.entity'
|
|
automation.setup(self.hass, {
|
|
automation.DOMAIN: {
|
|
'trigger': [
|
|
{
|
|
'platform': 'event',
|
|
'event_type': 'test_event',
|
|
},
|
|
],
|
|
'condition': [
|
|
{
|
|
'platform': 'state',
|
|
'entity_id': entity_id,
|
|
'state': 100
|
|
},
|
|
{
|
|
'platform': 'numeric_state',
|
|
'entity_id': entity_id,
|
|
'below': 150
|
|
}
|
|
],
|
|
'action': {
|
|
'execute_service': 'test.automation',
|
|
}
|
|
}
|
|
})
|
|
|
|
self.hass.states.set(entity_id, 100)
|
|
self.hass.bus.fire('test_event')
|
|
self.hass.pool.block_till_done()
|
|
self.assertEqual(1, len(self.calls))
|
|
|
|
self.hass.states.set(entity_id, 101)
|
|
self.hass.bus.fire('test_event')
|
|
self.hass.pool.block_till_done()
|
|
self.assertEqual(1, len(self.calls))
|
|
|
|
self.hass.states.set(entity_id, 151)
|
|
self.hass.bus.fire('test_event')
|
|
self.hass.pool.block_till_done()
|
|
self.assertEqual(1, len(self.calls))
|
|
|
|
def test_two_conditions_with_or(self):
|
|
entity_id = 'test.entity'
|
|
automation.setup(self.hass, {
|
|
automation.DOMAIN: {
|
|
'trigger': [
|
|
{
|
|
'platform': 'event',
|
|
'event_type': 'test_event',
|
|
},
|
|
],
|
|
'condition_type': 'OR',
|
|
'condition': [
|
|
{
|
|
'platform': 'state',
|
|
'entity_id': entity_id,
|
|
'state': 200
|
|
},
|
|
{
|
|
'platform': 'numeric_state',
|
|
'entity_id': entity_id,
|
|
'below': 150
|
|
}
|
|
],
|
|
'action': {
|
|
'execute_service': 'test.automation',
|
|
}
|
|
}
|
|
})
|
|
|
|
self.hass.states.set(entity_id, 200)
|
|
self.hass.bus.fire('test_event')
|
|
self.hass.pool.block_till_done()
|
|
self.assertEqual(1, len(self.calls))
|
|
|
|
self.hass.states.set(entity_id, 100)
|
|
self.hass.bus.fire('test_event')
|
|
self.hass.pool.block_till_done()
|
|
self.assertEqual(2, len(self.calls))
|
|
|
|
self.hass.states.set(entity_id, 250)
|
|
self.hass.bus.fire('test_event')
|
|
self.hass.pool.block_till_done()
|
|
self.assertEqual(2, len(self.calls))
|