Add shorthand notation for boolean conditions (#70120)
This commit is contained in:
parent
8f4979ea17
commit
b50f369fe4
3 changed files with 314 additions and 32 deletions
|
@ -3,6 +3,7 @@ from datetime import datetime
|
|||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components import sun
|
||||
import homeassistant.components.automation as automation
|
||||
|
@ -288,6 +289,101 @@ async def test_and_condition_with_template(hass):
|
|||
assert test(hass)
|
||||
|
||||
|
||||
async def test_and_condition_shorthand(hass):
|
||||
"""Test the 'and' condition shorthand."""
|
||||
config = {
|
||||
"alias": "And Condition Shorthand",
|
||||
"and": [
|
||||
{
|
||||
"alias": "Template Condition",
|
||||
"condition": "template",
|
||||
"value_template": '{{ states.sensor.temperature.state == "100" }}',
|
||||
},
|
||||
{
|
||||
"condition": "numeric_state",
|
||||
"entity_id": "sensor.temperature",
|
||||
"below": 110,
|
||||
},
|
||||
],
|
||||
}
|
||||
config = cv.CONDITION_SCHEMA(config)
|
||||
config = await condition.async_validate_condition_config(hass, config)
|
||||
test = await condition.async_from_config(hass, config)
|
||||
|
||||
assert config["alias"] == "And Condition Shorthand"
|
||||
assert "and" not in config.keys()
|
||||
|
||||
hass.states.async_set("sensor.temperature", 120)
|
||||
assert not test(hass)
|
||||
assert_condition_trace(
|
||||
{
|
||||
"": [{"result": {"result": False}}],
|
||||
"conditions/0": [
|
||||
{"result": {"entities": ["sensor.temperature"], "result": False}}
|
||||
],
|
||||
}
|
||||
)
|
||||
|
||||
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_list_shorthand(hass):
|
||||
"""Test the 'and' condition list shorthand."""
|
||||
config = {
|
||||
"alias": "And Condition List Shorthand",
|
||||
"condition": [
|
||||
{
|
||||
"alias": "Template Condition",
|
||||
"condition": "template",
|
||||
"value_template": '{{ states.sensor.temperature.state == "100" }}',
|
||||
},
|
||||
{
|
||||
"condition": "numeric_state",
|
||||
"entity_id": "sensor.temperature",
|
||||
"below": 110,
|
||||
},
|
||||
],
|
||||
}
|
||||
config = cv.CONDITION_SCHEMA(config)
|
||||
config = await condition.async_validate_condition_config(hass, config)
|
||||
test = await condition.async_from_config(hass, config)
|
||||
|
||||
assert config["alias"] == "And Condition List Shorthand"
|
||||
assert "and" not in config.keys()
|
||||
|
||||
hass.states.async_set("sensor.temperature", 120)
|
||||
assert not test(hass)
|
||||
assert_condition_trace(
|
||||
{
|
||||
"": [{"result": {"result": False}}],
|
||||
"conditions/0": [
|
||||
{"result": {"entities": ["sensor.temperature"], "result": False}}
|
||||
],
|
||||
}
|
||||
)
|
||||
|
||||
hass.states.async_set("sensor.temperature", 105)
|
||||
assert not test(hass)
|
||||
|
||||
hass.states.async_set("sensor.temperature", 100)
|
||||
assert test(hass)
|
||||
|
||||
|
||||
async def test_malformed_and_condition_list_shorthand(hass):
|
||||
"""Test the 'and' condition list shorthand syntax check."""
|
||||
config = {
|
||||
"alias": "Bad shorthand syntax",
|
||||
"condition": ["bad", "syntax"],
|
||||
}
|
||||
|
||||
with pytest.raises(vol.MultipleInvalid):
|
||||
cv.CONDITION_SCHEMA(config)
|
||||
|
||||
|
||||
async def test_or_condition(hass):
|
||||
"""Test the 'or' condition."""
|
||||
config = {
|
||||
|
@ -471,6 +567,36 @@ async def test_or_condition_with_template(hass):
|
|||
assert test(hass)
|
||||
|
||||
|
||||
async def test_or_condition_shorthand(hass):
|
||||
"""Test the 'or' condition shorthand."""
|
||||
config = {
|
||||
"alias": "Or Condition Shorthand",
|
||||
"or": [
|
||||
{'{{ states.sensor.temperature.state == "100" }}'},
|
||||
{
|
||||
"condition": "numeric_state",
|
||||
"entity_id": "sensor.temperature",
|
||||
"below": 110,
|
||||
},
|
||||
],
|
||||
}
|
||||
config = cv.CONDITION_SCHEMA(config)
|
||||
config = await condition.async_validate_condition_config(hass, config)
|
||||
test = await condition.async_from_config(hass, config)
|
||||
|
||||
assert config["alias"] == "Or Condition Shorthand"
|
||||
assert "or" not in config.keys()
|
||||
|
||||
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_not_condition(hass):
|
||||
"""Test the 'not' condition."""
|
||||
config = {
|
||||
|
@ -670,6 +796,42 @@ async def test_not_condition_with_template(hass):
|
|||
assert not test(hass)
|
||||
|
||||
|
||||
async def test_not_condition_shorthand(hass):
|
||||
"""Test the 'or' condition shorthand."""
|
||||
config = {
|
||||
"alias": "Not Condition Shorthand",
|
||||
"not": [
|
||||
{
|
||||
"condition": "template",
|
||||
"value_template": '{{ states.sensor.temperature.state == "100" }}',
|
||||
},
|
||||
{
|
||||
"condition": "numeric_state",
|
||||
"entity_id": "sensor.temperature",
|
||||
"below": 50,
|
||||
},
|
||||
],
|
||||
}
|
||||
config = cv.CONDITION_SCHEMA(config)
|
||||
config = await condition.async_validate_condition_config(hass, config)
|
||||
test = await condition.async_from_config(hass, config)
|
||||
|
||||
assert config["alias"] == "Not Condition Shorthand"
|
||||
assert "not" not in config.keys()
|
||||
|
||||
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_time_window(hass):
|
||||
"""Test time condition windows."""
|
||||
sixam = "06:00:00"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue