2015-08-11 05:21:34 +00:00
|
|
|
"""
|
2015-09-15 07:02:46 +00:00
|
|
|
tests.components.automation.test_state
|
2016-02-13 13:19:11 +00:00
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2015-08-11 05:21:34 +00:00
|
|
|
|
2015-09-15 07:02:46 +00:00
|
|
|
Tests state automation.
|
2015-08-11 05:21:34 +00:00
|
|
|
"""
|
|
|
|
import unittest
|
|
|
|
|
2015-08-17 03:44:46 +00:00
|
|
|
import homeassistant.core as ha
|
2015-08-11 05:21:34 +00:00
|
|
|
import homeassistant.components.automation as automation
|
2015-10-12 01:30:25 +00:00
|
|
|
import homeassistant.components.automation.state as state
|
2015-08-11 05:21:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestAutomationState(unittest.TestCase):
|
|
|
|
""" Test the event automation. """
|
|
|
|
|
|
|
|
def setUp(self): # pylint: disable=invalid-name
|
|
|
|
self.hass = ha.HomeAssistant()
|
|
|
|
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()
|
|
|
|
|
2015-09-15 05:05:40 +00:00
|
|
|
def test_old_config_if_fires_on_entity_change(self):
|
2015-08-11 06:11:46 +00:00
|
|
|
self.assertTrue(automation.setup(self.hass, {
|
2015-08-11 05:21:34 +00:00
|
|
|
automation.DOMAIN: {
|
2015-09-15 01:22:49 +00:00
|
|
|
'platform': 'state',
|
|
|
|
'state_entity_id': 'test.entity',
|
|
|
|
'execute_service': 'test.automation'
|
2015-08-11 05:21:34 +00:00
|
|
|
}
|
2015-08-11 06:11:46 +00:00
|
|
|
}))
|
2015-08-11 05:21:34 +00:00
|
|
|
|
|
|
|
self.hass.states.set('test.entity', 'world')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertEqual(1, len(self.calls))
|
|
|
|
|
2015-09-15 05:05:40 +00:00
|
|
|
def test_old_config_if_fires_on_entity_change_with_from_filter(self):
|
2015-08-11 06:11:46 +00:00
|
|
|
self.assertTrue(automation.setup(self.hass, {
|
2015-08-11 05:21:34 +00:00
|
|
|
automation.DOMAIN: {
|
2015-09-15 01:22:49 +00:00
|
|
|
'platform': 'state',
|
|
|
|
'state_entity_id': 'test.entity',
|
|
|
|
'state_from': 'hello',
|
|
|
|
'execute_service': 'test.automation'
|
2015-08-11 05:21:34 +00:00
|
|
|
}
|
2015-08-11 06:11:46 +00:00
|
|
|
}))
|
2015-08-11 05:21:34 +00:00
|
|
|
|
|
|
|
self.hass.states.set('test.entity', 'world')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertEqual(1, len(self.calls))
|
|
|
|
|
2015-09-15 05:05:40 +00:00
|
|
|
def test_old_config_if_fires_on_entity_change_with_to_filter(self):
|
2015-08-11 06:11:46 +00:00
|
|
|
self.assertTrue(automation.setup(self.hass, {
|
2015-08-11 05:21:34 +00:00
|
|
|
automation.DOMAIN: {
|
2015-09-15 01:22:49 +00:00
|
|
|
'platform': 'state',
|
|
|
|
'state_entity_id': 'test.entity',
|
|
|
|
'state_to': 'world',
|
|
|
|
'execute_service': 'test.automation'
|
2015-08-11 05:21:34 +00:00
|
|
|
}
|
2015-08-11 06:11:46 +00:00
|
|
|
}))
|
2015-08-11 05:21:34 +00:00
|
|
|
|
|
|
|
self.hass.states.set('test.entity', 'world')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertEqual(1, len(self.calls))
|
|
|
|
|
2015-09-15 05:05:40 +00:00
|
|
|
def test_old_config_if_fires_on_entity_change_with_both_filters(self):
|
2015-08-11 06:11:46 +00:00
|
|
|
self.assertTrue(automation.setup(self.hass, {
|
2015-08-11 05:21:34 +00:00
|
|
|
automation.DOMAIN: {
|
2015-09-15 01:22:49 +00:00
|
|
|
'platform': 'state',
|
|
|
|
'state_entity_id': 'test.entity',
|
|
|
|
'state_from': 'hello',
|
|
|
|
'state_to': 'world',
|
|
|
|
'execute_service': 'test.automation'
|
2015-08-11 05:21:34 +00:00
|
|
|
}
|
2015-08-11 06:11:46 +00:00
|
|
|
}))
|
2015-08-11 05:21:34 +00:00
|
|
|
|
|
|
|
self.hass.states.set('test.entity', 'world')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertEqual(1, len(self.calls))
|
|
|
|
|
2015-09-15 05:05:40 +00:00
|
|
|
def test_old_config_if_not_fires_if_to_filter_not_match(self):
|
2015-08-11 06:11:46 +00:00
|
|
|
self.assertTrue(automation.setup(self.hass, {
|
2015-08-11 05:21:34 +00:00
|
|
|
automation.DOMAIN: {
|
2015-09-15 01:22:49 +00:00
|
|
|
'platform': 'state',
|
|
|
|
'state_entity_id': 'test.entity',
|
|
|
|
'state_from': 'hello',
|
|
|
|
'state_to': 'world',
|
|
|
|
'execute_service': 'test.automation'
|
2015-08-11 05:21:34 +00:00
|
|
|
}
|
2015-08-11 06:11:46 +00:00
|
|
|
}))
|
2015-08-11 05:21:34 +00:00
|
|
|
|
|
|
|
self.hass.states.set('test.entity', 'moon')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertEqual(0, len(self.calls))
|
|
|
|
|
2015-09-15 05:05:40 +00:00
|
|
|
def test_old_config_if_not_fires_if_from_filter_not_match(self):
|
2015-08-11 05:21:34 +00:00
|
|
|
self.hass.states.set('test.entity', 'bye')
|
|
|
|
|
2015-08-11 06:11:46 +00:00
|
|
|
self.assertTrue(automation.setup(self.hass, {
|
2015-08-11 05:21:34 +00:00
|
|
|
automation.DOMAIN: {
|
2015-09-15 01:22:49 +00:00
|
|
|
'platform': 'state',
|
|
|
|
'state_entity_id': 'test.entity',
|
|
|
|
'state_from': 'hello',
|
|
|
|
'state_to': 'world',
|
|
|
|
'execute_service': 'test.automation'
|
2015-08-11 05:21:34 +00:00
|
|
|
}
|
2015-08-11 06:11:46 +00:00
|
|
|
}))
|
2015-08-11 05:21:34 +00:00
|
|
|
|
|
|
|
self.hass.states.set('test.entity', 'world')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertEqual(0, len(self.calls))
|
|
|
|
|
2015-09-15 05:05:40 +00:00
|
|
|
def test_old_config_if_not_fires_if_entity_not_match(self):
|
2015-08-11 06:11:46 +00:00
|
|
|
self.assertTrue(automation.setup(self.hass, {
|
2015-08-11 05:21:34 +00:00
|
|
|
automation.DOMAIN: {
|
2015-09-15 01:22:49 +00:00
|
|
|
'platform': 'state',
|
|
|
|
'state_entity_id': 'test.another_entity',
|
|
|
|
'execute_service': 'test.automation'
|
2015-08-11 05:21:34 +00:00
|
|
|
}
|
2015-08-11 06:11:46 +00:00
|
|
|
}))
|
2015-08-11 05:21:34 +00:00
|
|
|
|
|
|
|
self.hass.states.set('test.entity', 'world')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertEqual(0, len(self.calls))
|
2015-09-14 05:25:42 +00:00
|
|
|
|
2015-09-15 05:05:40 +00:00
|
|
|
def test_old_config_if_action(self):
|
2015-09-14 05:25:42 +00:00
|
|
|
entity_id = 'domain.test_entity'
|
|
|
|
test_state = 'new_state'
|
|
|
|
automation.setup(self.hass, {
|
|
|
|
automation.DOMAIN: {
|
2015-09-15 01:22:49 +00:00
|
|
|
'platform': 'event',
|
|
|
|
'event_type': 'test_event',
|
|
|
|
'execute_service': 'test.automation',
|
|
|
|
'if': [{
|
|
|
|
'platform': 'state',
|
|
|
|
'entity_id': entity_id,
|
|
|
|
'state': test_state,
|
2015-09-14 05:25:42 +00:00
|
|
|
}]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
self.hass.states.set(entity_id, test_state)
|
|
|
|
self.hass.bus.fire('test_event')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
|
|
|
|
self.assertEqual(1, len(self.calls))
|
|
|
|
|
|
|
|
self.hass.states.set(entity_id, test_state + 'something')
|
|
|
|
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
|
|
|
|
|
|
|
def test_if_fires_on_entity_change(self):
|
|
|
|
self.assertTrue(automation.setup(self.hass, {
|
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'state',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
},
|
|
|
|
'action': {
|
2015-09-19 15:43:56 +00:00
|
|
|
'service': 'test.automation'
|
2015-09-15 05:05:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
|
|
|
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):
|
|
|
|
self.assertTrue(automation.setup(self.hass, {
|
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'state',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
'from': 'hello'
|
|
|
|
},
|
|
|
|
'action': {
|
2015-09-19 15:43:56 +00:00
|
|
|
'service': 'test.automation'
|
2015-09-15 05:05:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
|
|
|
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):
|
|
|
|
self.assertTrue(automation.setup(self.hass, {
|
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'state',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
'to': 'world'
|
|
|
|
},
|
|
|
|
'action': {
|
2015-09-19 15:43:56 +00:00
|
|
|
'service': 'test.automation'
|
2015-09-15 05:05:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
|
|
|
self.hass.states.set('test.entity', 'world')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertEqual(1, len(self.calls))
|
|
|
|
|
2015-09-15 15:56:06 +00:00
|
|
|
def test_if_fires_on_entity_change_with_state_filter(self):
|
|
|
|
self.assertTrue(automation.setup(self.hass, {
|
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'state',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
'state': 'world'
|
|
|
|
},
|
|
|
|
'action': {
|
2015-09-19 15:43:56 +00:00
|
|
|
'service': 'test.automation'
|
2015-09-15 15:56:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
|
|
|
self.hass.states.set('test.entity', 'world')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertEqual(1, len(self.calls))
|
|
|
|
|
2015-09-15 05:05:40 +00:00
|
|
|
def test_if_fires_on_entity_change_with_both_filters(self):
|
|
|
|
self.assertTrue(automation.setup(self.hass, {
|
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'state',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
'from': 'hello',
|
|
|
|
'to': 'world'
|
|
|
|
},
|
|
|
|
'action': {
|
2015-09-19 15:43:56 +00:00
|
|
|
'service': 'test.automation'
|
2015-09-15 05:05:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
|
|
|
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):
|
|
|
|
self.assertTrue(automation.setup(self.hass, {
|
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'state',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
'from': 'hello',
|
|
|
|
'to': 'world'
|
|
|
|
},
|
|
|
|
'action': {
|
2015-09-19 15:43:56 +00:00
|
|
|
'service': 'test.automation'
|
2015-09-15 05:05:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
|
|
|
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')
|
|
|
|
|
|
|
|
self.assertTrue(automation.setup(self.hass, {
|
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'state',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
'from': 'hello',
|
|
|
|
'to': 'world'
|
|
|
|
},
|
|
|
|
'action': {
|
2015-09-19 15:43:56 +00:00
|
|
|
'service': 'test.automation'
|
2015-09-15 05:05:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
|
|
|
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):
|
|
|
|
self.assertTrue(automation.setup(self.hass, {
|
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'state',
|
|
|
|
'entity_id': 'test.anoter_entity',
|
|
|
|
},
|
|
|
|
'action': {
|
2015-09-19 15:43:56 +00:00
|
|
|
'service': 'test.automation'
|
2015-09-15 05:05:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
|
|
|
self.hass.states.set('test.entity', 'world')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertEqual(0, len(self.calls))
|
|
|
|
|
|
|
|
def test_if_action(self):
|
|
|
|
entity_id = 'domain.test_entity'
|
|
|
|
test_state = 'new_state'
|
|
|
|
automation.setup(self.hass, {
|
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'event',
|
|
|
|
'event_type': 'test_event',
|
|
|
|
},
|
|
|
|
'condition': [{
|
|
|
|
'platform': 'state',
|
|
|
|
'entity_id': entity_id,
|
|
|
|
'state': test_state
|
|
|
|
}],
|
|
|
|
'action': {
|
2015-09-19 15:43:56 +00:00
|
|
|
'service': 'test.automation'
|
2015-09-15 05:05:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
self.hass.states.set(entity_id, test_state)
|
|
|
|
self.hass.bus.fire('test_event')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
|
|
|
|
self.assertEqual(1, len(self.calls))
|
|
|
|
|
|
|
|
self.hass.states.set(entity_id, test_state + 'something')
|
|
|
|
self.hass.bus.fire('test_event')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
|
|
|
|
self.assertEqual(1, len(self.calls))
|
2015-10-12 01:30:25 +00:00
|
|
|
|
|
|
|
def test_if_fails_setup_if_to_boolean_value(self):
|
|
|
|
self.assertFalse(state.trigger(
|
|
|
|
self.hass, {
|
|
|
|
'platform': 'state',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
'to': True,
|
|
|
|
}, lambda x: x))
|
|
|
|
|
|
|
|
def test_if_fails_setup_if_from_boolean_value(self):
|
|
|
|
self.assertFalse(state.trigger(
|
|
|
|
self.hass, {
|
|
|
|
'platform': 'state',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
'from': True,
|
|
|
|
}, lambda x: x))
|