2016-03-09 09:25:50 +00:00
|
|
|
"""The tests for numeric state automation."""
|
2017-09-05 00:01:01 +00:00
|
|
|
from datetime import timedelta
|
2015-09-13 10:53:37 +00:00
|
|
|
|
2019-12-08 16:29:39 +00:00
|
|
|
import pytest
|
2020-03-04 01:26:44 +00:00
|
|
|
import voluptuous as vol
|
2019-12-08 16:29:39 +00:00
|
|
|
|
2017-09-05 00:01:01 +00:00
|
|
|
import homeassistant.components.automation as automation
|
2020-03-04 01:26:44 +00:00
|
|
|
from homeassistant.components.automation import numeric_state
|
2018-10-26 09:31:14 +00:00
|
|
|
from homeassistant.core import Context
|
|
|
|
from homeassistant.setup import async_setup_component
|
2017-09-05 00:01:01 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
2015-09-13 10:53:37 +00:00
|
|
|
|
2020-05-03 18:27:19 +00:00
|
|
|
from tests.async_mock import patch
|
2017-09-05 00:01:01 +00:00
|
|
|
from tests.common import (
|
2019-07-31 19:25:30 +00:00
|
|
|
assert_setup_component,
|
2019-12-08 16:29:39 +00:00
|
|
|
async_fire_time_changed,
|
2019-07-31 19:25:30 +00:00
|
|
|
async_mock_service,
|
2019-12-08 16:29:39 +00:00
|
|
|
mock_component,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-09-26 07:49:55 +00:00
|
|
|
from tests.components.automation import common
|
2016-02-14 23:08:23 +00:00
|
|
|
|
2015-09-13 10:53:37 +00:00
|
|
|
|
2018-10-26 09:31:14 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def calls(hass):
|
2020-01-27 18:56:26 +00:00
|
|
|
"""Track calls to a mock service."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return async_mock_service(hass, "test", "automation")
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def setup_comp(hass):
|
|
|
|
"""Initialize components."""
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_component(hass, "group")
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_if_fires_on_entity_change_below(hass, calls):
|
|
|
|
"""Test the firing with changed entity."""
|
|
|
|
context = Context()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "numeric_state",
|
|
|
|
"entity_id": "test.entity",
|
|
|
|
"below": 10,
|
|
|
|
},
|
|
|
|
"action": {"service": "test.automation"},
|
2018-10-26 09:31:14 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2018-10-26 09:31:14 +00:00
|
|
|
# 9 is below 10
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", 9, context=context)
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 1
|
2019-03-01 18:08:38 +00:00
|
|
|
assert calls[0].context.parent_id == context.id
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
# Set above 12 so the automation will fire again
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", 12)
|
2018-10-26 09:31:14 +00:00
|
|
|
await common.async_turn_off(hass)
|
|
|
|
await hass.async_block_till_done()
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", 9)
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 1
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_if_fires_on_entity_change_over_to_below(hass, calls):
|
|
|
|
"""Test the firing with changed entity."""
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", 11)
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "numeric_state",
|
|
|
|
"entity_id": "test.entity",
|
|
|
|
"below": 10,
|
|
|
|
},
|
|
|
|
"action": {"service": "test.automation"},
|
2018-10-26 09:31:14 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
# 9 is below 10
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", 9)
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 1
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_if_fires_on_entities_change_over_to_below(hass, calls):
|
|
|
|
"""Test the firing with changed entities."""
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity_1", 11)
|
|
|
|
hass.states.async_set("test.entity_2", 11)
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "numeric_state",
|
|
|
|
"entity_id": ["test.entity_1", "test.entity_2"],
|
|
|
|
"below": 10,
|
|
|
|
},
|
|
|
|
"action": {"service": "test.automation"},
|
2018-10-26 09:31:14 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
# 9 is below 10
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity_1", 9)
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 1
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity_2", 9)
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 2
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_if_not_fires_on_entity_change_below_to_below(hass, calls):
|
|
|
|
"""Test the firing with changed entity."""
|
|
|
|
context = Context()
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", 11)
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "numeric_state",
|
|
|
|
"entity_id": "test.entity",
|
|
|
|
"below": 10,
|
|
|
|
},
|
|
|
|
"action": {"service": "test.automation"},
|
2018-10-26 09:31:14 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
# 9 is below 10 so this should fire
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", 9, context=context)
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 1
|
2019-03-01 18:08:38 +00:00
|
|
|
assert calls[0].context.parent_id == context.id
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
# already below so should not fire again
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", 5)
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 1
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
# still below so should not fire again
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", 3)
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 1
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_if_not_below_fires_on_entity_change_to_equal(hass, calls):
|
|
|
|
"""Test the firing with changed entity."""
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", 11)
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "numeric_state",
|
|
|
|
"entity_id": "test.entity",
|
|
|
|
"below": 10,
|
|
|
|
},
|
|
|
|
"action": {"service": "test.automation"},
|
2018-10-26 09:31:14 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
# 10 is not below 10 so this should not fire again
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", 10)
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 0
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_if_fires_on_initial_entity_below(hass, calls):
|
|
|
|
"""Test the firing when starting with a match."""
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", 9)
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "numeric_state",
|
|
|
|
"entity_id": "test.entity",
|
|
|
|
"below": 10,
|
|
|
|
},
|
|
|
|
"action": {"service": "test.automation"},
|
2018-10-26 09:31:14 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
# Fire on first update even if initial state was already below
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", 8)
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 1
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_if_fires_on_initial_entity_above(hass, calls):
|
|
|
|
"""Test the firing when starting with a match."""
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", 11)
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "numeric_state",
|
|
|
|
"entity_id": "test.entity",
|
|
|
|
"above": 10,
|
|
|
|
},
|
|
|
|
"action": {"service": "test.automation"},
|
2018-10-26 09:31:14 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
# Fire on first update even if initial state was already above
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", 12)
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 1
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_if_fires_on_entity_change_above(hass, calls):
|
|
|
|
"""Test the firing with changed entity."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "numeric_state",
|
|
|
|
"entity_id": "test.entity",
|
|
|
|
"above": 10,
|
|
|
|
},
|
|
|
|
"action": {"service": "test.automation"},
|
2018-10-26 09:31:14 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2018-10-26 09:31:14 +00:00
|
|
|
# 11 is above 10
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", 11)
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 1
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_if_fires_on_entity_change_below_to_above(hass, calls):
|
|
|
|
"""Test the firing with changed entity."""
|
|
|
|
# set initial state
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", 9)
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "numeric_state",
|
|
|
|
"entity_id": "test.entity",
|
|
|
|
"above": 10,
|
|
|
|
},
|
|
|
|
"action": {"service": "test.automation"},
|
2018-10-26 09:31:14 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
# 11 is above 10 and 9 is below
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", 11)
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 1
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_if_not_fires_on_entity_change_above_to_above(hass, calls):
|
|
|
|
"""Test the firing with changed entity."""
|
|
|
|
# set initial state
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", 9)
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "numeric_state",
|
|
|
|
"entity_id": "test.entity",
|
|
|
|
"above": 10,
|
|
|
|
},
|
|
|
|
"action": {"service": "test.automation"},
|
2018-10-26 09:31:14 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
# 12 is above 10 so this should fire
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", 12)
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 1
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
# already above, should not fire again
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", 15)
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 1
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_if_not_above_fires_on_entity_change_to_equal(hass, calls):
|
|
|
|
"""Test the firing with changed entity."""
|
|
|
|
# set initial state
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", 9)
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "numeric_state",
|
|
|
|
"entity_id": "test.entity",
|
|
|
|
"above": 10,
|
|
|
|
},
|
|
|
|
"action": {"service": "test.automation"},
|
2018-10-26 09:31:14 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
# 10 is not above 10 so this should not fire again
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", 10)
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 0
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_if_fires_on_entity_change_below_range(hass, calls):
|
|
|
|
"""Test the firing with changed entity."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "numeric_state",
|
|
|
|
"entity_id": "test.entity",
|
|
|
|
"below": 10,
|
|
|
|
"above": 5,
|
|
|
|
},
|
|
|
|
"action": {"service": "test.automation"},
|
2018-10-26 09:31:14 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2018-10-26 09:31:14 +00:00
|
|
|
# 9 is below 10
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", 9)
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 1
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_if_fires_on_entity_change_below_above_range(hass, calls):
|
|
|
|
"""Test the firing with changed entity."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "numeric_state",
|
|
|
|
"entity_id": "test.entity",
|
|
|
|
"below": 10,
|
|
|
|
"above": 5,
|
|
|
|
},
|
|
|
|
"action": {"service": "test.automation"},
|
2018-10-26 09:31:14 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2018-10-26 09:31:14 +00:00
|
|
|
# 4 is below 5
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", 4)
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 0
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_if_fires_on_entity_change_over_to_below_range(hass, calls):
|
|
|
|
"""Test the firing with changed entity."""
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", 11)
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "numeric_state",
|
|
|
|
"entity_id": "test.entity",
|
|
|
|
"below": 10,
|
|
|
|
"above": 5,
|
|
|
|
},
|
|
|
|
"action": {"service": "test.automation"},
|
2018-10-26 09:31:14 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
# 9 is below 10
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", 9)
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 1
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def test_if_fires_on_entity_change_over_to_below_above_range(hass, calls):
|
2018-10-26 09:31:14 +00:00
|
|
|
"""Test the firing with changed entity."""
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", 11)
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "numeric_state",
|
|
|
|
"entity_id": "test.entity",
|
|
|
|
"below": 10,
|
|
|
|
"above": 5,
|
|
|
|
},
|
|
|
|
"action": {"service": "test.automation"},
|
2018-10-26 09:31:14 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
# 4 is below 5 so it should not fire
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", 4)
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 0
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_if_not_fires_if_entity_not_match(hass, calls):
|
|
|
|
"""Test if not fired with non matching entity."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "numeric_state",
|
|
|
|
"entity_id": "test.another_entity",
|
|
|
|
"below": 100,
|
|
|
|
},
|
|
|
|
"action": {"service": "test.automation"},
|
2018-10-26 09:31:14 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2018-10-26 09:31:14 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", 11)
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 0
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_if_fires_on_entity_change_below_with_attribute(hass, calls):
|
|
|
|
"""Test attributes change."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "numeric_state",
|
|
|
|
"entity_id": "test.entity",
|
|
|
|
"below": 10,
|
|
|
|
},
|
|
|
|
"action": {"service": "test.automation"},
|
2018-10-26 09:31:14 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2018-10-26 09:31:14 +00:00
|
|
|
# 9 is below 10
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", 9, {"test_attribute": 11})
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 1
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def test_if_not_fires_on_entity_change_not_below_with_attribute(hass, calls):
|
2018-10-26 09:31:14 +00:00
|
|
|
"""Test attributes."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "numeric_state",
|
|
|
|
"entity_id": "test.entity",
|
|
|
|
"below": 10,
|
|
|
|
},
|
|
|
|
"action": {"service": "test.automation"},
|
2018-10-26 09:31:14 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2018-10-26 09:31:14 +00:00
|
|
|
# 11 is not below 10
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", 11, {"test_attribute": 9})
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 0
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_if_fires_on_attribute_change_with_attribute_below(hass, calls):
|
|
|
|
"""Test attributes change."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "numeric_state",
|
|
|
|
"entity_id": "test.entity",
|
|
|
|
"value_template": "{{ state.attributes.test_attribute }}",
|
|
|
|
"below": 10,
|
|
|
|
},
|
|
|
|
"action": {"service": "test.automation"},
|
2018-10-26 09:31:14 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2018-10-26 09:31:14 +00:00
|
|
|
# 9 is below 10
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", "entity", {"test_attribute": 9})
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 1
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def test_if_not_fires_on_attribute_change_with_attribute_not_below(hass, calls):
|
2018-10-26 09:31:14 +00:00
|
|
|
"""Test attributes change."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "numeric_state",
|
|
|
|
"entity_id": "test.entity",
|
|
|
|
"value_template": "{{ state.attributes.test_attribute }}",
|
|
|
|
"below": 10,
|
|
|
|
},
|
|
|
|
"action": {"service": "test.automation"},
|
2018-10-26 09:31:14 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2018-10-26 09:31:14 +00:00
|
|
|
# 11 is not below 10
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", "entity", {"test_attribute": 11})
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 0
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_if_not_fires_on_entity_change_with_attribute_below(hass, calls):
|
|
|
|
"""Test attributes change."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "numeric_state",
|
|
|
|
"entity_id": "test.entity",
|
|
|
|
"value_template": "{{ state.attributes.test_attribute }}",
|
|
|
|
"below": 10,
|
|
|
|
},
|
|
|
|
"action": {"service": "test.automation"},
|
2018-10-26 09:31:14 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2018-10-26 09:31:14 +00:00
|
|
|
# 11 is not below 10, entity state value should not be tested
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", "9", {"test_attribute": 11})
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 0
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def test_if_not_fires_on_entity_change_with_not_attribute_below(hass, calls):
|
2018-10-26 09:31:14 +00:00
|
|
|
"""Test attributes change."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "numeric_state",
|
|
|
|
"entity_id": "test.entity",
|
|
|
|
"value_template": "{{ state.attributes.test_attribute }}",
|
|
|
|
"below": 10,
|
|
|
|
},
|
|
|
|
"action": {"service": "test.automation"},
|
2018-10-26 09:31:14 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2018-10-26 09:31:14 +00:00
|
|
|
# 11 is not below 10, entity state value should not be tested
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", "entity")
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 0
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def test_fires_on_attr_change_with_attribute_below_and_multiple_attr(hass, calls):
|
2018-10-26 09:31:14 +00:00
|
|
|
"""Test attributes change."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "numeric_state",
|
|
|
|
"entity_id": "test.entity",
|
|
|
|
"value_template": "{{ state.attributes.test_attribute }}",
|
|
|
|
"below": 10,
|
|
|
|
},
|
|
|
|
"action": {"service": "test.automation"},
|
2018-10-26 09:31:14 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2018-10-26 09:31:14 +00:00
|
|
|
# 9 is not below 10
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set(
|
|
|
|
"test.entity", "entity", {"test_attribute": 9, "not_test_attribute": 11}
|
|
|
|
)
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 1
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_template_list(hass, calls):
|
|
|
|
"""Test template list."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "numeric_state",
|
|
|
|
"entity_id": "test.entity",
|
|
|
|
"value_template": "{{ state.attributes.test_attribute[2] }}",
|
|
|
|
"below": 10,
|
|
|
|
},
|
|
|
|
"action": {"service": "test.automation"},
|
2018-10-26 09:31:14 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2018-10-26 09:31:14 +00:00
|
|
|
# 3 is below 10
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", "entity", {"test_attribute": [11, 15, 3]})
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 1
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_template_string(hass, calls):
|
|
|
|
"""Test template string."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "numeric_state",
|
|
|
|
"entity_id": "test.entity",
|
|
|
|
"value_template": "{{ state.attributes.test_attribute | multiply(10) }}",
|
|
|
|
"below": 10,
|
|
|
|
},
|
|
|
|
"action": {
|
|
|
|
"service": "test.automation",
|
|
|
|
"data_template": {
|
|
|
|
"some": "{{ trigger.%s }}"
|
|
|
|
% "}} - {{ trigger.".join(
|
|
|
|
(
|
|
|
|
"platform",
|
|
|
|
"entity_id",
|
|
|
|
"below",
|
|
|
|
"above",
|
|
|
|
"from_state.state",
|
|
|
|
"to_state.state",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
},
|
2018-10-26 09:31:14 +00:00
|
|
|
},
|
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
hass.states.async_set("test.entity", "test state 1", {"test_attribute": "1.2"})
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", "test state 2", {"test_attribute": "0.9"})
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 1
|
2019-07-31 19:25:30 +00:00
|
|
|
assert (
|
2020-05-01 14:37:25 +00:00
|
|
|
calls[0].data["some"]
|
|
|
|
== "numeric_state - test.entity - 10.0 - None - test state 1 - test state 2"
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def test_not_fires_on_attr_change_with_attr_not_below_multiple_attr(hass, calls):
|
2018-10-26 09:31:14 +00:00
|
|
|
"""Test if not fired changed attributes."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "numeric_state",
|
|
|
|
"entity_id": "test.entity",
|
|
|
|
"value_template": "{{ state.attributes.test_attribute }}",
|
|
|
|
"below": 10,
|
|
|
|
},
|
|
|
|
"action": {"service": "test.automation"},
|
2018-10-26 09:31:14 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2018-10-26 09:31:14 +00:00
|
|
|
# 11 is not below 10
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set(
|
|
|
|
"test.entity", "entity", {"test_attribute": 11, "not_test_attribute": 9}
|
|
|
|
)
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 0
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_if_action(hass, calls):
|
|
|
|
"""Test if action."""
|
2019-07-31 19:25:30 +00:00
|
|
|
entity_id = "domain.test_entity"
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {"platform": "event", "event_type": "test_event"},
|
|
|
|
"condition": {
|
|
|
|
"condition": "numeric_state",
|
|
|
|
"entity_id": entity_id,
|
|
|
|
"above": 8,
|
|
|
|
"below": 12,
|
|
|
|
},
|
|
|
|
"action": {"service": "test.automation"},
|
2018-10-26 09:31:14 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
hass.states.async_set(entity_id, 10)
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.bus.async_fire("test_event")
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 1
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
hass.states.async_set(entity_id, 8)
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.bus.async_fire("test_event")
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 1
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
hass.states.async_set(entity_id, 9)
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.bus.async_fire("test_event")
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 2
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_if_fails_setup_bad_for(hass, calls):
|
|
|
|
"""Test for setup failure for bad for."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "numeric_state",
|
|
|
|
"entity_id": "test.entity",
|
|
|
|
"above": 8,
|
|
|
|
"below": 12,
|
|
|
|
"for": {"invalid": 5},
|
2017-09-05 00:01:01 +00:00
|
|
|
},
|
2019-07-31 19:25:30 +00:00
|
|
|
"action": {"service": "homeassistant.turn_on"},
|
2019-07-08 20:58:50 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2019-07-08 20:58:50 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch.object(automation.numeric_state, "_LOGGER") as mock_logger:
|
|
|
|
hass.states.async_set("test.entity", 9)
|
2019-07-08 20:58:50 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert mock_logger.error.called
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_if_fails_setup_for_without_above_below(hass, calls):
|
|
|
|
"""Test for setup failures for missing above or below."""
|
2019-04-09 20:59:15 +00:00
|
|
|
with assert_setup_component(0, automation.DOMAIN):
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "numeric_state",
|
|
|
|
"entity_id": "test.entity",
|
|
|
|
"for": {"seconds": 5},
|
2017-09-05 00:01:01 +00:00
|
|
|
},
|
2019-07-31 19:25:30 +00:00
|
|
|
"action": {"service": "homeassistant.turn_on"},
|
2017-09-05 00:01:01 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_if_not_fires_on_entity_change_with_for(hass, calls):
|
|
|
|
"""Test for not firing on entity change with for."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "numeric_state",
|
|
|
|
"entity_id": "test.entity",
|
|
|
|
"above": 8,
|
|
|
|
"below": 12,
|
|
|
|
"for": {"seconds": 5},
|
2018-10-26 09:31:14 +00:00
|
|
|
},
|
2019-07-31 19:25:30 +00:00
|
|
|
"action": {"service": "test.automation"},
|
2018-10-26 09:31:14 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2018-10-26 09:31:14 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", 9)
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", 15)
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=10))
|
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 0
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def test_if_not_fires_on_entities_change_with_for_after_stop(hass, calls):
|
2018-10-26 09:31:14 +00:00
|
|
|
"""Test for not firing on entities change with for after stop."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "numeric_state",
|
|
|
|
"entity_id": ["test.entity_1", "test.entity_2"],
|
|
|
|
"above": 8,
|
|
|
|
"below": 12,
|
|
|
|
"for": {"seconds": 5},
|
2018-10-26 09:31:14 +00:00
|
|
|
},
|
2019-07-31 19:25:30 +00:00
|
|
|
"action": {"service": "test.automation"},
|
2018-10-26 09:31:14 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2018-10-26 09:31:14 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity_1", 9)
|
|
|
|
hass.states.async_set("test.entity_2", 9)
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=10))
|
|
|
|
await hass.async_block_till_done()
|
2020-07-15 18:38:29 +00:00
|
|
|
assert len(calls) == 1
|
2018-10-26 09:31:14 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity_1", 15)
|
|
|
|
hass.states.async_set("test.entity_2", 15)
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity_1", 9)
|
|
|
|
hass.states.async_set("test.entity_2", 9)
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
await common.async_turn_off(hass)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=10))
|
|
|
|
await hass.async_block_till_done()
|
2020-07-15 18:38:29 +00:00
|
|
|
assert len(calls) == 1
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def test_if_fires_on_entity_change_with_for_attribute_change(hass, calls):
|
2018-10-26 09:31:14 +00:00
|
|
|
"""Test for firing on entity change with for and attribute change."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "numeric_state",
|
|
|
|
"entity_id": "test.entity",
|
|
|
|
"above": 8,
|
|
|
|
"below": 12,
|
|
|
|
"for": {"seconds": 5},
|
2018-10-26 09:31:14 +00:00
|
|
|
},
|
2019-07-31 19:25:30 +00:00
|
|
|
"action": {"service": "test.automation"},
|
2018-10-26 09:31:14 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
utcnow = dt_util.utcnow()
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.core.dt_util.utcnow") as mock_utcnow:
|
2018-10-26 09:31:14 +00:00
|
|
|
mock_utcnow.return_value = utcnow
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", 9)
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
mock_utcnow.return_value += timedelta(seconds=4)
|
|
|
|
async_fire_time_changed(hass, mock_utcnow.return_value)
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", 9, attributes={"mock_attr": "attr_change"})
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 0
|
2018-10-26 09:31:14 +00:00
|
|
|
mock_utcnow.return_value += timedelta(seconds=4)
|
|
|
|
async_fire_time_changed(hass, mock_utcnow.return_value)
|
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 1
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_if_fires_on_entity_change_with_for(hass, calls):
|
|
|
|
"""Test for firing on entity change with for."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "numeric_state",
|
|
|
|
"entity_id": "test.entity",
|
|
|
|
"above": 8,
|
|
|
|
"below": 12,
|
|
|
|
"for": {"seconds": 5},
|
2018-10-26 09:31:14 +00:00
|
|
|
},
|
2019-07-31 19:25:30 +00:00
|
|
|
"action": {"service": "test.automation"},
|
2018-10-26 09:31:14 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2018-10-26 09:31:14 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", 9)
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=10))
|
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 1
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_wait_template_with_trigger(hass, calls):
|
|
|
|
"""Test using wait template with 'trigger.entity_id'."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
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")
|
|
|
|
)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", "12")
|
|
|
|
hass.states.async_set("test.entity", "8")
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 1
|
2019-07-31 19:25:30 +00:00
|
|
|
assert "numeric_state - test.entity - 12" == calls[0].data["some"]
|
2019-07-02 15:28:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_if_fires_on_entities_change_no_overlap(hass, calls):
|
|
|
|
"""Test for firing on entities change with no overlap."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "numeric_state",
|
|
|
|
"entity_id": ["test.entity_1", "test.entity_2"],
|
|
|
|
"above": 8,
|
|
|
|
"below": 12,
|
|
|
|
"for": {"seconds": 5},
|
2019-07-02 15:28:02 +00:00
|
|
|
},
|
2019-07-31 19:25:30 +00:00
|
|
|
"action": {
|
|
|
|
"service": "test.automation",
|
|
|
|
"data_template": {"some": "{{ trigger.entity_id }}"},
|
2019-07-02 15:28:02 +00:00
|
|
|
},
|
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2019-07-02 15:28:02 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
utcnow = dt_util.utcnow()
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.core.dt_util.utcnow") as mock_utcnow:
|
2019-07-02 15:28:02 +00:00
|
|
|
mock_utcnow.return_value = utcnow
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity_1", 9)
|
2019-07-02 15:28:02 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
mock_utcnow.return_value += timedelta(seconds=10)
|
|
|
|
async_fire_time_changed(hass, mock_utcnow.return_value)
|
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 1
|
|
|
|
assert calls[0].data["some"] == "test.entity_1"
|
2019-07-02 15:28:02 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity_2", 9)
|
2019-07-02 15:28:02 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
mock_utcnow.return_value += timedelta(seconds=10)
|
|
|
|
async_fire_time_changed(hass, mock_utcnow.return_value)
|
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 2
|
|
|
|
assert calls[1].data["some"] == "test.entity_2"
|
2019-07-02 15:28:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_if_fires_on_entities_change_overlap(hass, calls):
|
|
|
|
"""Test for firing on entities change with overlap."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "numeric_state",
|
|
|
|
"entity_id": ["test.entity_1", "test.entity_2"],
|
|
|
|
"above": 8,
|
|
|
|
"below": 12,
|
|
|
|
"for": {"seconds": 5},
|
2019-07-02 15:28:02 +00:00
|
|
|
},
|
2019-07-31 19:25:30 +00:00
|
|
|
"action": {
|
|
|
|
"service": "test.automation",
|
|
|
|
"data_template": {"some": "{{ trigger.entity_id }}"},
|
2019-07-02 15:28:02 +00:00
|
|
|
},
|
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2019-07-02 15:28:02 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
utcnow = dt_util.utcnow()
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.core.dt_util.utcnow") as mock_utcnow:
|
2019-07-02 15:28:02 +00:00
|
|
|
mock_utcnow.return_value = utcnow
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity_1", 9)
|
2019-07-02 15:28:02 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
mock_utcnow.return_value += timedelta(seconds=1)
|
|
|
|
async_fire_time_changed(hass, mock_utcnow.return_value)
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity_2", 9)
|
2019-07-02 15:28:02 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
mock_utcnow.return_value += timedelta(seconds=1)
|
|
|
|
async_fire_time_changed(hass, mock_utcnow.return_value)
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity_2", 15)
|
2019-07-02 15:28:02 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
mock_utcnow.return_value += timedelta(seconds=1)
|
|
|
|
async_fire_time_changed(hass, mock_utcnow.return_value)
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity_2", 9)
|
2019-07-02 15:28:02 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 0
|
2019-07-02 15:28:02 +00:00
|
|
|
mock_utcnow.return_value += timedelta(seconds=3)
|
|
|
|
async_fire_time_changed(hass, mock_utcnow.return_value)
|
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 1
|
|
|
|
assert calls[0].data["some"] == "test.entity_1"
|
2019-07-02 15:28:02 +00:00
|
|
|
|
|
|
|
mock_utcnow.return_value += timedelta(seconds=3)
|
|
|
|
async_fire_time_changed(hass, mock_utcnow.return_value)
|
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 2
|
|
|
|
assert calls[1].data["some"] == "test.entity_2"
|
2019-07-08 20:58:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_if_fires_on_change_with_for_template_1(hass, calls):
|
|
|
|
"""Test for firing on change with for template."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "numeric_state",
|
|
|
|
"entity_id": "test.entity",
|
|
|
|
"above": 8,
|
|
|
|
"below": 12,
|
|
|
|
"for": {"seconds": "{{ 5 }}"},
|
2019-07-08 20:58:50 +00:00
|
|
|
},
|
2019-07-31 19:25:30 +00:00
|
|
|
"action": {"service": "test.automation"},
|
2019-07-08 20:58:50 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2019-07-08 20:58:50 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", 9)
|
2019-07-08 20:58:50 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 0
|
2019-07-08 20:58:50 +00:00
|
|
|
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=10))
|
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 1
|
2019-07-08 20:58:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_if_fires_on_change_with_for_template_2(hass, calls):
|
|
|
|
"""Test for firing on change with for template."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "numeric_state",
|
|
|
|
"entity_id": "test.entity",
|
|
|
|
"above": 8,
|
|
|
|
"below": 12,
|
|
|
|
"for": "{{ 5 }}",
|
|
|
|
},
|
|
|
|
"action": {"service": "test.automation"},
|
2019-07-08 20:58:50 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2019-07-08 20:58:50 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", 9)
|
2019-07-08 20:58:50 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 0
|
2019-07-08 20:58:50 +00:00
|
|
|
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=10))
|
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 1
|
2019-07-08 20:58:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_if_fires_on_change_with_for_template_3(hass, calls):
|
|
|
|
"""Test for firing on change with for template."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "numeric_state",
|
|
|
|
"entity_id": "test.entity",
|
|
|
|
"above": 8,
|
|
|
|
"below": 12,
|
|
|
|
"for": "00:00:{{ 5 }}",
|
|
|
|
},
|
|
|
|
"action": {"service": "test.automation"},
|
2019-07-08 20:58:50 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2019-07-08 20:58:50 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", 9)
|
2019-07-08 20:58:50 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 0
|
2019-07-08 20:58:50 +00:00
|
|
|
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=10))
|
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 1
|
2019-07-08 20:58:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_invalid_for_template(hass, calls):
|
|
|
|
"""Test for invalid for template."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "numeric_state",
|
|
|
|
"entity_id": "test.entity",
|
|
|
|
"above": 8,
|
|
|
|
"below": 12,
|
|
|
|
"for": "{{ five }}",
|
|
|
|
},
|
|
|
|
"action": {"service": "test.automation"},
|
2019-07-08 20:58:50 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2019-07-08 20:58:50 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch.object(automation.numeric_state, "_LOGGER") as mock_logger:
|
|
|
|
hass.states.async_set("test.entity", 9)
|
2019-07-08 20:58:50 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert mock_logger.error.called
|
|
|
|
|
|
|
|
|
|
|
|
async def test_if_fires_on_entities_change_overlap_for_template(hass, calls):
|
|
|
|
"""Test for firing on entities change with overlap and for template."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "numeric_state",
|
|
|
|
"entity_id": ["test.entity_1", "test.entity_2"],
|
|
|
|
"above": 8,
|
|
|
|
"below": 12,
|
|
|
|
"for": '{{ 5 if trigger.entity_id == "test.entity_1"'
|
|
|
|
" else 10 }}",
|
|
|
|
},
|
|
|
|
"action": {
|
|
|
|
"service": "test.automation",
|
|
|
|
"data_template": {
|
|
|
|
"some": "{{ trigger.entity_id }} - {{ trigger.for }}"
|
|
|
|
},
|
2019-07-08 20:58:50 +00:00
|
|
|
},
|
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2019-07-08 20:58:50 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
utcnow = dt_util.utcnow()
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.util.dt.utcnow") as mock_utcnow:
|
2019-07-08 20:58:50 +00:00
|
|
|
mock_utcnow.return_value = utcnow
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity_1", 9)
|
2019-07-08 20:58:50 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
mock_utcnow.return_value += timedelta(seconds=1)
|
|
|
|
async_fire_time_changed(hass, mock_utcnow.return_value)
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity_2", 9)
|
2019-07-08 20:58:50 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
mock_utcnow.return_value += timedelta(seconds=1)
|
|
|
|
async_fire_time_changed(hass, mock_utcnow.return_value)
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity_2", 15)
|
2019-07-08 20:58:50 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
mock_utcnow.return_value += timedelta(seconds=1)
|
|
|
|
async_fire_time_changed(hass, mock_utcnow.return_value)
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity_2", 9)
|
2019-07-08 20:58:50 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 0
|
2019-07-08 20:58:50 +00:00
|
|
|
mock_utcnow.return_value += timedelta(seconds=3)
|
|
|
|
async_fire_time_changed(hass, mock_utcnow.return_value)
|
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 1
|
|
|
|
assert calls[0].data["some"] == "test.entity_1 - 0:00:05"
|
2019-07-08 20:58:50 +00:00
|
|
|
|
|
|
|
mock_utcnow.return_value += timedelta(seconds=3)
|
|
|
|
async_fire_time_changed(hass, mock_utcnow.return_value)
|
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 1
|
2019-07-08 20:58:50 +00:00
|
|
|
mock_utcnow.return_value += timedelta(seconds=5)
|
|
|
|
async_fire_time_changed(hass, mock_utcnow.return_value)
|
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 2
|
|
|
|
assert calls[1].data["some"] == "test.entity_2 - 0:00:10"
|
2020-03-04 01:26:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_below_above():
|
|
|
|
"""Test above cannot be above below."""
|
|
|
|
with pytest.raises(vol.Invalid):
|
|
|
|
numeric_state.TRIGGER_SCHEMA(
|
|
|
|
{"platform": "numeric_state", "above": 1200, "below": 1000}
|
|
|
|
)
|