Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion homeassistant/helpers/condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -1748,9 +1748,14 @@ def state_validate_config(hass: HomeAssistant, config: ConfigType) -> ConfigType


async def async_validate_condition_config(
hass: HomeAssistant, config: ConfigType
hass: HomeAssistant, config: ConfigType | str
) -> ConfigType:
"""Validate config."""
if isinstance(config, str):
config = {
CONF_CONDITION: "template",
CONF_VALUE_TEMPLATE: cv.dynamic_template(config),
}
condition_key: str = config[CONF_CONDITION]

if condition_key in ("and", "not", "or"):
Expand Down
19 changes: 19 additions & 0 deletions tests/helpers/test_condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,25 @@ async def test_or_condition_shorthand(hass: HomeAssistant) -> None:
assert test.async_check()


async def test_shorthand_template_condition_in_or(hass: HomeAssistant) -> None:
"""Test shorthand template condition inside or block doesn't crash."""
config = {
"condition": "or",
"conditions": [
'{{ states("sensor.test") == "on" }}',
{"condition": "state", "entity_id": "sensor.other", "state": "on"},
],
}
config = await condition.async_validate_condition_config(hass, config)
assert config["conditions"][0]["condition"] == "template"

# Verify the condition can actually be evaluated at runtime
test = await condition.async_from_config(hass, config)
hass.states.async_set("sensor.test", "on")
hass.states.async_set("sensor.other", "off")
assert test.async_check()


async def test_not_condition(hass: HomeAssistant) -> None:
"""Test the 'not' condition."""
config = {
Expand Down