2016-04-28 10:03:57 +00:00
|
|
|
"""Test the condition helper."""
|
2021-02-11 09:30:09 +00:00
|
|
|
from logging import WARNING
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import patch
|
2020-08-16 00:53:03 +00:00
|
|
|
|
2020-04-30 22:15:53 +00:00
|
|
|
import pytest
|
|
|
|
|
2021-02-08 09:47:57 +00:00
|
|
|
from homeassistant.exceptions import ConditionError, HomeAssistantError
|
2016-04-28 10:03:57 +00:00
|
|
|
from homeassistant.helpers import condition
|
2020-09-13 20:05:45 +00:00
|
|
|
from homeassistant.helpers.template import Template
|
2020-09-06 14:06:09 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
2016-05-29 21:32:32 +00:00
|
|
|
from homeassistant.util import dt
|
2016-04-28 10:03:57 +00:00
|
|
|
|
2019-09-05 14:49:32 +00:00
|
|
|
|
2020-04-30 22:15:53 +00:00
|
|
|
async def test_invalid_condition(hass):
|
|
|
|
"""Test if invalid condition raises."""
|
|
|
|
with pytest.raises(HomeAssistantError):
|
|
|
|
await condition.async_from_config(
|
|
|
|
hass,
|
|
|
|
{
|
|
|
|
"condition": "invalid",
|
|
|
|
"conditions": [
|
|
|
|
{
|
|
|
|
"condition": "state",
|
|
|
|
"entity_id": "sensor.temperature",
|
|
|
|
"state": "100",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-09-05 14:49:32 +00:00
|
|
|
async def test_and_condition(hass):
|
|
|
|
"""Test the 'and' condition."""
|
|
|
|
test = await condition.async_from_config(
|
|
|
|
hass,
|
|
|
|
{
|
2021-02-21 03:21:09 +00:00
|
|
|
"alias": "And Condition",
|
2019-09-05 14:49:32 +00:00
|
|
|
"condition": "and",
|
|
|
|
"conditions": [
|
|
|
|
{
|
|
|
|
"condition": "state",
|
|
|
|
"entity_id": "sensor.temperature",
|
|
|
|
"state": "100",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"condition": "numeric_state",
|
|
|
|
"entity_id": "sensor.temperature",
|
|
|
|
"below": 110,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2021-02-19 12:15:30 +00:00
|
|
|
with pytest.raises(ConditionError):
|
|
|
|
test(hass)
|
|
|
|
|
2019-09-05 14:49:32 +00:00
|
|
|
hass.states.async_set("sensor.temperature", 120)
|
|
|
|
assert not test(hass)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature", 105)
|
|
|
|
assert not test(hass)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature", 100)
|
|
|
|
assert test(hass)
|
|
|
|
|
|
|
|
|
|
|
|
async def test_and_condition_with_template(hass):
|
|
|
|
"""Test the 'and' condition."""
|
|
|
|
test = await condition.async_from_config(
|
|
|
|
hass,
|
|
|
|
{
|
|
|
|
"condition": "and",
|
|
|
|
"conditions": [
|
|
|
|
{
|
2021-02-21 03:21:09 +00:00
|
|
|
"alias": "Template Condition",
|
2019-09-05 14:49:32 +00:00
|
|
|
"condition": "template",
|
|
|
|
"value_template": '{{ states.sensor.temperature.state == "100" }}',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"condition": "numeric_state",
|
|
|
|
"entity_id": "sensor.temperature",
|
|
|
|
"below": 110,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature", 120)
|
|
|
|
assert not test(hass)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature", 105)
|
|
|
|
assert not test(hass)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature", 100)
|
|
|
|
assert test(hass)
|
|
|
|
|
|
|
|
|
|
|
|
async def test_or_condition(hass):
|
|
|
|
"""Test the 'or' condition."""
|
|
|
|
test = await condition.async_from_config(
|
|
|
|
hass,
|
|
|
|
{
|
2021-02-21 03:21:09 +00:00
|
|
|
"alias": "Or Condition",
|
2019-09-05 14:49:32 +00:00
|
|
|
"condition": "or",
|
|
|
|
"conditions": [
|
|
|
|
{
|
|
|
|
"condition": "state",
|
|
|
|
"entity_id": "sensor.temperature",
|
|
|
|
"state": "100",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"condition": "numeric_state",
|
|
|
|
"entity_id": "sensor.temperature",
|
|
|
|
"below": 110,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2021-02-19 12:15:30 +00:00
|
|
|
with pytest.raises(ConditionError):
|
|
|
|
test(hass)
|
|
|
|
|
2019-09-05 14:49:32 +00:00
|
|
|
hass.states.async_set("sensor.temperature", 120)
|
|
|
|
assert not test(hass)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature", 105)
|
|
|
|
assert test(hass)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature", 100)
|
|
|
|
assert test(hass)
|
|
|
|
|
|
|
|
|
|
|
|
async def test_or_condition_with_template(hass):
|
|
|
|
"""Test the 'or' condition."""
|
|
|
|
test = await condition.async_from_config(
|
|
|
|
hass,
|
|
|
|
{
|
|
|
|
"condition": "or",
|
|
|
|
"conditions": [
|
2020-09-06 14:55:06 +00:00
|
|
|
{'{{ states.sensor.temperature.state == "100" }}'},
|
2019-09-05 14:49:32 +00:00
|
|
|
{
|
|
|
|
"condition": "numeric_state",
|
|
|
|
"entity_id": "sensor.temperature",
|
|
|
|
"below": 110,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature", 120)
|
|
|
|
assert not test(hass)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature", 105)
|
|
|
|
assert test(hass)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature", 100)
|
|
|
|
assert test(hass)
|
|
|
|
|
|
|
|
|
2020-04-24 16:40:23 +00:00
|
|
|
async def test_not_condition(hass):
|
|
|
|
"""Test the 'not' condition."""
|
|
|
|
test = await condition.async_from_config(
|
|
|
|
hass,
|
|
|
|
{
|
2021-02-21 03:21:09 +00:00
|
|
|
"alias": "Not Condition",
|
2020-04-24 16:40:23 +00:00
|
|
|
"condition": "not",
|
|
|
|
"conditions": [
|
|
|
|
{
|
|
|
|
"condition": "state",
|
|
|
|
"entity_id": "sensor.temperature",
|
|
|
|
"state": "100",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"condition": "numeric_state",
|
|
|
|
"entity_id": "sensor.temperature",
|
|
|
|
"below": 50,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2021-02-19 12:15:30 +00:00
|
|
|
with pytest.raises(ConditionError):
|
|
|
|
test(hass)
|
|
|
|
|
2020-04-24 16:40:23 +00:00
|
|
|
hass.states.async_set("sensor.temperature", 101)
|
|
|
|
assert test(hass)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature", 50)
|
|
|
|
assert test(hass)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature", 49)
|
|
|
|
assert not test(hass)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature", 100)
|
|
|
|
assert not test(hass)
|
|
|
|
|
|
|
|
|
|
|
|
async def test_not_condition_with_template(hass):
|
|
|
|
"""Test the 'or' condition."""
|
|
|
|
test = await condition.async_from_config(
|
|
|
|
hass,
|
|
|
|
{
|
|
|
|
"condition": "not",
|
|
|
|
"conditions": [
|
|
|
|
{
|
|
|
|
"condition": "template",
|
|
|
|
"value_template": '{{ states.sensor.temperature.state == "100" }}',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"condition": "numeric_state",
|
|
|
|
"entity_id": "sensor.temperature",
|
|
|
|
"below": 50,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature", 101)
|
|
|
|
assert test(hass)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature", 50)
|
|
|
|
assert test(hass)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature", 49)
|
|
|
|
assert not test(hass)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature", 100)
|
|
|
|
assert not test(hass)
|
|
|
|
|
|
|
|
|
2019-09-05 14:49:32 +00:00
|
|
|
async def test_time_window(hass):
|
|
|
|
"""Test time condition windows."""
|
2021-02-21 03:21:09 +00:00
|
|
|
sixam = "06:00:00"
|
|
|
|
sixpm = "18:00:00"
|
|
|
|
|
|
|
|
test1 = await condition.async_from_config(
|
|
|
|
hass,
|
|
|
|
{"alias": "Time Cond", "condition": "time", "after": sixam, "before": sixpm},
|
|
|
|
)
|
|
|
|
test2 = await condition.async_from_config(
|
|
|
|
hass,
|
|
|
|
{"alias": "Time Cond", "condition": "time", "after": sixpm, "before": sixam},
|
|
|
|
)
|
2019-09-05 14:49:32 +00:00
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.helpers.condition.dt_util.now",
|
|
|
|
return_value=dt.now().replace(hour=3),
|
|
|
|
):
|
2021-02-21 03:21:09 +00:00
|
|
|
assert not test1(hass)
|
|
|
|
assert test2(hass)
|
2019-09-05 14:49:32 +00:00
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.helpers.condition.dt_util.now",
|
|
|
|
return_value=dt.now().replace(hour=9),
|
|
|
|
):
|
2021-02-21 03:21:09 +00:00
|
|
|
assert test1(hass)
|
|
|
|
assert not test2(hass)
|
2019-09-05 14:49:32 +00:00
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.helpers.condition.dt_util.now",
|
|
|
|
return_value=dt.now().replace(hour=15),
|
|
|
|
):
|
2021-02-21 03:21:09 +00:00
|
|
|
assert test1(hass)
|
|
|
|
assert not test2(hass)
|
2019-09-05 14:49:32 +00:00
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.helpers.condition.dt_util.now",
|
|
|
|
return_value=dt.now().replace(hour=21),
|
|
|
|
):
|
2021-02-21 03:21:09 +00:00
|
|
|
assert not test1(hass)
|
|
|
|
assert test2(hass)
|
2020-09-06 14:06:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_time_using_input_datetime(hass):
|
|
|
|
"""Test time conditions using input_datetime entities."""
|
|
|
|
await async_setup_component(
|
|
|
|
hass,
|
|
|
|
"input_datetime",
|
|
|
|
{
|
|
|
|
"input_datetime": {
|
|
|
|
"am": {"has_date": True, "has_time": True},
|
|
|
|
"pm": {"has_date": True, "has_time": True},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
await hass.services.async_call(
|
|
|
|
"input_datetime",
|
|
|
|
"set_datetime",
|
|
|
|
{
|
|
|
|
"entity_id": "input_datetime.am",
|
|
|
|
"datetime": str(
|
|
|
|
dt.now()
|
|
|
|
.replace(hour=6, minute=0, second=0, microsecond=0)
|
|
|
|
.replace(tzinfo=None)
|
|
|
|
),
|
|
|
|
},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
await hass.services.async_call(
|
|
|
|
"input_datetime",
|
|
|
|
"set_datetime",
|
|
|
|
{
|
|
|
|
"entity_id": "input_datetime.pm",
|
|
|
|
"datetime": str(
|
|
|
|
dt.now()
|
|
|
|
.replace(hour=18, minute=0, second=0, microsecond=0)
|
|
|
|
.replace(tzinfo=None)
|
|
|
|
),
|
|
|
|
},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.helpers.condition.dt_util.now",
|
|
|
|
return_value=dt.now().replace(hour=3),
|
|
|
|
):
|
|
|
|
assert not condition.time(
|
|
|
|
hass, after="input_datetime.am", before="input_datetime.pm"
|
|
|
|
)
|
|
|
|
assert condition.time(
|
|
|
|
hass, after="input_datetime.pm", before="input_datetime.am"
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.helpers.condition.dt_util.now",
|
|
|
|
return_value=dt.now().replace(hour=9),
|
|
|
|
):
|
|
|
|
assert condition.time(
|
|
|
|
hass, after="input_datetime.am", before="input_datetime.pm"
|
|
|
|
)
|
|
|
|
assert not condition.time(
|
|
|
|
hass, after="input_datetime.pm", before="input_datetime.am"
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.helpers.condition.dt_util.now",
|
|
|
|
return_value=dt.now().replace(hour=15),
|
|
|
|
):
|
|
|
|
assert condition.time(
|
|
|
|
hass, after="input_datetime.am", before="input_datetime.pm"
|
|
|
|
)
|
|
|
|
assert not condition.time(
|
|
|
|
hass, after="input_datetime.pm", before="input_datetime.am"
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.helpers.condition.dt_util.now",
|
|
|
|
return_value=dt.now().replace(hour=21),
|
|
|
|
):
|
|
|
|
assert not condition.time(
|
|
|
|
hass, after="input_datetime.am", before="input_datetime.pm"
|
|
|
|
)
|
|
|
|
assert condition.time(
|
|
|
|
hass, after="input_datetime.pm", before="input_datetime.am"
|
|
|
|
)
|
|
|
|
|
2021-02-11 16:29:17 +00:00
|
|
|
with pytest.raises(ConditionError):
|
|
|
|
condition.time(hass, after="input_datetime.not_existing")
|
|
|
|
|
|
|
|
with pytest.raises(ConditionError):
|
|
|
|
condition.time(hass, before="input_datetime.not_existing")
|
2019-09-05 14:49:32 +00:00
|
|
|
|
|
|
|
|
2021-02-08 13:33:57 +00:00
|
|
|
async def test_if_numeric_state_raises_on_unavailable(hass, caplog):
|
2021-02-08 09:47:57 +00:00
|
|
|
"""Test numeric_state raises on unavailable/unknown state."""
|
2019-09-05 14:49:32 +00:00
|
|
|
test = await condition.async_from_config(
|
|
|
|
hass,
|
|
|
|
{"condition": "numeric_state", "entity_id": "sensor.temperature", "below": 42},
|
|
|
|
)
|
|
|
|
|
2021-02-08 13:33:57 +00:00
|
|
|
caplog.clear()
|
|
|
|
caplog.set_level(WARNING)
|
2019-09-05 14:49:32 +00:00
|
|
|
|
2021-02-08 13:33:57 +00:00
|
|
|
hass.states.async_set("sensor.temperature", "unavailable")
|
|
|
|
with pytest.raises(ConditionError):
|
|
|
|
test(hass)
|
|
|
|
assert len(caplog.record_tuples) == 0
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature", "unknown")
|
|
|
|
with pytest.raises(ConditionError):
|
|
|
|
test(hass)
|
|
|
|
assert len(caplog.record_tuples) == 0
|
2020-01-30 00:19:13 +00:00
|
|
|
|
|
|
|
|
2021-02-09 08:46:36 +00:00
|
|
|
async def test_state_raises(hass):
|
|
|
|
"""Test that state raises ConditionError on errors."""
|
|
|
|
# Unknown entity_id
|
|
|
|
with pytest.raises(ConditionError, match="Unknown entity"):
|
|
|
|
test = await condition.async_from_config(
|
|
|
|
hass,
|
|
|
|
{
|
|
|
|
"condition": "state",
|
|
|
|
"entity_id": "sensor.door_unknown",
|
|
|
|
"state": "open",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
test(hass)
|
|
|
|
|
|
|
|
# Unknown attribute
|
|
|
|
with pytest.raises(ConditionError, match=r"Attribute .* does not exist"):
|
|
|
|
test = await condition.async_from_config(
|
|
|
|
hass,
|
|
|
|
{
|
|
|
|
"condition": "state",
|
|
|
|
"entity_id": "sensor.door",
|
|
|
|
"attribute": "model",
|
|
|
|
"state": "acme",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.door", "open")
|
|
|
|
test(hass)
|
|
|
|
|
|
|
|
|
2020-06-15 20:54:19 +00:00
|
|
|
async def test_state_multiple_entities(hass):
|
|
|
|
"""Test with multiple entities in condition."""
|
|
|
|
test = await condition.async_from_config(
|
|
|
|
hass,
|
|
|
|
{
|
|
|
|
"condition": "and",
|
|
|
|
"conditions": [
|
|
|
|
{
|
|
|
|
"condition": "state",
|
|
|
|
"entity_id": ["sensor.temperature_1", "sensor.temperature_2"],
|
|
|
|
"state": "100",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature_1", 100)
|
|
|
|
hass.states.async_set("sensor.temperature_2", 100)
|
|
|
|
assert test(hass)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature_1", 101)
|
|
|
|
hass.states.async_set("sensor.temperature_2", 100)
|
|
|
|
assert not test(hass)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature_1", 100)
|
|
|
|
hass.states.async_set("sensor.temperature_2", 101)
|
|
|
|
assert not test(hass)
|
|
|
|
|
|
|
|
|
2020-06-15 22:53:13 +00:00
|
|
|
async def test_multiple_states(hass):
|
|
|
|
"""Test with multiple states in condition."""
|
|
|
|
test = await condition.async_from_config(
|
|
|
|
hass,
|
|
|
|
{
|
|
|
|
"condition": "and",
|
|
|
|
"conditions": [
|
|
|
|
{
|
2021-02-21 03:21:09 +00:00
|
|
|
"alias": "State Condition",
|
2020-06-15 22:53:13 +00:00
|
|
|
"condition": "state",
|
|
|
|
"entity_id": "sensor.temperature",
|
|
|
|
"state": ["100", "200"],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature", 100)
|
|
|
|
assert test(hass)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature", 200)
|
|
|
|
assert test(hass)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature", 42)
|
|
|
|
assert not test(hass)
|
|
|
|
|
|
|
|
|
2020-08-19 18:01:27 +00:00
|
|
|
async def test_state_attribute(hass):
|
|
|
|
"""Test with state attribute in condition."""
|
|
|
|
test = await condition.async_from_config(
|
|
|
|
hass,
|
|
|
|
{
|
|
|
|
"condition": "and",
|
|
|
|
"conditions": [
|
|
|
|
{
|
|
|
|
"condition": "state",
|
|
|
|
"entity_id": "sensor.temperature",
|
|
|
|
"attribute": "attribute1",
|
2020-10-05 10:53:12 +00:00
|
|
|
"state": 200,
|
2020-08-19 18:01:27 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature", 100, {"unkown_attr": 200})
|
2021-02-19 12:15:30 +00:00
|
|
|
with pytest.raises(ConditionError):
|
|
|
|
test(hass)
|
2020-08-19 18:01:27 +00:00
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature", 100, {"attribute1": 200})
|
|
|
|
assert test(hass)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature", 100, {"attribute1": "200"})
|
2020-10-05 10:53:12 +00:00
|
|
|
assert not test(hass)
|
2020-08-19 18:01:27 +00:00
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature", 100, {"attribute1": 201})
|
|
|
|
assert not test(hass)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature", 100, {"attribute1": None})
|
|
|
|
assert not test(hass)
|
|
|
|
|
|
|
|
|
2020-10-05 10:53:12 +00:00
|
|
|
async def test_state_attribute_boolean(hass):
|
|
|
|
"""Test with boolean state attribute in condition."""
|
|
|
|
test = await condition.async_from_config(
|
|
|
|
hass,
|
|
|
|
{
|
|
|
|
"condition": "state",
|
|
|
|
"entity_id": "sensor.temperature",
|
|
|
|
"attribute": "happening",
|
|
|
|
"state": False,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature", 100, {"happening": 200})
|
|
|
|
assert not test(hass)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature", 100, {"happening": True})
|
|
|
|
assert not test(hass)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature", 100, {"no_happening": 201})
|
2021-02-09 08:46:36 +00:00
|
|
|
with pytest.raises(ConditionError):
|
|
|
|
test(hass)
|
2020-10-05 10:53:12 +00:00
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature", 100, {"happening": False})
|
|
|
|
assert test(hass)
|
|
|
|
|
|
|
|
|
2020-09-06 22:36:01 +00:00
|
|
|
async def test_state_using_input_entities(hass):
|
|
|
|
"""Test state conditions using input_* entities."""
|
|
|
|
await async_setup_component(
|
|
|
|
hass,
|
|
|
|
"input_text",
|
|
|
|
{
|
|
|
|
"input_text": {
|
|
|
|
"hello": {"initial": "goodbye"},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
await async_setup_component(
|
|
|
|
hass,
|
|
|
|
"input_select",
|
|
|
|
{
|
|
|
|
"input_select": {
|
|
|
|
"hello": {"options": ["cya", "goodbye", "welcome"], "initial": "cya"},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
test = await condition.async_from_config(
|
|
|
|
hass,
|
|
|
|
{
|
|
|
|
"condition": "and",
|
|
|
|
"conditions": [
|
|
|
|
{
|
|
|
|
"condition": "state",
|
|
|
|
"entity_id": "sensor.salut",
|
|
|
|
"state": [
|
|
|
|
"input_text.hello",
|
|
|
|
"input_select.hello",
|
|
|
|
"input_number.not_exist",
|
|
|
|
"salut",
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.salut", "goodbye")
|
|
|
|
assert test(hass)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.salut", "salut")
|
|
|
|
assert test(hass)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.salut", "hello")
|
|
|
|
assert not test(hass)
|
|
|
|
|
|
|
|
await hass.services.async_call(
|
|
|
|
"input_text",
|
|
|
|
"set_value",
|
|
|
|
{
|
|
|
|
"entity_id": "input_text.hello",
|
|
|
|
"value": "hi",
|
|
|
|
},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
assert not test(hass)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.salut", "hi")
|
|
|
|
assert test(hass)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.salut", "cya")
|
|
|
|
assert test(hass)
|
|
|
|
|
|
|
|
await hass.services.async_call(
|
|
|
|
"input_select",
|
|
|
|
"select_option",
|
|
|
|
{
|
|
|
|
"entity_id": "input_select.hello",
|
|
|
|
"option": "welcome",
|
|
|
|
},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
assert not test(hass)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.salut", "welcome")
|
|
|
|
assert test(hass)
|
|
|
|
|
|
|
|
|
2021-02-08 09:47:57 +00:00
|
|
|
async def test_numeric_state_raises(hass):
|
|
|
|
"""Test that numeric_state raises ConditionError on errors."""
|
|
|
|
# Unknown entity_id
|
|
|
|
with pytest.raises(ConditionError, match="Unknown entity"):
|
|
|
|
test = await condition.async_from_config(
|
|
|
|
hass,
|
|
|
|
{
|
|
|
|
"condition": "numeric_state",
|
|
|
|
"entity_id": "sensor.temperature_unknown",
|
|
|
|
"above": 0,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2021-02-09 08:46:36 +00:00
|
|
|
test(hass)
|
2021-02-08 09:47:57 +00:00
|
|
|
|
|
|
|
# Unknown attribute
|
|
|
|
with pytest.raises(ConditionError, match=r"Attribute .* does not exist"):
|
|
|
|
test = await condition.async_from_config(
|
|
|
|
hass,
|
|
|
|
{
|
|
|
|
"condition": "numeric_state",
|
|
|
|
"entity_id": "sensor.temperature",
|
|
|
|
"attribute": "temperature",
|
|
|
|
"above": 0,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature", 50)
|
|
|
|
test(hass)
|
|
|
|
|
|
|
|
# Template error
|
|
|
|
with pytest.raises(ConditionError, match="ZeroDivisionError"):
|
|
|
|
test = await condition.async_from_config(
|
|
|
|
hass,
|
|
|
|
{
|
|
|
|
"condition": "numeric_state",
|
|
|
|
"entity_id": "sensor.temperature",
|
|
|
|
"value_template": "{{ 1 / 0 }}",
|
|
|
|
"above": 0,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature", 50)
|
|
|
|
test(hass)
|
|
|
|
|
|
|
|
# Unavailable state
|
|
|
|
with pytest.raises(ConditionError, match="State is not available"):
|
|
|
|
test = await condition.async_from_config(
|
|
|
|
hass,
|
|
|
|
{
|
|
|
|
"condition": "numeric_state",
|
|
|
|
"entity_id": "sensor.temperature",
|
|
|
|
"above": 0,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature", "unavailable")
|
|
|
|
test(hass)
|
|
|
|
|
|
|
|
# Bad number
|
|
|
|
with pytest.raises(ConditionError, match="cannot be processed as a number"):
|
|
|
|
test = await condition.async_from_config(
|
|
|
|
hass,
|
|
|
|
{
|
|
|
|
"condition": "numeric_state",
|
|
|
|
"entity_id": "sensor.temperature",
|
|
|
|
"above": 0,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature", "fifty")
|
|
|
|
test(hass)
|
|
|
|
|
|
|
|
# Below entity missing
|
|
|
|
with pytest.raises(ConditionError, match="below entity"):
|
|
|
|
test = await condition.async_from_config(
|
|
|
|
hass,
|
|
|
|
{
|
|
|
|
"condition": "numeric_state",
|
|
|
|
"entity_id": "sensor.temperature",
|
|
|
|
"below": "input_number.missing",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature", 50)
|
|
|
|
test(hass)
|
|
|
|
|
|
|
|
# Above entity missing
|
|
|
|
with pytest.raises(ConditionError, match="above entity"):
|
|
|
|
test = await condition.async_from_config(
|
|
|
|
hass,
|
|
|
|
{
|
|
|
|
"condition": "numeric_state",
|
|
|
|
"entity_id": "sensor.temperature",
|
|
|
|
"above": "input_number.missing",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature", 50)
|
|
|
|
test(hass)
|
|
|
|
|
|
|
|
|
2020-06-15 20:54:19 +00:00
|
|
|
async def test_numeric_state_multiple_entities(hass):
|
|
|
|
"""Test with multiple entities in condition."""
|
|
|
|
test = await condition.async_from_config(
|
|
|
|
hass,
|
|
|
|
{
|
|
|
|
"condition": "and",
|
|
|
|
"conditions": [
|
|
|
|
{
|
2021-02-21 03:21:09 +00:00
|
|
|
"alias": "Numeric State Condition",
|
2020-06-15 20:54:19 +00:00
|
|
|
"condition": "numeric_state",
|
|
|
|
"entity_id": ["sensor.temperature_1", "sensor.temperature_2"],
|
|
|
|
"below": 50,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature_1", 49)
|
|
|
|
hass.states.async_set("sensor.temperature_2", 49)
|
|
|
|
assert test(hass)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature_1", 50)
|
|
|
|
hass.states.async_set("sensor.temperature_2", 49)
|
|
|
|
assert not test(hass)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature_1", 49)
|
|
|
|
hass.states.async_set("sensor.temperature_2", 50)
|
|
|
|
assert not test(hass)
|
|
|
|
|
|
|
|
|
2021-02-19 12:15:30 +00:00
|
|
|
async def test_numeric_state_attribute(hass):
|
2020-08-19 18:01:27 +00:00
|
|
|
"""Test with numeric state attribute in condition."""
|
|
|
|
test = await condition.async_from_config(
|
|
|
|
hass,
|
|
|
|
{
|
|
|
|
"condition": "and",
|
|
|
|
"conditions": [
|
|
|
|
{
|
|
|
|
"condition": "numeric_state",
|
|
|
|
"entity_id": "sensor.temperature",
|
|
|
|
"attribute": "attribute1",
|
|
|
|
"below": 50,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature", 100, {"unkown_attr": 10})
|
2021-02-19 12:15:30 +00:00
|
|
|
with pytest.raises(ConditionError):
|
|
|
|
assert test(hass)
|
2020-08-19 18:01:27 +00:00
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature", 100, {"attribute1": 49})
|
|
|
|
assert test(hass)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature", 100, {"attribute1": "49"})
|
|
|
|
assert test(hass)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature", 100, {"attribute1": 51})
|
|
|
|
assert not test(hass)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature", 100, {"attribute1": None})
|
2021-02-19 12:15:30 +00:00
|
|
|
with pytest.raises(ConditionError):
|
|
|
|
assert test(hass)
|
2020-08-19 18:01:27 +00:00
|
|
|
|
|
|
|
|
2020-09-06 18:04:07 +00:00
|
|
|
async def test_numeric_state_using_input_number(hass):
|
|
|
|
"""Test numeric_state conditions using input_number entities."""
|
|
|
|
await async_setup_component(
|
|
|
|
hass,
|
|
|
|
"input_number",
|
|
|
|
{
|
|
|
|
"input_number": {
|
|
|
|
"low": {"min": 0, "max": 255, "initial": 10},
|
|
|
|
"high": {"min": 0, "max": 255, "initial": 100},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
test = await condition.async_from_config(
|
|
|
|
hass,
|
|
|
|
{
|
|
|
|
"condition": "and",
|
|
|
|
"conditions": [
|
|
|
|
{
|
|
|
|
"condition": "numeric_state",
|
|
|
|
"entity_id": "sensor.temperature",
|
|
|
|
"below": "input_number.high",
|
|
|
|
"above": "input_number.low",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature", 42)
|
|
|
|
assert test(hass)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature", 10)
|
|
|
|
assert not test(hass)
|
|
|
|
|
|
|
|
hass.states.async_set("sensor.temperature", 100)
|
|
|
|
assert not test(hass)
|
|
|
|
|
|
|
|
await hass.services.async_call(
|
|
|
|
"input_number",
|
|
|
|
"set_value",
|
|
|
|
{
|
|
|
|
"entity_id": "input_number.high",
|
|
|
|
"value": 101,
|
|
|
|
},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
assert test(hass)
|
|
|
|
|
2021-02-08 09:47:57 +00:00
|
|
|
with pytest.raises(ConditionError):
|
|
|
|
condition.async_numeric_state(
|
|
|
|
hass, entity="sensor.temperature", below="input_number.not_exist"
|
|
|
|
)
|
|
|
|
with pytest.raises(ConditionError):
|
|
|
|
condition.async_numeric_state(
|
|
|
|
hass, entity="sensor.temperature", above="input_number.not_exist"
|
|
|
|
)
|
2020-09-06 18:04:07 +00:00
|
|
|
|
|
|
|
|
2021-02-19 12:14:47 +00:00
|
|
|
async def test_zone_raises(hass):
|
|
|
|
"""Test that zone raises ConditionError on errors."""
|
|
|
|
test = await condition.async_from_config(
|
|
|
|
hass,
|
|
|
|
{
|
|
|
|
"condition": "zone",
|
|
|
|
"entity_id": "device_tracker.cat",
|
|
|
|
"zone": "zone.home",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
with pytest.raises(ConditionError, match="Unknown zone"):
|
|
|
|
test(hass)
|
|
|
|
|
|
|
|
hass.states.async_set(
|
|
|
|
"zone.home",
|
|
|
|
"zoning",
|
|
|
|
{"name": "home", "latitude": 2.1, "longitude": 1.1, "radius": 10},
|
|
|
|
)
|
|
|
|
|
|
|
|
with pytest.raises(ConditionError, match="Unknown entity"):
|
|
|
|
test(hass)
|
|
|
|
|
|
|
|
hass.states.async_set(
|
|
|
|
"device_tracker.cat",
|
|
|
|
"home",
|
|
|
|
{"friendly_name": "cat"},
|
|
|
|
)
|
|
|
|
|
|
|
|
with pytest.raises(ConditionError, match="latitude"):
|
|
|
|
test(hass)
|
|
|
|
|
|
|
|
hass.states.async_set(
|
|
|
|
"device_tracker.cat",
|
|
|
|
"home",
|
|
|
|
{"friendly_name": "cat", "latitude": 2.1},
|
|
|
|
)
|
|
|
|
|
|
|
|
with pytest.raises(ConditionError, match="longitude"):
|
|
|
|
test(hass)
|
|
|
|
|
|
|
|
hass.states.async_set(
|
|
|
|
"device_tracker.cat",
|
|
|
|
"home",
|
|
|
|
{"friendly_name": "cat", "latitude": 2.1, "longitude": 1.1},
|
|
|
|
)
|
|
|
|
|
|
|
|
# All okay, now test multiple failed conditions
|
|
|
|
assert test(hass)
|
|
|
|
|
|
|
|
test = await condition.async_from_config(
|
|
|
|
hass,
|
|
|
|
{
|
|
|
|
"condition": "zone",
|
|
|
|
"entity_id": ["device_tracker.cat", "device_tracker.dog"],
|
|
|
|
"zone": ["zone.home", "zone.work"],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
with pytest.raises(ConditionError, match="dog"):
|
|
|
|
test(hass)
|
|
|
|
|
|
|
|
with pytest.raises(ConditionError, match="work"):
|
|
|
|
test(hass)
|
|
|
|
|
|
|
|
hass.states.async_set(
|
|
|
|
"zone.work",
|
|
|
|
"zoning",
|
|
|
|
{"name": "work", "latitude": 20, "longitude": 10, "radius": 25000},
|
|
|
|
)
|
|
|
|
|
|
|
|
hass.states.async_set(
|
|
|
|
"device_tracker.dog",
|
|
|
|
"work",
|
|
|
|
{"friendly_name": "dog", "latitude": 20.1, "longitude": 10.1},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert test(hass)
|
|
|
|
|
|
|
|
|
2020-06-15 20:54:19 +00:00
|
|
|
async def test_zone_multiple_entities(hass):
|
|
|
|
"""Test with multiple entities in condition."""
|
|
|
|
test = await condition.async_from_config(
|
|
|
|
hass,
|
|
|
|
{
|
|
|
|
"condition": "and",
|
|
|
|
"conditions": [
|
|
|
|
{
|
2021-02-21 03:21:09 +00:00
|
|
|
"alias": "Zone Condition",
|
2020-06-15 20:54:19 +00:00
|
|
|
"condition": "zone",
|
|
|
|
"entity_id": ["device_tracker.person_1", "device_tracker.person_2"],
|
|
|
|
"zone": "zone.home",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
hass.states.async_set(
|
|
|
|
"zone.home",
|
|
|
|
"zoning",
|
|
|
|
{"name": "home", "latitude": 2.1, "longitude": 1.1, "radius": 10},
|
|
|
|
)
|
|
|
|
|
|
|
|
hass.states.async_set(
|
|
|
|
"device_tracker.person_1",
|
|
|
|
"home",
|
|
|
|
{"friendly_name": "person_1", "latitude": 2.1, "longitude": 1.1},
|
|
|
|
)
|
|
|
|
hass.states.async_set(
|
|
|
|
"device_tracker.person_2",
|
|
|
|
"home",
|
|
|
|
{"friendly_name": "person_2", "latitude": 2.1, "longitude": 1.1},
|
|
|
|
)
|
|
|
|
assert test(hass)
|
|
|
|
|
|
|
|
hass.states.async_set(
|
|
|
|
"device_tracker.person_1",
|
|
|
|
"home",
|
|
|
|
{"friendly_name": "person_1", "latitude": 20.1, "longitude": 10.1},
|
|
|
|
)
|
|
|
|
hass.states.async_set(
|
|
|
|
"device_tracker.person_2",
|
|
|
|
"home",
|
|
|
|
{"friendly_name": "person_2", "latitude": 2.1, "longitude": 1.1},
|
|
|
|
)
|
|
|
|
assert not test(hass)
|
|
|
|
|
|
|
|
hass.states.async_set(
|
|
|
|
"device_tracker.person_1",
|
|
|
|
"home",
|
|
|
|
{"friendly_name": "person_1", "latitude": 2.1, "longitude": 1.1},
|
|
|
|
)
|
|
|
|
hass.states.async_set(
|
|
|
|
"device_tracker.person_2",
|
|
|
|
"home",
|
|
|
|
{"friendly_name": "person_2", "latitude": 20.1, "longitude": 10.1},
|
|
|
|
)
|
|
|
|
assert not test(hass)
|
|
|
|
|
|
|
|
|
2020-06-15 22:53:13 +00:00
|
|
|
async def test_multiple_zones(hass):
|
|
|
|
"""Test with multiple entities in condition."""
|
|
|
|
test = await condition.async_from_config(
|
|
|
|
hass,
|
|
|
|
{
|
|
|
|
"condition": "and",
|
|
|
|
"conditions": [
|
|
|
|
{
|
|
|
|
"condition": "zone",
|
|
|
|
"entity_id": "device_tracker.person",
|
|
|
|
"zone": ["zone.home", "zone.work"],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
hass.states.async_set(
|
|
|
|
"zone.home",
|
|
|
|
"zoning",
|
|
|
|
{"name": "home", "latitude": 2.1, "longitude": 1.1, "radius": 10},
|
|
|
|
)
|
|
|
|
hass.states.async_set(
|
|
|
|
"zone.work",
|
|
|
|
"zoning",
|
|
|
|
{"name": "work", "latitude": 20.1, "longitude": 10.1, "radius": 10},
|
|
|
|
)
|
|
|
|
|
|
|
|
hass.states.async_set(
|
|
|
|
"device_tracker.person",
|
|
|
|
"home",
|
|
|
|
{"friendly_name": "person", "latitude": 2.1, "longitude": 1.1},
|
|
|
|
)
|
|
|
|
assert test(hass)
|
|
|
|
|
|
|
|
hass.states.async_set(
|
|
|
|
"device_tracker.person",
|
|
|
|
"home",
|
|
|
|
{"friendly_name": "person", "latitude": 20.1, "longitude": 10.1},
|
|
|
|
)
|
|
|
|
assert test(hass)
|
|
|
|
|
|
|
|
hass.states.async_set(
|
|
|
|
"device_tracker.person",
|
|
|
|
"home",
|
|
|
|
{"friendly_name": "person", "latitude": 50.1, "longitude": 20.1},
|
|
|
|
)
|
|
|
|
assert not test(hass)
|
|
|
|
|
|
|
|
|
2020-01-30 00:19:13 +00:00
|
|
|
async def test_extract_entities():
|
|
|
|
"""Test extracting entities."""
|
2020-04-06 10:51:48 +00:00
|
|
|
assert condition.async_extract_entities(
|
2020-01-30 00:19:13 +00:00
|
|
|
{
|
|
|
|
"condition": "and",
|
|
|
|
"conditions": [
|
|
|
|
{
|
|
|
|
"condition": "state",
|
|
|
|
"entity_id": "sensor.temperature",
|
|
|
|
"state": "100",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"condition": "numeric_state",
|
|
|
|
"entity_id": "sensor.temperature_2",
|
|
|
|
"below": 110,
|
|
|
|
},
|
2020-04-30 22:15:53 +00:00
|
|
|
{
|
|
|
|
"condition": "not",
|
|
|
|
"conditions": [
|
|
|
|
{
|
|
|
|
"condition": "state",
|
|
|
|
"entity_id": "sensor.temperature_3",
|
|
|
|
"state": "100",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"condition": "numeric_state",
|
|
|
|
"entity_id": "sensor.temperature_4",
|
|
|
|
"below": 110,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"condition": "or",
|
|
|
|
"conditions": [
|
|
|
|
{
|
|
|
|
"condition": "state",
|
|
|
|
"entity_id": "sensor.temperature_5",
|
|
|
|
"state": "100",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"condition": "numeric_state",
|
|
|
|
"entity_id": "sensor.temperature_6",
|
|
|
|
"below": 110,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2020-06-15 20:54:19 +00:00
|
|
|
{
|
|
|
|
"condition": "state",
|
|
|
|
"entity_id": ["sensor.temperature_7", "sensor.temperature_8"],
|
|
|
|
"state": "100",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"condition": "numeric_state",
|
|
|
|
"entity_id": ["sensor.temperature_9", "sensor.temperature_10"],
|
|
|
|
"below": 110,
|
|
|
|
},
|
2020-09-13 20:05:45 +00:00
|
|
|
Template("{{ is_state('light.example', 'on') }}"),
|
2020-01-30 00:19:13 +00:00
|
|
|
],
|
|
|
|
}
|
2020-04-30 22:15:53 +00:00
|
|
|
) == {
|
|
|
|
"sensor.temperature",
|
|
|
|
"sensor.temperature_2",
|
|
|
|
"sensor.temperature_3",
|
|
|
|
"sensor.temperature_4",
|
|
|
|
"sensor.temperature_5",
|
|
|
|
"sensor.temperature_6",
|
2020-06-15 20:54:19 +00:00
|
|
|
"sensor.temperature_7",
|
|
|
|
"sensor.temperature_8",
|
|
|
|
"sensor.temperature_9",
|
|
|
|
"sensor.temperature_10",
|
2020-04-30 22:15:53 +00:00
|
|
|
}
|
2020-01-30 00:19:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_extract_devices():
|
|
|
|
"""Test extracting devices."""
|
2020-08-27 11:56:20 +00:00
|
|
|
assert (
|
|
|
|
condition.async_extract_devices(
|
|
|
|
{
|
|
|
|
"condition": "and",
|
|
|
|
"conditions": [
|
|
|
|
{"condition": "device", "device_id": "abcd", "domain": "light"},
|
|
|
|
{"condition": "device", "device_id": "qwer", "domain": "switch"},
|
|
|
|
{
|
|
|
|
"condition": "state",
|
|
|
|
"entity_id": "sensor.not_a_device",
|
|
|
|
"state": "100",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"condition": "not",
|
|
|
|
"conditions": [
|
|
|
|
{
|
|
|
|
"condition": "device",
|
|
|
|
"device_id": "abcd_not",
|
|
|
|
"domain": "light",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"condition": "device",
|
|
|
|
"device_id": "qwer_not",
|
|
|
|
"domain": "switch",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"condition": "or",
|
|
|
|
"conditions": [
|
|
|
|
{
|
|
|
|
"condition": "device",
|
|
|
|
"device_id": "abcd_or",
|
|
|
|
"domain": "light",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"condition": "device",
|
|
|
|
"device_id": "qwer_or",
|
|
|
|
"domain": "switch",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2020-09-13 20:05:45 +00:00
|
|
|
Template("{{ is_state('light.example', 'on') }}"),
|
2020-08-27 11:56:20 +00:00
|
|
|
],
|
|
|
|
}
|
|
|
|
)
|
|
|
|
== {"abcd", "qwer", "abcd_not", "qwer_not", "abcd_or", "qwer_or"}
|
|
|
|
)
|
2020-08-16 00:53:03 +00:00
|
|
|
|
|
|
|
|
2021-02-11 09:30:09 +00:00
|
|
|
async def test_condition_template_error(hass):
|
2020-08-16 00:53:03 +00:00
|
|
|
"""Test invalid template."""
|
|
|
|
test = await condition.async_from_config(
|
|
|
|
hass, {"condition": "template", "value_template": "{{ undefined.state }}"}
|
|
|
|
)
|
|
|
|
|
2021-02-11 09:30:09 +00:00
|
|
|
with pytest.raises(ConditionError, match="template"):
|
|
|
|
test(hass)
|
2020-10-26 15:01:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_condition_template_invalid_results(hass):
|
|
|
|
"""Test template condition render false with invalid results."""
|
|
|
|
test = await condition.async_from_config(
|
|
|
|
hass, {"condition": "template", "value_template": "{{ 'string' }}"}
|
|
|
|
)
|
|
|
|
assert not test(hass)
|
|
|
|
|
|
|
|
test = await condition.async_from_config(
|
|
|
|
hass, {"condition": "template", "value_template": "{{ 10.1 }}"}
|
|
|
|
)
|
|
|
|
assert not test(hass)
|
|
|
|
|
|
|
|
test = await condition.async_from_config(
|
|
|
|
hass, {"condition": "template", "value_template": "{{ 42 }}"}
|
|
|
|
)
|
|
|
|
assert not test(hass)
|
|
|
|
|
|
|
|
test = await condition.async_from_config(
|
|
|
|
hass, {"condition": "template", "value_template": "{{ [1, 2, 3] }}"}
|
|
|
|
)
|
|
|
|
assert not test(hass)
|