2016-03-09 10:25:50 +01:00
|
|
|
"""The tests for numeric state automation."""
|
2017-09-05 02:01:01 +02:00
|
|
|
from datetime import timedelta
|
2015-09-13 12:53:37 +02:00
|
|
|
import unittest
|
2017-09-05 02:01:01 +02:00
|
|
|
from unittest.mock import patch
|
2015-09-13 12:53:37 +02:00
|
|
|
|
2017-09-05 02:01:01 +02:00
|
|
|
import homeassistant.components.automation as automation
|
2018-09-04 21:16:24 +02:00
|
|
|
from homeassistant.core import Context, callback
|
2017-03-05 01:41:54 -08:00
|
|
|
from homeassistant.setup import setup_component
|
2017-09-05 02:01:01 +02:00
|
|
|
import homeassistant.util.dt as dt_util
|
2015-09-13 12:53:37 +02:00
|
|
|
|
2017-09-05 02:01:01 +02:00
|
|
|
from tests.common import (
|
|
|
|
get_test_home_assistant, mock_component, fire_time_changed,
|
|
|
|
assert_setup_component)
|
2018-09-26 09:49:55 +02:00
|
|
|
from tests.components.automation import common
|
2016-02-14 15:08:23 -08:00
|
|
|
|
2015-09-13 12:53:37 +02:00
|
|
|
|
2016-12-02 06:45:19 +01:00
|
|
|
# pylint: disable=invalid-name
|
2015-09-13 19:59:26 +02:00
|
|
|
class TestAutomationNumericState(unittest.TestCase):
|
2016-03-09 10:25:50 +01:00
|
|
|
"""Test the event automation."""
|
2015-09-13 12:53:37 +02:00
|
|
|
|
2016-12-02 06:45:19 +01:00
|
|
|
def setUp(self):
|
2018-08-19 22:29:08 +02:00
|
|
|
"""Set up things to be run when tests are started."""
|
2016-02-14 15:08:23 -08:00
|
|
|
self.hass = get_test_home_assistant()
|
2017-03-01 05:33:19 +01:00
|
|
|
mock_component(self.hass, 'group')
|
2015-09-13 12:53:37 +02:00
|
|
|
self.calls = []
|
|
|
|
|
2016-11-05 16:36:20 -07:00
|
|
|
@callback
|
2015-09-13 12:53:37 +02:00
|
|
|
def record_call(service):
|
2018-08-24 11:28:43 +03:00
|
|
|
"""Record calls."""
|
2015-09-13 12:53:37 +02:00
|
|
|
self.calls.append(service)
|
|
|
|
|
|
|
|
self.hass.services.register('test', 'automation', record_call)
|
|
|
|
|
|
|
|
def tearDown(self): # pylint: disable=invalid-name
|
2016-03-09 10:25:50 +01:00
|
|
|
"""Stop everything that was started."""
|
2015-09-13 12:53:37 +02:00
|
|
|
self.hass.stop()
|
|
|
|
|
|
|
|
def test_if_fires_on_entity_change_below(self):
|
2018-05-13 11:06:15 +02:00
|
|
|
"""Test the firing with changed entity."""
|
2018-09-04 21:16:24 +02:00
|
|
|
context = Context()
|
2016-10-27 09:16:23 +02:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2015-09-13 12:53:37 +02:00
|
|
|
automation.DOMAIN: {
|
2015-09-14 22:05:40 -07:00
|
|
|
'trigger': {
|
|
|
|
'platform': 'numeric_state',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
'below': 10,
|
|
|
|
},
|
|
|
|
'action': {
|
2015-09-19 08:43:56 -07:00
|
|
|
'service': 'test.automation'
|
2015-09-14 22:05:40 -07:00
|
|
|
}
|
2015-09-13 12:53:37 +02:00
|
|
|
}
|
2016-04-04 12:18:58 -07:00
|
|
|
})
|
2015-09-13 12:53:37 +02:00
|
|
|
# 9 is below 10
|
2018-09-04 21:16:24 +02:00
|
|
|
self.hass.states.set('test.entity', 9, context=context)
|
2016-09-12 19:16:14 -07:00
|
|
|
self.hass.block_till_done()
|
2018-10-24 12:10:05 +02:00
|
|
|
assert 1 == len(self.calls)
|
2018-09-04 21:16:24 +02:00
|
|
|
assert self.calls[0].context is context
|
2015-09-13 12:53:37 +02:00
|
|
|
|
2016-08-25 23:25:57 -07:00
|
|
|
# Set above 12 so the automation will fire again
|
|
|
|
self.hass.states.set('test.entity', 12)
|
2018-09-26 09:49:55 +02:00
|
|
|
common.turn_off(self.hass)
|
2016-09-12 19:16:14 -07:00
|
|
|
self.hass.block_till_done()
|
2016-08-25 23:25:57 -07:00
|
|
|
self.hass.states.set('test.entity', 9)
|
2016-09-12 19:16:14 -07:00
|
|
|
self.hass.block_till_done()
|
2018-10-24 12:10:05 +02:00
|
|
|
assert 1 == len(self.calls)
|
2016-08-25 23:25:57 -07:00
|
|
|
|
2015-09-13 12:53:37 +02:00
|
|
|
def test_if_fires_on_entity_change_over_to_below(self):
|
2018-05-13 11:06:15 +02:00
|
|
|
"""Test the firing with changed entity."""
|
2015-09-13 12:53:37 +02:00
|
|
|
self.hass.states.set('test.entity', 11)
|
2016-09-12 19:16:14 -07:00
|
|
|
self.hass.block_till_done()
|
2015-09-13 12:53:37 +02:00
|
|
|
|
2016-10-27 09:16:23 +02:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2015-09-13 12:53:37 +02:00
|
|
|
automation.DOMAIN: {
|
2015-09-14 22:05:40 -07:00
|
|
|
'trigger': {
|
|
|
|
'platform': 'numeric_state',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
'below': 10,
|
|
|
|
},
|
|
|
|
'action': {
|
2015-09-19 08:43:56 -07:00
|
|
|
'service': 'test.automation'
|
2015-09-14 22:05:40 -07:00
|
|
|
}
|
2015-09-13 12:53:37 +02:00
|
|
|
}
|
2016-04-04 12:18:58 -07:00
|
|
|
})
|
2015-09-13 12:53:37 +02:00
|
|
|
|
|
|
|
# 9 is below 10
|
|
|
|
self.hass.states.set('test.entity', 9)
|
2016-09-12 19:16:14 -07:00
|
|
|
self.hass.block_till_done()
|
2018-10-24 12:10:05 +02:00
|
|
|
assert 1 == len(self.calls)
|
2015-09-13 12:53:37 +02:00
|
|
|
|
2017-11-30 21:03:52 +01:00
|
|
|
def test_if_fires_on_entities_change_over_to_below(self):
|
2018-05-13 11:06:15 +02:00
|
|
|
"""Test the firing with changed entities."""
|
2017-11-30 21:03:52 +01:00
|
|
|
self.hass.states.set('test.entity_1', 11)
|
|
|
|
self.hass.states.set('test.entity_2', 11)
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'numeric_state',
|
|
|
|
'entity_id': [
|
|
|
|
'test.entity_1',
|
|
|
|
'test.entity_2',
|
|
|
|
],
|
|
|
|
'below': 10,
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
# 9 is below 10
|
|
|
|
self.hass.states.set('test.entity_1', 9)
|
|
|
|
self.hass.block_till_done()
|
2018-10-24 12:10:05 +02:00
|
|
|
assert 1 == len(self.calls)
|
2017-11-30 21:03:52 +01:00
|
|
|
self.hass.states.set('test.entity_2', 9)
|
|
|
|
self.hass.block_till_done()
|
2018-10-24 12:10:05 +02:00
|
|
|
assert 2 == len(self.calls)
|
2017-11-30 21:03:52 +01:00
|
|
|
|
2015-09-13 12:53:37 +02:00
|
|
|
def test_if_not_fires_on_entity_change_below_to_below(self):
|
2018-05-13 11:06:15 +02:00
|
|
|
"""Test the firing with changed entity."""
|
2018-09-04 21:16:24 +02:00
|
|
|
context = Context()
|
2017-10-25 16:01:09 +02:00
|
|
|
self.hass.states.set('test.entity', 11)
|
2016-09-12 19:16:14 -07:00
|
|
|
self.hass.block_till_done()
|
2015-09-13 12:53:37 +02:00
|
|
|
|
2016-10-27 09:16:23 +02:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2015-09-13 12:53:37 +02:00
|
|
|
automation.DOMAIN: {
|
2015-09-14 22:05:40 -07:00
|
|
|
'trigger': {
|
|
|
|
'platform': 'numeric_state',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
'below': 10,
|
|
|
|
},
|
|
|
|
'action': {
|
2015-09-19 08:43:56 -07:00
|
|
|
'service': 'test.automation'
|
2015-09-14 22:05:40 -07:00
|
|
|
}
|
2015-09-13 12:53:37 +02:00
|
|
|
}
|
2016-04-04 12:18:58 -07:00
|
|
|
})
|
2015-09-13 12:53:37 +02:00
|
|
|
|
2017-10-25 16:01:09 +02:00
|
|
|
# 9 is below 10 so this should fire
|
2018-09-04 21:16:24 +02:00
|
|
|
self.hass.states.set('test.entity', 9, context=context)
|
2016-09-12 19:16:14 -07:00
|
|
|
self.hass.block_till_done()
|
2018-10-24 12:10:05 +02:00
|
|
|
assert 1 == len(self.calls)
|
2018-09-04 21:16:24 +02:00
|
|
|
assert self.calls[0].context is context
|
2017-10-25 16:01:09 +02:00
|
|
|
|
|
|
|
# already below so should not fire again
|
|
|
|
self.hass.states.set('test.entity', 5)
|
|
|
|
self.hass.block_till_done()
|
2018-10-24 12:10:05 +02:00
|
|
|
assert 1 == len(self.calls)
|
2015-09-13 12:53:37 +02:00
|
|
|
|
2017-11-30 21:03:52 +01:00
|
|
|
# still below so should not fire again
|
|
|
|
self.hass.states.set('test.entity', 3)
|
|
|
|
self.hass.block_till_done()
|
2018-10-24 12:10:05 +02:00
|
|
|
assert 1 == len(self.calls)
|
2017-11-30 21:03:52 +01:00
|
|
|
|
2017-06-02 01:43:24 -04:00
|
|
|
def test_if_not_below_fires_on_entity_change_to_equal(self):
|
2018-05-13 11:06:15 +02:00
|
|
|
"""Test the firing with changed entity."""
|
2017-06-02 01:43:24 -04:00
|
|
|
self.hass.states.set('test.entity', 11)
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'numeric_state',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
'below': 10,
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
# 10 is not below 10 so this should not fire again
|
|
|
|
self.hass.states.set('test.entity', 10)
|
|
|
|
self.hass.block_till_done()
|
2018-10-24 12:10:05 +02:00
|
|
|
assert 0 == len(self.calls)
|
2017-06-02 01:43:24 -04:00
|
|
|
|
2017-10-25 16:01:09 +02:00
|
|
|
def test_if_fires_on_initial_entity_below(self):
|
2018-05-13 11:06:15 +02:00
|
|
|
"""Test the firing when starting with a match."""
|
2017-10-25 16:01:09 +02:00
|
|
|
self.hass.states.set('test.entity', 9)
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'numeric_state',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
'below': 10,
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
# Fire on first update even if initial state was already below
|
|
|
|
self.hass.states.set('test.entity', 8)
|
|
|
|
self.hass.block_till_done()
|
2018-10-24 12:10:05 +02:00
|
|
|
assert 1 == len(self.calls)
|
2017-10-25 16:01:09 +02:00
|
|
|
|
|
|
|
def test_if_fires_on_initial_entity_above(self):
|
2018-05-13 11:06:15 +02:00
|
|
|
"""Test the firing when starting with a match."""
|
2017-10-25 16:01:09 +02:00
|
|
|
self.hass.states.set('test.entity', 11)
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'numeric_state',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
'above': 10,
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
# Fire on first update even if initial state was already above
|
|
|
|
self.hass.states.set('test.entity', 12)
|
|
|
|
self.hass.block_till_done()
|
2018-10-24 12:10:05 +02:00
|
|
|
assert 1 == len(self.calls)
|
2017-10-25 16:01:09 +02:00
|
|
|
|
2015-09-13 12:53:37 +02:00
|
|
|
def test_if_fires_on_entity_change_above(self):
|
2018-05-13 11:06:15 +02:00
|
|
|
"""Test the firing with changed entity."""
|
2016-10-27 09:16:23 +02:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2015-09-13 12:53:37 +02:00
|
|
|
automation.DOMAIN: {
|
2015-09-14 22:05:40 -07:00
|
|
|
'trigger': {
|
|
|
|
'platform': 'numeric_state',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
'above': 10,
|
|
|
|
},
|
|
|
|
'action': {
|
2015-09-19 08:43:56 -07:00
|
|
|
'service': 'test.automation'
|
2015-09-14 22:05:40 -07:00
|
|
|
}
|
2015-09-13 12:53:37 +02:00
|
|
|
}
|
2016-04-04 12:18:58 -07:00
|
|
|
})
|
2015-09-13 12:53:37 +02:00
|
|
|
# 11 is above 10
|
|
|
|
self.hass.states.set('test.entity', 11)
|
2016-09-12 19:16:14 -07:00
|
|
|
self.hass.block_till_done()
|
2018-10-24 12:10:05 +02:00
|
|
|
assert 1 == len(self.calls)
|
2015-09-13 12:53:37 +02:00
|
|
|
|
|
|
|
def test_if_fires_on_entity_change_below_to_above(self):
|
2018-05-13 11:06:15 +02:00
|
|
|
"""Test the firing with changed entity."""
|
2015-09-13 12:53:37 +02:00
|
|
|
# set initial state
|
|
|
|
self.hass.states.set('test.entity', 9)
|
2016-09-12 19:16:14 -07:00
|
|
|
self.hass.block_till_done()
|
2015-09-13 12:53:37 +02:00
|
|
|
|
2016-10-27 09:16:23 +02:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2015-09-13 12:53:37 +02:00
|
|
|
automation.DOMAIN: {
|
2015-09-14 22:05:40 -07:00
|
|
|
'trigger': {
|
|
|
|
'platform': 'numeric_state',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
'above': 10,
|
|
|
|
},
|
|
|
|
'action': {
|
2015-09-19 08:43:56 -07:00
|
|
|
'service': 'test.automation'
|
2015-09-14 22:05:40 -07:00
|
|
|
}
|
2015-09-13 12:53:37 +02:00
|
|
|
}
|
2016-04-04 12:18:58 -07:00
|
|
|
})
|
2015-09-13 12:53:37 +02:00
|
|
|
|
|
|
|
# 11 is above 10 and 9 is below
|
|
|
|
self.hass.states.set('test.entity', 11)
|
2016-09-12 19:16:14 -07:00
|
|
|
self.hass.block_till_done()
|
2018-10-24 12:10:05 +02:00
|
|
|
assert 1 == len(self.calls)
|
2015-09-13 12:53:37 +02:00
|
|
|
|
|
|
|
def test_if_not_fires_on_entity_change_above_to_above(self):
|
2018-05-13 11:06:15 +02:00
|
|
|
"""Test the firing with changed entity."""
|
2015-09-13 12:53:37 +02:00
|
|
|
# set initial state
|
2017-10-25 16:01:09 +02:00
|
|
|
self.hass.states.set('test.entity', 9)
|
2016-09-12 19:16:14 -07:00
|
|
|
self.hass.block_till_done()
|
2015-09-13 12:53:37 +02:00
|
|
|
|
2016-10-27 09:16:23 +02:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2015-09-13 12:53:37 +02:00
|
|
|
automation.DOMAIN: {
|
2015-09-14 22:05:40 -07:00
|
|
|
'trigger': {
|
|
|
|
'platform': 'numeric_state',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
'above': 10,
|
|
|
|
},
|
|
|
|
'action': {
|
2015-09-19 08:43:56 -07:00
|
|
|
'service': 'test.automation'
|
2015-09-14 22:05:40 -07:00
|
|
|
}
|
2015-09-13 12:53:37 +02:00
|
|
|
}
|
2016-04-04 12:18:58 -07:00
|
|
|
})
|
2015-09-13 12:53:37 +02:00
|
|
|
|
2017-10-25 16:01:09 +02:00
|
|
|
# 12 is above 10 so this should fire
|
2015-09-13 12:53:37 +02:00
|
|
|
self.hass.states.set('test.entity', 12)
|
2016-09-12 19:16:14 -07:00
|
|
|
self.hass.block_till_done()
|
2018-10-24 12:10:05 +02:00
|
|
|
assert 1 == len(self.calls)
|
2017-10-25 16:01:09 +02:00
|
|
|
|
|
|
|
# already above, should not fire again
|
|
|
|
self.hass.states.set('test.entity', 15)
|
|
|
|
self.hass.block_till_done()
|
2018-10-24 12:10:05 +02:00
|
|
|
assert 1 == len(self.calls)
|
2015-09-13 12:53:37 +02:00
|
|
|
|
2017-06-02 01:43:24 -04:00
|
|
|
def test_if_not_above_fires_on_entity_change_to_equal(self):
|
2018-05-13 11:06:15 +02:00
|
|
|
"""Test the firing with changed entity."""
|
2017-06-02 01:43:24 -04:00
|
|
|
# set initial state
|
|
|
|
self.hass.states.set('test.entity', 9)
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'numeric_state',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
'above': 10,
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
# 10 is not above 10 so this should not fire again
|
|
|
|
self.hass.states.set('test.entity', 10)
|
|
|
|
self.hass.block_till_done()
|
2018-10-24 12:10:05 +02:00
|
|
|
assert 0 == len(self.calls)
|
2017-06-02 01:43:24 -04:00
|
|
|
|
2015-09-13 12:53:37 +02:00
|
|
|
def test_if_fires_on_entity_change_below_range(self):
|
2018-05-13 11:06:15 +02:00
|
|
|
"""Test the firing with changed entity."""
|
2016-10-27 09:16:23 +02:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2015-09-13 12:53:37 +02:00
|
|
|
automation.DOMAIN: {
|
2015-09-14 22:05:40 -07:00
|
|
|
'trigger': {
|
|
|
|
'platform': 'numeric_state',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
'below': 10,
|
|
|
|
'above': 5,
|
|
|
|
},
|
|
|
|
'action': {
|
2015-09-19 08:43:56 -07:00
|
|
|
'service': 'test.automation'
|
2015-09-14 22:05:40 -07:00
|
|
|
}
|
2015-09-13 12:53:37 +02:00
|
|
|
}
|
2016-04-04 12:18:58 -07:00
|
|
|
})
|
2015-09-13 12:53:37 +02:00
|
|
|
# 9 is below 10
|
|
|
|
self.hass.states.set('test.entity', 9)
|
2016-09-12 19:16:14 -07:00
|
|
|
self.hass.block_till_done()
|
2018-10-24 12:10:05 +02:00
|
|
|
assert 1 == len(self.calls)
|
2015-09-13 12:53:37 +02:00
|
|
|
|
|
|
|
def test_if_fires_on_entity_change_below_above_range(self):
|
2018-05-13 11:06:15 +02:00
|
|
|
"""Test the firing with changed entity."""
|
2016-10-27 09:16:23 +02:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2015-09-13 12:53:37 +02:00
|
|
|
automation.DOMAIN: {
|
2015-09-14 22:05:40 -07:00
|
|
|
'trigger': {
|
|
|
|
'platform': 'numeric_state',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
'below': 10,
|
|
|
|
'above': 5,
|
|
|
|
},
|
|
|
|
'action': {
|
2015-09-19 08:43:56 -07:00
|
|
|
'service': 'test.automation'
|
2015-09-14 22:05:40 -07:00
|
|
|
}
|
2015-09-13 12:53:37 +02:00
|
|
|
}
|
2016-04-04 12:18:58 -07:00
|
|
|
})
|
2015-09-13 12:53:37 +02:00
|
|
|
# 4 is below 5
|
|
|
|
self.hass.states.set('test.entity', 4)
|
2016-09-12 19:16:14 -07:00
|
|
|
self.hass.block_till_done()
|
2018-10-24 12:10:05 +02:00
|
|
|
assert 0 == len(self.calls)
|
2015-09-13 12:53:37 +02:00
|
|
|
|
|
|
|
def test_if_fires_on_entity_change_over_to_below_range(self):
|
2018-05-13 11:06:15 +02:00
|
|
|
"""Test the firing with changed entity."""
|
2015-09-13 12:53:37 +02:00
|
|
|
self.hass.states.set('test.entity', 11)
|
2016-09-12 19:16:14 -07:00
|
|
|
self.hass.block_till_done()
|
2015-09-13 12:53:37 +02:00
|
|
|
|
2016-10-27 09:16:23 +02:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2015-09-13 12:53:37 +02:00
|
|
|
automation.DOMAIN: {
|
2015-09-14 22:05:40 -07:00
|
|
|
'trigger': {
|
|
|
|
'platform': 'numeric_state',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
'below': 10,
|
|
|
|
'above': 5,
|
|
|
|
},
|
|
|
|
'action': {
|
2015-09-19 08:43:56 -07:00
|
|
|
'service': 'test.automation'
|
2015-09-14 22:05:40 -07:00
|
|
|
}
|
2015-09-13 12:53:37 +02:00
|
|
|
}
|
2016-04-04 12:18:58 -07:00
|
|
|
})
|
2015-09-13 12:53:37 +02:00
|
|
|
|
|
|
|
# 9 is below 10
|
|
|
|
self.hass.states.set('test.entity', 9)
|
2016-09-12 19:16:14 -07:00
|
|
|
self.hass.block_till_done()
|
2018-10-24 12:10:05 +02:00
|
|
|
assert 1 == len(self.calls)
|
2015-09-13 12:53:37 +02:00
|
|
|
|
|
|
|
def test_if_fires_on_entity_change_over_to_below_above_range(self):
|
2018-05-13 11:06:15 +02:00
|
|
|
"""Test the firing with changed entity."""
|
2015-09-13 12:53:37 +02:00
|
|
|
self.hass.states.set('test.entity', 11)
|
2016-09-12 19:16:14 -07:00
|
|
|
self.hass.block_till_done()
|
2015-09-13 12:53:37 +02:00
|
|
|
|
2016-10-27 09:16:23 +02:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2015-09-13 12:53:37 +02:00
|
|
|
automation.DOMAIN: {
|
2015-09-14 22:05:40 -07:00
|
|
|
'trigger': {
|
|
|
|
'platform': 'numeric_state',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
'below': 10,
|
|
|
|
'above': 5,
|
|
|
|
},
|
|
|
|
'action': {
|
2015-09-19 08:43:56 -07:00
|
|
|
'service': 'test.automation'
|
2015-09-14 22:05:40 -07:00
|
|
|
}
|
2015-09-13 12:53:37 +02:00
|
|
|
}
|
2016-04-04 12:18:58 -07:00
|
|
|
})
|
2015-09-13 12:53:37 +02:00
|
|
|
|
2015-09-13 13:05:36 +02:00
|
|
|
# 4 is below 5 so it should not fire
|
2015-09-13 12:53:37 +02:00
|
|
|
self.hass.states.set('test.entity', 4)
|
2016-09-12 19:16:14 -07:00
|
|
|
self.hass.block_till_done()
|
2018-10-24 12:10:05 +02:00
|
|
|
assert 0 == len(self.calls)
|
2015-09-13 12:53:37 +02:00
|
|
|
|
|
|
|
def test_if_not_fires_if_entity_not_match(self):
|
2018-05-13 11:06:15 +02:00
|
|
|
"""Test if not fired with non matching entity."""
|
2016-10-27 09:16:23 +02:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2015-09-13 12:53:37 +02:00
|
|
|
automation.DOMAIN: {
|
2015-09-14 22:05:40 -07:00
|
|
|
'trigger': {
|
|
|
|
'platform': 'numeric_state',
|
|
|
|
'entity_id': 'test.another_entity',
|
2016-04-28 12:03:57 +02:00
|
|
|
'below': 100,
|
2015-09-14 22:05:40 -07:00
|
|
|
},
|
|
|
|
'action': {
|
2015-09-19 08:43:56 -07:00
|
|
|
'service': 'test.automation'
|
2015-09-14 22:05:40 -07:00
|
|
|
}
|
2015-09-13 12:53:37 +02:00
|
|
|
}
|
2016-04-04 12:18:58 -07:00
|
|
|
})
|
2015-09-13 12:53:37 +02:00
|
|
|
|
|
|
|
self.hass.states.set('test.entity', 11)
|
2016-09-12 19:16:14 -07:00
|
|
|
self.hass.block_till_done()
|
2018-10-24 12:10:05 +02:00
|
|
|
assert 0 == len(self.calls)
|
2015-09-14 20:33:01 +02:00
|
|
|
|
2015-11-29 21:37:08 +00:00
|
|
|
def test_if_fires_on_entity_change_below_with_attribute(self):
|
2018-05-13 11:06:15 +02:00
|
|
|
"""Test attributes change."""
|
2016-10-27 09:16:23 +02:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2015-11-29 21:37:08 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'numeric_state',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
'below': 10,
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation'
|
|
|
|
}
|
|
|
|
}
|
2016-04-04 12:18:58 -07:00
|
|
|
})
|
2015-11-29 21:37:08 +00:00
|
|
|
# 9 is below 10
|
2016-02-13 14:19:11 +01:00
|
|
|
self.hass.states.set('test.entity', 9, {'test_attribute': 11})
|
2016-09-12 19:16:14 -07:00
|
|
|
self.hass.block_till_done()
|
2018-10-24 12:10:05 +02:00
|
|
|
assert 1 == len(self.calls)
|
2015-11-29 21:37:08 +00:00
|
|
|
|
|
|
|
def test_if_not_fires_on_entity_change_not_below_with_attribute(self):
|
2018-05-13 11:06:15 +02:00
|
|
|
"""Test attributes."""
|
2016-10-27 09:16:23 +02:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2015-11-29 21:37:08 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'numeric_state',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
'below': 10,
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation'
|
|
|
|
}
|
|
|
|
}
|
2016-04-04 12:18:58 -07:00
|
|
|
})
|
2015-11-29 21:37:08 +00:00
|
|
|
# 11 is not below 10
|
2016-02-13 14:19:11 +01:00
|
|
|
self.hass.states.set('test.entity', 11, {'test_attribute': 9})
|
2016-09-12 19:16:14 -07:00
|
|
|
self.hass.block_till_done()
|
2018-10-24 12:10:05 +02:00
|
|
|
assert 0 == len(self.calls)
|
2015-11-29 21:37:08 +00:00
|
|
|
|
|
|
|
def test_if_fires_on_attribute_change_with_attribute_below(self):
|
2018-05-13 11:06:15 +02:00
|
|
|
"""Test attributes change."""
|
2016-10-27 09:16:23 +02:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2015-11-29 21:37:08 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'numeric_state',
|
|
|
|
'entity_id': 'test.entity',
|
2015-12-15 10:12:43 -07:00
|
|
|
'value_template': '{{ state.attributes.test_attribute }}',
|
2015-11-29 21:37:08 +00:00
|
|
|
'below': 10,
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation'
|
|
|
|
}
|
|
|
|
}
|
2016-04-04 12:18:58 -07:00
|
|
|
})
|
2015-11-29 21:37:08 +00:00
|
|
|
# 9 is below 10
|
2016-02-13 14:19:11 +01:00
|
|
|
self.hass.states.set('test.entity', 'entity', {'test_attribute': 9})
|
2016-09-12 19:16:14 -07:00
|
|
|
self.hass.block_till_done()
|
2018-10-24 12:10:05 +02:00
|
|
|
assert 1 == len(self.calls)
|
2015-11-29 21:37:08 +00:00
|
|
|
|
|
|
|
def test_if_not_fires_on_attribute_change_with_attribute_not_below(self):
|
2018-05-13 11:06:15 +02:00
|
|
|
"""Test attributes change."""
|
2016-10-27 09:16:23 +02:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2015-11-29 21:37:08 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'numeric_state',
|
|
|
|
'entity_id': 'test.entity',
|
2015-12-15 10:12:43 -07:00
|
|
|
'value_template': '{{ state.attributes.test_attribute }}',
|
2015-11-29 21:37:08 +00:00
|
|
|
'below': 10,
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation'
|
|
|
|
}
|
|
|
|
}
|
2016-04-04 12:18:58 -07:00
|
|
|
})
|
2015-11-29 21:37:08 +00:00
|
|
|
# 11 is not below 10
|
2016-02-13 14:19:11 +01:00
|
|
|
self.hass.states.set('test.entity', 'entity', {'test_attribute': 11})
|
2016-09-12 19:16:14 -07:00
|
|
|
self.hass.block_till_done()
|
2018-10-24 12:10:05 +02:00
|
|
|
assert 0 == len(self.calls)
|
2015-11-29 21:37:08 +00:00
|
|
|
|
|
|
|
def test_if_not_fires_on_entity_change_with_attribute_below(self):
|
2018-05-13 11:06:15 +02:00
|
|
|
"""Test attributes change."""
|
2016-10-27 09:16:23 +02:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2015-11-29 21:37:08 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'numeric_state',
|
|
|
|
'entity_id': 'test.entity',
|
2015-12-15 10:12:43 -07:00
|
|
|
'value_template': '{{ state.attributes.test_attribute }}',
|
2015-11-29 21:37:08 +00:00
|
|
|
'below': 10,
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation'
|
|
|
|
}
|
|
|
|
}
|
2016-04-04 12:18:58 -07:00
|
|
|
})
|
2015-11-29 21:37:08 +00:00
|
|
|
# 11 is not below 10, entity state value should not be tested
|
2016-02-13 14:19:11 +01:00
|
|
|
self.hass.states.set('test.entity', '9', {'test_attribute': 11})
|
2016-09-12 19:16:14 -07:00
|
|
|
self.hass.block_till_done()
|
2018-10-24 12:10:05 +02:00
|
|
|
assert 0 == len(self.calls)
|
2015-11-29 21:37:08 +00:00
|
|
|
|
|
|
|
def test_if_not_fires_on_entity_change_with_not_attribute_below(self):
|
2018-05-13 11:06:15 +02:00
|
|
|
"""Test attributes change."""
|
2016-10-27 09:16:23 +02:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2015-11-29 21:37:08 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'numeric_state',
|
|
|
|
'entity_id': 'test.entity',
|
2015-12-15 10:12:43 -07:00
|
|
|
'value_template': '{{ state.attributes.test_attribute }}',
|
2015-11-29 21:37:08 +00:00
|
|
|
'below': 10,
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation'
|
|
|
|
}
|
|
|
|
}
|
2016-04-04 12:18:58 -07:00
|
|
|
})
|
2015-11-29 21:37:08 +00:00
|
|
|
# 11 is not below 10, entity state value should not be tested
|
|
|
|
self.hass.states.set('test.entity', 'entity')
|
2016-09-12 19:16:14 -07:00
|
|
|
self.hass.block_till_done()
|
2018-10-24 12:10:05 +02:00
|
|
|
assert 0 == len(self.calls)
|
2015-11-29 21:37:08 +00:00
|
|
|
|
2016-02-14 13:07:21 -08:00
|
|
|
def test_fires_on_attr_change_with_attribute_below_and_multiple_attr(self):
|
2018-05-13 11:06:15 +02:00
|
|
|
"""Test attributes change."""
|
2016-10-27 09:16:23 +02:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2015-11-29 21:37:08 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'numeric_state',
|
|
|
|
'entity_id': 'test.entity',
|
2015-12-15 10:12:43 -07:00
|
|
|
'value_template': '{{ state.attributes.test_attribute }}',
|
2015-11-29 21:37:08 +00:00
|
|
|
'below': 10,
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation'
|
|
|
|
}
|
|
|
|
}
|
2016-04-04 12:18:58 -07:00
|
|
|
})
|
2015-11-29 21:37:08 +00:00
|
|
|
# 9 is not below 10
|
2016-02-14 13:07:21 -08:00
|
|
|
self.hass.states.set('test.entity', 'entity',
|
|
|
|
{'test_attribute': 9, 'not_test_attribute': 11})
|
2016-09-12 19:16:14 -07:00
|
|
|
self.hass.block_till_done()
|
2018-10-24 12:10:05 +02:00
|
|
|
assert 1 == len(self.calls)
|
2015-11-29 21:37:08 +00:00
|
|
|
|
2015-12-14 14:47:32 -07:00
|
|
|
def test_template_list(self):
|
2018-05-13 11:06:15 +02:00
|
|
|
"""Test template list."""
|
2016-10-27 09:16:23 +02:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2015-12-14 14:47:32 -07:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'numeric_state',
|
|
|
|
'entity_id': 'test.entity',
|
2016-02-14 13:07:21 -08:00
|
|
|
'value_template':
|
|
|
|
'{{ state.attributes.test_attribute[2] }}',
|
2015-12-14 14:47:32 -07:00
|
|
|
'below': 10,
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation'
|
|
|
|
}
|
|
|
|
}
|
2016-04-04 12:18:58 -07:00
|
|
|
})
|
2015-12-14 14:47:32 -07:00
|
|
|
# 3 is below 10
|
2016-02-14 13:07:21 -08:00
|
|
|
self.hass.states.set('test.entity', 'entity',
|
|
|
|
{'test_attribute': [11, 15, 3]})
|
2016-09-12 19:16:14 -07:00
|
|
|
self.hass.block_till_done()
|
2018-10-24 12:10:05 +02:00
|
|
|
assert 1 == len(self.calls)
|
2015-12-14 14:47:32 -07:00
|
|
|
|
|
|
|
def test_template_string(self):
|
2018-05-13 11:06:15 +02:00
|
|
|
"""Test template string."""
|
2016-10-27 09:16:23 +02:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2015-12-14 14:47:32 -07:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'numeric_state',
|
|
|
|
'entity_id': 'test.entity',
|
2016-02-14 13:07:21 -08:00
|
|
|
'value_template':
|
|
|
|
'{{ state.attributes.test_attribute | multiply(10) }}',
|
2015-12-14 14:47:32 -07:00
|
|
|
'below': 10,
|
|
|
|
},
|
|
|
|
'action': {
|
2016-04-21 13:59:42 -07:00
|
|
|
'service': 'test.automation',
|
|
|
|
'data_template': {
|
|
|
|
'some': '{{ trigger.%s }}' % '}} - {{ trigger.'.join((
|
2016-12-02 06:45:19 +01:00
|
|
|
'platform', 'entity_id', 'below', 'above',
|
|
|
|
'from_state.state', 'to_state.state'))
|
2016-04-21 13:59:42 -07:00
|
|
|
},
|
2015-12-14 14:47:32 -07:00
|
|
|
}
|
|
|
|
}
|
2016-04-04 12:18:58 -07:00
|
|
|
})
|
2016-04-21 13:59:42 -07:00
|
|
|
self.hass.states.set('test.entity', 'test state 1',
|
|
|
|
{'test_attribute': '1.2'})
|
2016-09-12 19:16:14 -07:00
|
|
|
self.hass.block_till_done()
|
2016-04-21 13:59:42 -07:00
|
|
|
self.hass.states.set('test.entity', 'test state 2',
|
2016-02-14 13:07:21 -08:00
|
|
|
{'test_attribute': '0.9'})
|
2016-09-12 19:16:14 -07:00
|
|
|
self.hass.block_till_done()
|
2018-10-24 12:10:05 +02:00
|
|
|
assert 1 == len(self.calls)
|
|
|
|
assert 'numeric_state - test.entity - 10.0 - None - test state 1 - ' \
|
|
|
|
'test state 2' == \
|
|
|
|
self.calls[0].data['some']
|
2015-12-14 14:47:32 -07:00
|
|
|
|
2016-02-14 13:07:21 -08:00
|
|
|
def test_not_fires_on_attr_change_with_attr_not_below_multiple_attr(self):
|
2018-05-13 11:06:15 +02:00
|
|
|
"""Test if not fired changed attributes."""
|
2016-10-27 09:16:23 +02:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2015-11-29 21:37:08 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'numeric_state',
|
|
|
|
'entity_id': 'test.entity',
|
2015-12-15 10:12:43 -07:00
|
|
|
'value_template': '{{ state.attributes.test_attribute }}',
|
2015-11-29 21:37:08 +00:00
|
|
|
'below': 10,
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation'
|
|
|
|
}
|
|
|
|
}
|
2016-04-04 12:18:58 -07:00
|
|
|
})
|
2015-11-29 21:37:08 +00:00
|
|
|
# 11 is not below 10
|
2016-02-14 13:07:21 -08:00
|
|
|
self.hass.states.set('test.entity', 'entity',
|
|
|
|
{'test_attribute': 11, 'not_test_attribute': 9})
|
2016-09-12 19:16:14 -07:00
|
|
|
self.hass.block_till_done()
|
2018-10-24 12:10:05 +02:00
|
|
|
assert 0 == len(self.calls)
|
2015-11-29 21:37:08 +00:00
|
|
|
|
2015-09-14 20:33:01 +02:00
|
|
|
def test_if_action(self):
|
2018-05-13 11:06:15 +02:00
|
|
|
"""Test if action."""
|
2015-09-14 20:33:01 +02:00
|
|
|
entity_id = 'domain.test_entity'
|
2016-10-27 09:16:23 +02:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2015-09-14 20:33:01 +02:00
|
|
|
automation.DOMAIN: {
|
2015-09-14 22:05:40 -07:00
|
|
|
'trigger': {
|
|
|
|
'platform': 'event',
|
|
|
|
'event_type': 'test_event',
|
|
|
|
},
|
|
|
|
'condition': {
|
2016-10-01 01:22:13 -07:00
|
|
|
'condition': 'numeric_state',
|
2015-09-14 22:05:40 -07:00
|
|
|
'entity_id': entity_id,
|
2017-06-02 01:43:24 -04:00
|
|
|
'above': 8,
|
|
|
|
'below': 12,
|
2015-09-14 22:05:40 -07:00
|
|
|
},
|
|
|
|
'action': {
|
2015-09-19 08:43:56 -07:00
|
|
|
'service': 'test.automation'
|
2015-09-14 22:05:40 -07:00
|
|
|
}
|
2015-09-14 20:33:01 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-06-02 01:43:24 -04:00
|
|
|
self.hass.states.set(entity_id, 10)
|
2015-09-14 20:33:01 +02:00
|
|
|
self.hass.bus.fire('test_event')
|
2016-09-12 19:16:14 -07:00
|
|
|
self.hass.block_till_done()
|
2015-09-14 20:33:01 +02:00
|
|
|
|
2018-10-24 12:10:05 +02:00
|
|
|
assert 1 == len(self.calls)
|
2015-09-14 20:33:01 +02:00
|
|
|
|
2017-06-02 01:43:24 -04:00
|
|
|
self.hass.states.set(entity_id, 8)
|
2015-09-14 20:33:01 +02:00
|
|
|
self.hass.bus.fire('test_event')
|
2016-09-12 19:16:14 -07:00
|
|
|
self.hass.block_till_done()
|
2015-09-14 20:33:01 +02:00
|
|
|
|
2018-10-24 12:10:05 +02:00
|
|
|
assert 1 == len(self.calls)
|
2015-09-14 20:33:01 +02:00
|
|
|
|
2017-06-02 01:43:24 -04:00
|
|
|
self.hass.states.set(entity_id, 9)
|
2015-09-14 20:33:01 +02:00
|
|
|
self.hass.bus.fire('test_event')
|
2016-09-12 19:16:14 -07:00
|
|
|
self.hass.block_till_done()
|
2015-09-14 20:33:01 +02:00
|
|
|
|
2018-10-24 12:10:05 +02:00
|
|
|
assert 2 == len(self.calls)
|
2017-09-05 02:01:01 +02:00
|
|
|
|
|
|
|
def test_if_fails_setup_bad_for(self):
|
|
|
|
"""Test for setup failure for bad for."""
|
|
|
|
with assert_setup_component(0):
|
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'numeric_state',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
'above': 8,
|
|
|
|
'below': 12,
|
|
|
|
'for': {
|
|
|
|
'invalid': 5
|
|
|
|
},
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'homeassistant.turn_on',
|
|
|
|
}
|
|
|
|
}})
|
|
|
|
|
|
|
|
def test_if_fails_setup_for_without_above_below(self):
|
|
|
|
"""Test for setup failures for missing above or below."""
|
|
|
|
with assert_setup_component(0):
|
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'numeric_state',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
'for': {
|
|
|
|
'seconds': 5
|
|
|
|
},
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'homeassistant.turn_on',
|
|
|
|
}
|
|
|
|
}})
|
|
|
|
|
|
|
|
def test_if_not_fires_on_entity_change_with_for(self):
|
|
|
|
"""Test for not firing on entity change with for."""
|
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'numeric_state',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
'above': 8,
|
|
|
|
'below': 12,
|
|
|
|
'for': {
|
|
|
|
'seconds': 5
|
|
|
|
},
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
self.hass.states.set('test.entity', 9)
|
|
|
|
self.hass.block_till_done()
|
|
|
|
self.hass.states.set('test.entity', 15)
|
|
|
|
self.hass.block_till_done()
|
|
|
|
fire_time_changed(self.hass, dt_util.utcnow() + timedelta(seconds=10))
|
|
|
|
self.hass.block_till_done()
|
2018-10-24 12:10:05 +02:00
|
|
|
assert 0 == len(self.calls)
|
2017-09-05 02:01:01 +02:00
|
|
|
|
2018-01-29 23:37:19 +01:00
|
|
|
def test_if_not_fires_on_entities_change_with_for_after_stop(self):
|
2017-11-30 21:03:52 +01:00
|
|
|
"""Test for not firing on entities change with for after stop."""
|
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'numeric_state',
|
|
|
|
'entity_id': [
|
|
|
|
'test.entity_1',
|
|
|
|
'test.entity_2',
|
|
|
|
],
|
|
|
|
'above': 8,
|
|
|
|
'below': 12,
|
|
|
|
'for': {
|
|
|
|
'seconds': 5
|
|
|
|
},
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
self.hass.states.set('test.entity_1', 9)
|
|
|
|
self.hass.states.set('test.entity_2', 9)
|
|
|
|
self.hass.block_till_done()
|
|
|
|
fire_time_changed(self.hass, dt_util.utcnow() + timedelta(seconds=10))
|
|
|
|
self.hass.block_till_done()
|
2018-10-24 12:10:05 +02:00
|
|
|
assert 2 == len(self.calls)
|
2017-11-30 21:03:52 +01:00
|
|
|
|
|
|
|
self.hass.states.set('test.entity_1', 15)
|
|
|
|
self.hass.states.set('test.entity_2', 15)
|
|
|
|
self.hass.block_till_done()
|
|
|
|
self.hass.states.set('test.entity_1', 9)
|
|
|
|
self.hass.states.set('test.entity_2', 9)
|
|
|
|
self.hass.block_till_done()
|
2018-09-26 09:49:55 +02:00
|
|
|
common.turn_off(self.hass)
|
2017-11-30 21:03:52 +01:00
|
|
|
self.hass.block_till_done()
|
|
|
|
|
|
|
|
fire_time_changed(self.hass, dt_util.utcnow() + timedelta(seconds=10))
|
|
|
|
self.hass.block_till_done()
|
2018-10-24 12:10:05 +02:00
|
|
|
assert 2 == len(self.calls)
|
2017-11-30 21:03:52 +01:00
|
|
|
|
2017-09-05 02:01:01 +02: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': 'numeric_state',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
'above': 8,
|
|
|
|
'below': 12,
|
|
|
|
'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', 9)
|
|
|
|
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', 9,
|
|
|
|
attributes={"mock_attr": "attr_change"})
|
|
|
|
self.hass.block_till_done()
|
2018-10-24 12:10:05 +02:00
|
|
|
assert 0 == len(self.calls)
|
2017-09-05 02:01:01 +02:00
|
|
|
mock_utcnow.return_value += timedelta(seconds=4)
|
|
|
|
fire_time_changed(self.hass, mock_utcnow.return_value)
|
|
|
|
self.hass.block_till_done()
|
2018-10-24 12:10:05 +02:00
|
|
|
assert 1 == len(self.calls)
|
2017-09-05 02:01:01 +02:00
|
|
|
|
|
|
|
def test_if_fires_on_entity_change_with_for(self):
|
|
|
|
"""Test for firing on entity change with for."""
|
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'numeric_state',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
'above': 8,
|
|
|
|
'below': 12,
|
|
|
|
'for': {
|
|
|
|
'seconds': 5
|
|
|
|
},
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
self.hass.states.set('test.entity', 9)
|
|
|
|
self.hass.block_till_done()
|
|
|
|
fire_time_changed(self.hass, dt_util.utcnow() + timedelta(seconds=10))
|
|
|
|
self.hass.block_till_done()
|
2018-10-24 12:10:05 +02:00
|
|
|
assert 1 == len(self.calls)
|
2017-10-12 16:57:18 +02:00
|
|
|
|
|
|
|
def test_wait_template_with_trigger(self):
|
|
|
|
"""Test using wait template with 'trigger.entity_id'."""
|
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'numeric_state',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
'above': 10,
|
|
|
|
},
|
|
|
|
'action': [
|
|
|
|
{'wait_template':
|
|
|
|
"{{ states(trigger.entity_id) | int < 10 }}"},
|
|
|
|
{'service': 'test.automation',
|
|
|
|
'data_template': {
|
|
|
|
'some':
|
|
|
|
'{{ trigger.%s }}' % '}} - {{ trigger.'.join((
|
|
|
|
'platform', 'entity_id', 'to_state.state'))
|
|
|
|
}}
|
|
|
|
],
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
self.hass.block_till_done()
|
|
|
|
self.calls = []
|
|
|
|
|
|
|
|
self.hass.states.set('test.entity', '12')
|
|
|
|
self.hass.block_till_done()
|
|
|
|
self.hass.states.set('test.entity', '8')
|
|
|
|
self.hass.block_till_done()
|
2018-10-24 12:10:05 +02:00
|
|
|
assert 1 == len(self.calls)
|
|
|
|
assert 'numeric_state - test.entity - 12' == \
|
|
|
|
self.calls[0].data['some']
|