mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 21:06:19 +00:00
Add device automation condition (#26313)
* Add support for device conditions * Lint * Update test case * Make and+or conditions async, adjust tests * Cleanup tests * Remove non callback versions of conditions, correct typing * Correct typing * Update light/strings.json * Address review comments * Make device automation lists simple lists, not dicts * Add device_automation/const.py * Use IS_ON/IS_OFF everywhere for conditions
This commit is contained in:
@@ -4,182 +4,175 @@ from unittest.mock import patch
|
||||
from homeassistant.helpers import condition
|
||||
from homeassistant.util import dt
|
||||
|
||||
from tests.common import get_test_home_assistant
|
||||
|
||||
async def test_and_condition(hass):
|
||||
"""Test the 'and' condition."""
|
||||
test = await condition.async_from_config(
|
||||
hass,
|
||||
{
|
||||
"condition": "and",
|
||||
"conditions": [
|
||||
{
|
||||
"condition": "state",
|
||||
"entity_id": "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)
|
||||
|
||||
|
||||
class TestConditionHelper:
|
||||
"""Test condition helpers."""
|
||||
async def test_and_condition_with_template(hass):
|
||||
"""Test the 'and' condition."""
|
||||
test = await condition.async_from_config(
|
||||
hass,
|
||||
{
|
||||
"condition": "and",
|
||||
"conditions": [
|
||||
{
|
||||
"condition": "template",
|
||||
"value_template": '{{ states.sensor.temperature.state == "100" }}',
|
||||
},
|
||||
{
|
||||
"condition": "numeric_state",
|
||||
"entity_id": "sensor.temperature",
|
||||
"below": 110,
|
||||
},
|
||||
],
|
||||
},
|
||||
)
|
||||
|
||||
def setup_method(self, method):
|
||||
"""Set up things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
hass.states.async_set("sensor.temperature", 120)
|
||||
assert not test(hass)
|
||||
|
||||
def teardown_method(self, method):
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
hass.states.async_set("sensor.temperature", 105)
|
||||
assert not test(hass)
|
||||
|
||||
def test_and_condition(self):
|
||||
"""Test the 'and' condition."""
|
||||
test = condition.from_config(
|
||||
{
|
||||
"condition": "and",
|
||||
"conditions": [
|
||||
{
|
||||
"condition": "state",
|
||||
"entity_id": "sensor.temperature",
|
||||
"state": "100",
|
||||
},
|
||||
{
|
||||
"condition": "numeric_state",
|
||||
"entity_id": "sensor.temperature",
|
||||
"below": 110,
|
||||
},
|
||||
],
|
||||
}
|
||||
)
|
||||
hass.states.async_set("sensor.temperature", 100)
|
||||
assert test(hass)
|
||||
|
||||
self.hass.states.set("sensor.temperature", 120)
|
||||
assert not test(self.hass)
|
||||
|
||||
self.hass.states.set("sensor.temperature", 105)
|
||||
assert not test(self.hass)
|
||||
async def test_or_condition(hass):
|
||||
"""Test the 'or' condition."""
|
||||
test = await condition.async_from_config(
|
||||
hass,
|
||||
{
|
||||
"condition": "or",
|
||||
"conditions": [
|
||||
{
|
||||
"condition": "state",
|
||||
"entity_id": "sensor.temperature",
|
||||
"state": "100",
|
||||
},
|
||||
{
|
||||
"condition": "numeric_state",
|
||||
"entity_id": "sensor.temperature",
|
||||
"below": 110,
|
||||
},
|
||||
],
|
||||
},
|
||||
)
|
||||
|
||||
self.hass.states.set("sensor.temperature", 100)
|
||||
assert test(self.hass)
|
||||
hass.states.async_set("sensor.temperature", 120)
|
||||
assert not test(hass)
|
||||
|
||||
def test_and_condition_with_template(self):
|
||||
"""Test the 'and' condition."""
|
||||
test = condition.from_config(
|
||||
{
|
||||
"condition": "and",
|
||||
"conditions": [
|
||||
{
|
||||
"condition": "template",
|
||||
"value_template": '{{ states.sensor.temperature.state == "100" }}',
|
||||
},
|
||||
{
|
||||
"condition": "numeric_state",
|
||||
"entity_id": "sensor.temperature",
|
||||
"below": 110,
|
||||
},
|
||||
],
|
||||
}
|
||||
)
|
||||
hass.states.async_set("sensor.temperature", 105)
|
||||
assert test(hass)
|
||||
|
||||
self.hass.states.set("sensor.temperature", 120)
|
||||
assert not test(self.hass)
|
||||
hass.states.async_set("sensor.temperature", 100)
|
||||
assert test(hass)
|
||||
|
||||
self.hass.states.set("sensor.temperature", 105)
|
||||
assert not test(self.hass)
|
||||
|
||||
self.hass.states.set("sensor.temperature", 100)
|
||||
assert test(self.hass)
|
||||
async def test_or_condition_with_template(hass):
|
||||
"""Test the 'or' condition."""
|
||||
test = await condition.async_from_config(
|
||||
hass,
|
||||
{
|
||||
"condition": "or",
|
||||
"conditions": [
|
||||
{
|
||||
"condition": "template",
|
||||
"value_template": '{{ states.sensor.temperature.state == "100" }}',
|
||||
},
|
||||
{
|
||||
"condition": "numeric_state",
|
||||
"entity_id": "sensor.temperature",
|
||||
"below": 110,
|
||||
},
|
||||
],
|
||||
},
|
||||
)
|
||||
|
||||
def test_or_condition(self):
|
||||
"""Test the 'or' condition."""
|
||||
test = condition.from_config(
|
||||
{
|
||||
"condition": "or",
|
||||
"conditions": [
|
||||
{
|
||||
"condition": "state",
|
||||
"entity_id": "sensor.temperature",
|
||||
"state": "100",
|
||||
},
|
||||
{
|
||||
"condition": "numeric_state",
|
||||
"entity_id": "sensor.temperature",
|
||||
"below": 110,
|
||||
},
|
||||
],
|
||||
}
|
||||
)
|
||||
hass.states.async_set("sensor.temperature", 120)
|
||||
assert not test(hass)
|
||||
|
||||
self.hass.states.set("sensor.temperature", 120)
|
||||
assert not test(self.hass)
|
||||
hass.states.async_set("sensor.temperature", 105)
|
||||
assert test(hass)
|
||||
|
||||
self.hass.states.set("sensor.temperature", 105)
|
||||
assert test(self.hass)
|
||||
hass.states.async_set("sensor.temperature", 100)
|
||||
assert test(hass)
|
||||
|
||||
self.hass.states.set("sensor.temperature", 100)
|
||||
assert test(self.hass)
|
||||
|
||||
def test_or_condition_with_template(self):
|
||||
"""Test the 'or' condition."""
|
||||
test = condition.from_config(
|
||||
{
|
||||
"condition": "or",
|
||||
"conditions": [
|
||||
{
|
||||
"condition": "template",
|
||||
"value_template": '{{ states.sensor.temperature.state == "100" }}',
|
||||
},
|
||||
{
|
||||
"condition": "numeric_state",
|
||||
"entity_id": "sensor.temperature",
|
||||
"below": 110,
|
||||
},
|
||||
],
|
||||
}
|
||||
)
|
||||
async def test_time_window(hass):
|
||||
"""Test time condition windows."""
|
||||
sixam = dt.parse_time("06:00:00")
|
||||
sixpm = dt.parse_time("18:00:00")
|
||||
|
||||
self.hass.states.set("sensor.temperature", 120)
|
||||
assert not test(self.hass)
|
||||
with patch(
|
||||
"homeassistant.helpers.condition.dt_util.now",
|
||||
return_value=dt.now().replace(hour=3),
|
||||
):
|
||||
assert not condition.time(after=sixam, before=sixpm)
|
||||
assert condition.time(after=sixpm, before=sixam)
|
||||
|
||||
self.hass.states.set("sensor.temperature", 105)
|
||||
assert test(self.hass)
|
||||
with patch(
|
||||
"homeassistant.helpers.condition.dt_util.now",
|
||||
return_value=dt.now().replace(hour=9),
|
||||
):
|
||||
assert condition.time(after=sixam, before=sixpm)
|
||||
assert not condition.time(after=sixpm, before=sixam)
|
||||
|
||||
self.hass.states.set("sensor.temperature", 100)
|
||||
assert test(self.hass)
|
||||
with patch(
|
||||
"homeassistant.helpers.condition.dt_util.now",
|
||||
return_value=dt.now().replace(hour=15),
|
||||
):
|
||||
assert condition.time(after=sixam, before=sixpm)
|
||||
assert not condition.time(after=sixpm, before=sixam)
|
||||
|
||||
def test_time_window(self):
|
||||
"""Test time condition windows."""
|
||||
sixam = dt.parse_time("06:00:00")
|
||||
sixpm = dt.parse_time("18:00:00")
|
||||
with patch(
|
||||
"homeassistant.helpers.condition.dt_util.now",
|
||||
return_value=dt.now().replace(hour=21),
|
||||
):
|
||||
assert not condition.time(after=sixam, before=sixpm)
|
||||
assert condition.time(after=sixpm, before=sixam)
|
||||
|
||||
with patch(
|
||||
"homeassistant.helpers.condition.dt_util.now",
|
||||
return_value=dt.now().replace(hour=3),
|
||||
):
|
||||
assert not condition.time(after=sixam, before=sixpm)
|
||||
assert condition.time(after=sixpm, before=sixam)
|
||||
|
||||
with patch(
|
||||
"homeassistant.helpers.condition.dt_util.now",
|
||||
return_value=dt.now().replace(hour=9),
|
||||
):
|
||||
assert condition.time(after=sixam, before=sixpm)
|
||||
assert not condition.time(after=sixpm, before=sixam)
|
||||
async def test_if_numeric_state_not_raise_on_unavailable(hass):
|
||||
"""Test numeric_state doesn't raise on unavailable/unknown state."""
|
||||
test = await condition.async_from_config(
|
||||
hass,
|
||||
{"condition": "numeric_state", "entity_id": "sensor.temperature", "below": 42},
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.helpers.condition.dt_util.now",
|
||||
return_value=dt.now().replace(hour=15),
|
||||
):
|
||||
assert condition.time(after=sixam, before=sixpm)
|
||||
assert not condition.time(after=sixpm, before=sixam)
|
||||
with patch("homeassistant.helpers.condition._LOGGER.warning") as logwarn:
|
||||
hass.states.async_set("sensor.temperature", "unavailable")
|
||||
assert not test(hass)
|
||||
assert len(logwarn.mock_calls) == 0
|
||||
|
||||
with patch(
|
||||
"homeassistant.helpers.condition.dt_util.now",
|
||||
return_value=dt.now().replace(hour=21),
|
||||
):
|
||||
assert not condition.time(after=sixam, before=sixpm)
|
||||
assert condition.time(after=sixpm, before=sixam)
|
||||
|
||||
def test_if_numeric_state_not_raise_on_unavailable(self):
|
||||
"""Test numeric_state doesn't raise on unavailable/unknown state."""
|
||||
test = condition.from_config(
|
||||
{
|
||||
"condition": "numeric_state",
|
||||
"entity_id": "sensor.temperature",
|
||||
"below": 42,
|
||||
}
|
||||
)
|
||||
|
||||
with patch("homeassistant.helpers.condition._LOGGER.warning") as logwarn:
|
||||
self.hass.states.set("sensor.temperature", "unavailable")
|
||||
assert not test(self.hass)
|
||||
assert len(logwarn.mock_calls) == 0
|
||||
|
||||
self.hass.states.set("sensor.temperature", "unknown")
|
||||
assert not test(self.hass)
|
||||
assert len(logwarn.mock_calls) == 0
|
||||
hass.states.async_set("sensor.temperature", "unknown")
|
||||
assert not test(hass)
|
||||
assert len(logwarn.mock_calls) == 0
|
||||
|
||||
Reference in New Issue
Block a user