2016-03-09 09:25:50 +00:00
|
|
|
"""The test for state automation."""
|
2016-02-19 14:49:11 +00:00
|
|
|
from datetime import timedelta
|
2016-12-02 05:45:19 +00:00
|
|
|
|
|
|
|
import unittest
|
2016-02-20 21:15:49 +00:00
|
|
|
from unittest.mock import patch
|
2016-02-20 21:25:41 +00:00
|
|
|
|
2016-11-05 23:36:20 +00:00
|
|
|
from homeassistant.core import callback
|
2017-03-05 09:41:54 +00:00
|
|
|
from homeassistant.setup import setup_component
|
2016-02-19 14:49:11 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
2015-08-11 05:21:34 +00:00
|
|
|
import homeassistant.components.automation as automation
|
|
|
|
|
2016-10-27 07:16:23 +00:00
|
|
|
from tests.common import (
|
2017-03-01 04:33:19 +00:00
|
|
|
fire_time_changed, get_test_home_assistant, assert_setup_component,
|
|
|
|
mock_component)
|
2016-02-14 23:08:23 +00:00
|
|
|
|
2015-08-11 05:21:34 +00:00
|
|
|
|
2016-12-02 05:45:19 +00:00
|
|
|
# pylint: disable=invalid-name
|
2015-08-11 05:21:34 +00:00
|
|
|
class TestAutomationState(unittest.TestCase):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the event automation."""
|
2015-08-11 05:21:34 +00:00
|
|
|
|
2016-12-02 05:45:19 +00:00
|
|
|
def setUp(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Setup things to be run when tests are started."""
|
2016-02-14 23:08:23 +00:00
|
|
|
self.hass = get_test_home_assistant()
|
2017-03-01 04:33:19 +00:00
|
|
|
mock_component(self.hass, 'group')
|
2015-08-11 05:21:34 +00:00
|
|
|
self.hass.states.set('test.entity', 'hello')
|
|
|
|
self.calls = []
|
|
|
|
|
2016-11-05 23:36:20 +00:00
|
|
|
@callback
|
2015-08-11 05:21:34 +00:00
|
|
|
def record_call(service):
|
2016-12-02 05:45:19 +00:00
|
|
|
"""Call recorder."""
|
2015-08-11 05:21:34 +00:00
|
|
|
self.calls.append(service)
|
|
|
|
|
|
|
|
self.hass.services.register('test', 'automation', record_call)
|
|
|
|
|
2016-12-02 05:45:19 +00:00
|
|
|
def tearDown(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Stop everything that was started."""
|
2015-08-11 05:21:34 +00:00
|
|
|
self.hass.stop()
|
|
|
|
|
2015-09-15 05:05:40 +00:00
|
|
|
def test_if_fires_on_entity_change(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test for firing on entity change."""
|
2016-04-21 20:59:42 +00:00
|
|
|
self.hass.states.set('test.entity', 'hello')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-04-21 20:59:42 +00:00
|
|
|
|
2016-10-27 07:16:23 +00:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2015-09-15 05:05:40 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'state',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
},
|
|
|
|
'action': {
|
2016-04-21 20:59:42 +00:00
|
|
|
'service': 'test.automation',
|
|
|
|
'data_template': {
|
|
|
|
'some': '{{ trigger.%s }}' % '}} - {{ trigger.'.join((
|
2016-12-02 05:45:19 +00:00
|
|
|
'platform', 'entity_id',
|
|
|
|
'from_state.state', 'to_state.state',
|
|
|
|
'for'))
|
2016-04-21 20:59:42 +00:00
|
|
|
},
|
2015-09-15 05:05:40 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-04 19:18:58 +00:00
|
|
|
})
|
2015-09-15 05:05:40 +00:00
|
|
|
|
|
|
|
self.hass.states.set('test.entity', 'world')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-09-15 05:05:40 +00:00
|
|
|
self.assertEqual(1, len(self.calls))
|
2016-04-21 20:59:42 +00:00
|
|
|
self.assertEqual(
|
|
|
|
'state - test.entity - hello - world - None',
|
|
|
|
self.calls[0].data['some'])
|
2015-09-15 05:05:40 +00:00
|
|
|
|
2016-08-26 06:25:57 +00:00
|
|
|
automation.turn_off(self.hass)
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-08-26 06:25:57 +00:00
|
|
|
self.hass.states.set('test.entity', 'planet')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-08-26 06:25:57 +00:00
|
|
|
self.assertEqual(1, len(self.calls))
|
|
|
|
|
2015-09-15 05:05:40 +00:00
|
|
|
def test_if_fires_on_entity_change_with_from_filter(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test for firing on entity change with filter."""
|
2016-10-27 07:16:23 +00:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2015-09-15 05:05:40 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2016-04-04 19:18:58 +00:00
|
|
|
})
|
2015-09-15 05:05:40 +00:00
|
|
|
|
|
|
|
self.hass.states.set('test.entity', 'world')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-09-15 05:05:40 +00:00
|
|
|
self.assertEqual(1, len(self.calls))
|
|
|
|
|
|
|
|
def test_if_fires_on_entity_change_with_to_filter(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test for firing on entity change with no filter."""
|
2016-10-27 07:16:23 +00:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2015-09-15 05:05:40 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2016-04-04 19:18:58 +00:00
|
|
|
})
|
2015-09-15 05:05:40 +00:00
|
|
|
|
|
|
|
self.hass.states.set('test.entity', 'world')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-09-15 05:05:40 +00:00
|
|
|
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):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test for firing on entity change with state filter."""
|
2016-10-27 07:16:23 +00:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2015-09-15 15:56:06 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2016-04-04 19:18:58 +00:00
|
|
|
})
|
2015-09-15 15:56:06 +00:00
|
|
|
|
|
|
|
self.hass.states.set('test.entity', 'world')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-09-15 15:56:06 +00:00
|
|
|
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):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test for firing if both filters are a non match."""
|
2016-10-27 07:16:23 +00:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2015-09-15 05:05:40 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2016-04-04 19:18:58 +00:00
|
|
|
})
|
2015-09-15 05:05:40 +00:00
|
|
|
|
|
|
|
self.hass.states.set('test.entity', 'world')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-09-15 05:05:40 +00:00
|
|
|
self.assertEqual(1, len(self.calls))
|
|
|
|
|
|
|
|
def test_if_not_fires_if_to_filter_not_match(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test for not firing if to filter is not a match."""
|
2016-10-27 07:16:23 +00:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2015-09-15 05:05:40 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2016-04-04 19:18:58 +00:00
|
|
|
})
|
2015-09-15 05:05:40 +00:00
|
|
|
|
|
|
|
self.hass.states.set('test.entity', 'moon')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-09-15 05:05:40 +00:00
|
|
|
self.assertEqual(0, len(self.calls))
|
|
|
|
|
|
|
|
def test_if_not_fires_if_from_filter_not_match(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test for not firing if from filter is not a match."""
|
2015-09-15 05:05:40 +00:00
|
|
|
self.hass.states.set('test.entity', 'bye')
|
|
|
|
|
2016-10-27 07:16:23 +00:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2015-09-15 05:05:40 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2016-04-04 19:18:58 +00:00
|
|
|
})
|
2015-09-15 05:05:40 +00:00
|
|
|
|
|
|
|
self.hass.states.set('test.entity', 'world')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-09-15 05:05:40 +00:00
|
|
|
self.assertEqual(0, len(self.calls))
|
|
|
|
|
|
|
|
def test_if_not_fires_if_entity_not_match(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test for not firing if entity is not matching."""
|
2016-10-27 07:16:23 +00:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2015-09-15 05:05:40 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2016-04-04 19:18:58 +00:00
|
|
|
})
|
2015-09-15 05:05:40 +00:00
|
|
|
|
|
|
|
self.hass.states.set('test.entity', 'world')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-09-15 05:05:40 +00:00
|
|
|
self.assertEqual(0, len(self.calls))
|
|
|
|
|
|
|
|
def test_if_action(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test for to action."""
|
2015-09-15 05:05:40 +00:00
|
|
|
entity_id = 'domain.test_entity'
|
|
|
|
test_state = 'new_state'
|
2016-10-27 07:16:23 +00:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2015-09-15 05:05:40 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'event',
|
|
|
|
'event_type': 'test_event',
|
|
|
|
},
|
|
|
|
'condition': [{
|
2016-10-01 08:22:13 +00:00
|
|
|
'condition': 'state',
|
2015-09-15 05:05:40 +00:00
|
|
|
'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')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-09-15 05:05:40 +00:00
|
|
|
|
|
|
|
self.assertEqual(1, len(self.calls))
|
|
|
|
|
|
|
|
self.hass.states.set(entity_id, test_state + 'something')
|
|
|
|
self.hass.bus.fire('test_event')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-09-15 05:05:40 +00:00
|
|
|
|
|
|
|
self.assertEqual(1, len(self.calls))
|
2015-10-12 01:30:25 +00:00
|
|
|
|
|
|
|
def test_if_fails_setup_if_to_boolean_value(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test for setup failure for boolean to."""
|
2016-10-27 07:16:23 +00:00
|
|
|
with assert_setup_component(0):
|
2017-05-18 04:57:50 +00:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2016-10-27 07:16:23 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'state',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
'to': True,
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'homeassistant.turn_on',
|
|
|
|
}
|
|
|
|
}})
|
2015-10-12 01:30:25 +00:00
|
|
|
|
|
|
|
def test_if_fails_setup_if_from_boolean_value(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test for setup failure for boolean from."""
|
2016-10-27 07:16:23 +00:00
|
|
|
with assert_setup_component(0):
|
2017-05-18 04:57:50 +00:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2016-10-27 07:16:23 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'state',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
'from': True,
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'homeassistant.turn_on',
|
|
|
|
}
|
|
|
|
}})
|
2016-02-19 14:49:11 +00:00
|
|
|
|
|
|
|
def test_if_fails_setup_bad_for(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test for setup failure for bad for."""
|
2016-10-27 07:16:23 +00:00
|
|
|
with assert_setup_component(0):
|
2017-05-18 04:57:50 +00:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2016-10-27 07:16:23 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'state',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
'to': 'world',
|
|
|
|
'for': {
|
|
|
|
'invalid': 5
|
|
|
|
},
|
2016-04-04 19:18:58 +00:00
|
|
|
},
|
2016-10-27 07:16:23 +00:00
|
|
|
'action': {
|
|
|
|
'service': 'homeassistant.turn_on',
|
|
|
|
}
|
|
|
|
}})
|
2016-02-19 14:49:11 +00:00
|
|
|
|
|
|
|
def test_if_fails_setup_for_without_to(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test for setup failures for missing to."""
|
2016-10-27 07:16:23 +00:00
|
|
|
with assert_setup_component(0):
|
2017-05-18 04:57:50 +00:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2016-10-27 07:16:23 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'state',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
'for': {
|
|
|
|
'seconds': 5
|
|
|
|
},
|
2016-04-04 19:18:58 +00:00
|
|
|
},
|
2016-10-27 07:16:23 +00:00
|
|
|
'action': {
|
|
|
|
'service': 'homeassistant.turn_on',
|
|
|
|
}
|
|
|
|
}})
|
2016-02-19 14:49:11 +00:00
|
|
|
|
|
|
|
def test_if_not_fires_on_entity_change_with_for(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test for not firing on entity change with for."""
|
2016-10-27 07:16:23 +00:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2016-02-19 14:49:11 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'state',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
'to': 'world',
|
|
|
|
'for': {
|
|
|
|
'seconds': 5
|
|
|
|
},
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation'
|
|
|
|
}
|
|
|
|
}
|
2016-04-04 19:18:58 +00:00
|
|
|
})
|
2016-02-19 14:49:11 +00:00
|
|
|
|
|
|
|
self.hass.states.set('test.entity', 'world')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-02-19 14:49:11 +00:00
|
|
|
self.hass.states.set('test.entity', 'not_world')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-02-19 14:49:11 +00:00
|
|
|
fire_time_changed(self.hass, dt_util.utcnow() + timedelta(seconds=10))
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-02-19 14:49:11 +00:00
|
|
|
self.assertEqual(0, len(self.calls))
|
|
|
|
|
2017-05-15 07:34:30 +00:00
|
|
|
def test_if_fires_on_entity_change_with_for_attribute_change(self):
|
|
|
|
"""Test for firing on entity change with for and attribute change."""
|
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'state',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
'to': 'world',
|
|
|
|
'for': {
|
|
|
|
'seconds': 5
|
|
|
|
},
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
utcnow = dt_util.utcnow()
|
|
|
|
with patch('homeassistant.core.dt_util.utcnow') as mock_utcnow:
|
|
|
|
mock_utcnow.return_value = utcnow
|
|
|
|
self.hass.states.set('test.entity', 'world')
|
|
|
|
self.hass.block_till_done()
|
|
|
|
mock_utcnow.return_value += timedelta(seconds=4)
|
|
|
|
fire_time_changed(self.hass, mock_utcnow.return_value)
|
|
|
|
self.hass.states.set('test.entity', 'world',
|
|
|
|
attributes={"mock_attr": "attr_change"})
|
|
|
|
self.hass.block_till_done()
|
|
|
|
self.assertEqual(0, len(self.calls))
|
|
|
|
mock_utcnow.return_value += timedelta(seconds=4)
|
|
|
|
fire_time_changed(self.hass, mock_utcnow.return_value)
|
|
|
|
self.hass.block_till_done()
|
|
|
|
self.assertEqual(1, len(self.calls))
|
|
|
|
|
2016-02-19 14:49:11 +00:00
|
|
|
def test_if_fires_on_entity_change_with_for(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test for firing on entity change with for."""
|
2016-10-27 07:16:23 +00:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2016-02-19 14:49:11 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'state',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
'to': 'world',
|
|
|
|
'for': {
|
|
|
|
'seconds': 5
|
|
|
|
},
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation'
|
|
|
|
}
|
|
|
|
}
|
2016-04-04 19:18:58 +00:00
|
|
|
})
|
2016-02-19 14:49:11 +00:00
|
|
|
|
|
|
|
self.hass.states.set('test.entity', 'world')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-02-19 14:49:11 +00:00
|
|
|
fire_time_changed(self.hass, dt_util.utcnow() + timedelta(seconds=10))
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-02-19 14:49:11 +00:00
|
|
|
self.assertEqual(1, len(self.calls))
|
2016-02-20 11:48:05 +00:00
|
|
|
|
|
|
|
def test_if_fires_on_for_condition(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test for firing if contition is on."""
|
2016-02-20 21:15:49 +00:00
|
|
|
point1 = dt_util.utcnow()
|
|
|
|
point2 = point1 + timedelta(seconds=10)
|
|
|
|
with patch('homeassistant.core.dt_util.utcnow') as mock_utcnow:
|
|
|
|
mock_utcnow.return_value = point1
|
|
|
|
self.hass.states.set('test.entity', 'on')
|
2016-10-27 07:16:23 +00:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2016-02-20 21:15:49 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'event',
|
|
|
|
'event_type': 'test_event',
|
|
|
|
},
|
|
|
|
'condition': {
|
2016-10-01 08:22:13 +00:00
|
|
|
'condition': 'state',
|
2016-02-20 21:15:49 +00:00
|
|
|
'entity_id': 'test.entity',
|
|
|
|
'state': 'on',
|
|
|
|
'for': {
|
|
|
|
'seconds': 5
|
|
|
|
},
|
2016-02-20 11:48:05 +00:00
|
|
|
},
|
2016-04-04 19:18:58 +00:00
|
|
|
'action': {'service': 'test.automation'},
|
2016-02-20 11:48:05 +00:00
|
|
|
}
|
2016-04-04 19:18:58 +00:00
|
|
|
})
|
2016-02-20 21:15:49 +00:00
|
|
|
|
|
|
|
# not enough time has passed
|
|
|
|
self.hass.bus.fire('test_event')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-02-20 21:15:49 +00:00
|
|
|
self.assertEqual(0, len(self.calls))
|
|
|
|
|
|
|
|
# Time travel 10 secs into the future
|
|
|
|
mock_utcnow.return_value = point2
|
|
|
|
self.hass.bus.fire('test_event')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-02-20 21:15:49 +00:00
|
|
|
self.assertEqual(1, len(self.calls))
|
2016-02-22 08:40:27 +00:00
|
|
|
|
2017-05-15 07:34:30 +00:00
|
|
|
def test_if_fires_on_for_condition_attribute_change(self):
|
|
|
|
"""Test for firing if contition is on with attribute change."""
|
|
|
|
point1 = dt_util.utcnow()
|
|
|
|
point2 = point1 + timedelta(seconds=4)
|
|
|
|
point3 = point1 + timedelta(seconds=8)
|
|
|
|
with patch('homeassistant.core.dt_util.utcnow') as mock_utcnow:
|
|
|
|
mock_utcnow.return_value = point1
|
|
|
|
self.hass.states.set('test.entity', 'on')
|
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'event',
|
|
|
|
'event_type': 'test_event',
|
|
|
|
},
|
|
|
|
'condition': {
|
|
|
|
'condition': 'state',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
'state': 'on',
|
|
|
|
'for': {
|
|
|
|
'seconds': 5
|
|
|
|
},
|
|
|
|
},
|
|
|
|
'action': {'service': 'test.automation'},
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
# not enough time has passed
|
|
|
|
self.hass.bus.fire('test_event')
|
|
|
|
self.hass.block_till_done()
|
|
|
|
self.assertEqual(0, len(self.calls))
|
|
|
|
|
|
|
|
# Still not enough time has passed, but an attribute is changed
|
|
|
|
mock_utcnow.return_value = point2
|
|
|
|
self.hass.states.set('test.entity', 'on',
|
|
|
|
attributes={"mock_attr": "attr_change"})
|
|
|
|
self.hass.bus.fire('test_event')
|
|
|
|
self.hass.block_till_done()
|
|
|
|
self.assertEqual(0, len(self.calls))
|
|
|
|
|
|
|
|
# Enough time has now passed
|
|
|
|
mock_utcnow.return_value = point3
|
|
|
|
self.hass.bus.fire('test_event')
|
|
|
|
self.hass.block_till_done()
|
|
|
|
self.assertEqual(1, len(self.calls))
|
|
|
|
|
2016-02-22 08:40:27 +00:00
|
|
|
def test_if_fails_setup_for_without_time(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test for setup failure if no time is provided."""
|
2016-10-27 07:16:23 +00:00
|
|
|
with assert_setup_component(0):
|
2017-05-18 04:57:50 +00:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2016-10-27 07:16:23 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'event',
|
|
|
|
'event_type': 'bla'
|
|
|
|
},
|
|
|
|
'condition': {
|
|
|
|
'platform': 'state',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
'state': 'on',
|
|
|
|
'for': {},
|
|
|
|
},
|
|
|
|
'action': {'service': 'test.automation'},
|
|
|
|
}})
|
2016-02-22 08:40:27 +00:00
|
|
|
|
|
|
|
def test_if_fails_setup_for_without_entity(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test for setup failure if no entity is provided."""
|
2016-10-27 07:16:23 +00:00
|
|
|
with assert_setup_component(0):
|
2017-05-18 04:57:50 +00:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2016-10-27 07:16:23 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {'event_type': 'bla'},
|
|
|
|
'condition': {
|
|
|
|
'platform': 'state',
|
|
|
|
'state': 'on',
|
|
|
|
'for': {
|
|
|
|
'seconds': 5
|
|
|
|
},
|
2016-04-04 19:18:58 +00:00
|
|
|
},
|
2016-10-27 07:16:23 +00:00
|
|
|
'action': {'service': 'test.automation'},
|
|
|
|
}})
|