mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 04:50:05 +00:00
Refactor automation configuration
This commit is contained in:
@@ -8,8 +8,7 @@ import unittest
|
||||
|
||||
import homeassistant.core as ha
|
||||
import homeassistant.components.automation as automation
|
||||
import homeassistant.components.automation.event as event
|
||||
from homeassistant.const import CONF_PLATFORM, ATTR_ENTITY_ID
|
||||
from homeassistant.const import ATTR_ENTITY_ID
|
||||
|
||||
|
||||
class TestAutomationEvent(unittest.TestCase):
|
||||
@@ -28,20 +27,13 @@ class TestAutomationEvent(unittest.TestCase):
|
||||
""" Stop down stuff we started. """
|
||||
self.hass.stop()
|
||||
|
||||
def test_setup_fails_if_unknown_platform(self):
|
||||
self.assertFalse(automation.setup(self.hass, {
|
||||
automation.DOMAIN: {
|
||||
CONF_PLATFORM: 'i_do_not_exist'
|
||||
}
|
||||
}))
|
||||
|
||||
def test_service_data_not_a_dict(self):
|
||||
automation.setup(self.hass, {
|
||||
automation.DOMAIN: {
|
||||
CONF_PLATFORM: 'event',
|
||||
event.CONF_EVENT_TYPE: 'test_event',
|
||||
automation.CONF_SERVICE: 'test.automation',
|
||||
automation.CONF_SERVICE_DATA: 100
|
||||
'platform': 'event',
|
||||
'event_type': 'test_event',
|
||||
'execute_service': 'test.automation',
|
||||
'service_data': 100
|
||||
}
|
||||
})
|
||||
|
||||
@@ -49,13 +41,64 @@ class TestAutomationEvent(unittest.TestCase):
|
||||
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: {
|
||||
CONF_PLATFORM: 'event',
|
||||
event.CONF_EVENT_TYPE: 'test_event',
|
||||
automation.CONF_SERVICE: 'test.automation',
|
||||
automation.CONF_SERVICE_DATA: {'some': 'data'}
|
||||
'trigger': {
|
||||
'platform': 'event',
|
||||
'event_type': 'test_event',
|
||||
},
|
||||
'action': {
|
||||
'execute_service': 'test.automation',
|
||||
'service_data': {'some': 'data'}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
@@ -67,29 +110,66 @@ class TestAutomationEvent(unittest.TestCase):
|
||||
def test_service_specify_entity_id(self):
|
||||
automation.setup(self.hass, {
|
||||
automation.DOMAIN: {
|
||||
CONF_PLATFORM: 'event',
|
||||
event.CONF_EVENT_TYPE: 'test_event',
|
||||
automation.CONF_SERVICE: 'test.automation',
|
||||
automation.CONF_SERVICE_ENTITY_ID: 'hello.world'
|
||||
'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[ATTR_ENTITY_ID])
|
||||
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: {
|
||||
CONF_PLATFORM: 'event',
|
||||
event.CONF_EVENT_TYPE: 'test_event',
|
||||
automation.CONF_SERVICE: 'test.automation',
|
||||
automation.CONF_SERVICE_ENTITY_ID: ['hello.world', 'hello.world2']
|
||||
'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[ATTR_ENTITY_ID])
|
||||
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']
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
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))
|
||||
|
||||
Reference in New Issue
Block a user