2015-08-11 06:11:46 +00:00
|
|
|
"""
|
|
|
|
tests.test_component_demo
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Tests demo component.
|
|
|
|
"""
|
|
|
|
import unittest
|
|
|
|
|
2015-08-17 03:44:46 +00:00
|
|
|
import homeassistant.core as ha
|
2015-08-11 06:11:46 +00:00
|
|
|
import homeassistant.components.automation as automation
|
2015-09-15 05:05:40 +00:00
|
|
|
from homeassistant.const import ATTR_ENTITY_ID
|
2015-08-11 06:11:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
2015-09-15 05:05:40 +00:00
|
|
|
def test_service_data_not_a_dict(self):
|
|
|
|
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',
|
|
|
|
'execute_service': 'test.automation',
|
|
|
|
'service_data': 100
|
2015-08-11 06:11:46 +00:00
|
|
|
}
|
2015-09-15 05:05:40 +00:00
|
|
|
})
|
2015-08-11 06:11:46 +00:00
|
|
|
|
2015-09-15 05:05:40 +00:00
|
|
|
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):
|
2015-08-11 06:11:46 +00:00
|
|
|
automation.setup(self.hass, {
|
|
|
|
automation.DOMAIN: {
|
2015-09-15 05:05:40 +00:00
|
|
|
'platform': 'event',
|
|
|
|
'event_type': 'test_event',
|
|
|
|
'execute_service': 'test.automation',
|
|
|
|
'service_data': {'some': 'data'}
|
2015-08-11 06:11:46 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
self.hass.bus.fire('test_event')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertEqual(1, len(self.calls))
|
2015-09-15 05:05:40 +00:00
|
|
|
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))
|
2015-08-11 06:11:46 +00:00
|
|
|
|
|
|
|
def test_service_specify_data(self):
|
|
|
|
automation.setup(self.hass, {
|
|
|
|
automation.DOMAIN: {
|
2015-09-15 05:05:40 +00:00
|
|
|
'trigger': {
|
|
|
|
'platform': 'event',
|
|
|
|
'event_type': 'test_event',
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'execute_service': 'test.automation',
|
|
|
|
'service_data': {'some': 'data'}
|
|
|
|
}
|
2015-08-11 06:11:46 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
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: {
|
2015-09-15 05:05:40 +00:00
|
|
|
'trigger': {
|
|
|
|
'platform': 'event',
|
|
|
|
'event_type': 'test_event',
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'execute_service': 'test.automation',
|
|
|
|
'service_entity_id': 'hello.world'
|
|
|
|
}
|
2015-08-11 06:11:46 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
self.hass.bus.fire('test_event')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertEqual(1, len(self.calls))
|
2015-09-15 05:05:40 +00:00
|
|
|
self.assertEqual(['hello.world'],
|
|
|
|
self.calls[0].data.get(ATTR_ENTITY_ID))
|
2015-08-25 04:50:20 +00:00
|
|
|
|
|
|
|
def test_service_specify_entity_id_list(self):
|
|
|
|
automation.setup(self.hass, {
|
|
|
|
automation.DOMAIN: {
|
2015-09-15 05:05:40 +00:00
|
|
|
'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',
|
|
|
|
'service_entity_id': ['hello.world', 'hello.world2']
|
|
|
|
}
|
2015-08-25 04:50:20 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
self.hass.bus.fire('test_event')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertEqual(1, len(self.calls))
|
2015-09-15 05:05:40 +00:00
|
|
|
self.hass.states.set('test.entity', 'hello')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertEqual(2, len(self.calls))
|