Skip to content

Commit

Permalink
Support shorthand logical operators in script sequences (home-assista…
Browse files Browse the repository at this point in the history
  • Loading branch information
frenck authored and balloob committed Apr 29, 2022
1 parent 0b144be commit 3ffdbc4
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 1 deletion.
2 changes: 1 addition & 1 deletion homeassistant/helpers/config_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1599,7 +1599,7 @@ def determine_script_action(action: dict[str, Any]) -> str:
if CONF_WAIT_TEMPLATE in action:
return SCRIPT_ACTION_WAIT_TEMPLATE

if CONF_CONDITION in action:
if any(key in action for key in (CONF_CONDITION, "and", "or", "not")):
return SCRIPT_ACTION_CHECK_CONDITION

if CONF_EVENT in action:
Expand Down
117 changes: 117 additions & 0 deletions tests/helpers/test_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -4744,3 +4744,120 @@ def broken_service(service: ServiceCall) -> None:
"3": [{"result": {"event": "test_event", "event_data": {}}}],
},
)


async def test_condition_and_shorthand(hass, caplog):
"""Test if we can use the shorthand and conditions in a script."""
events = async_capture_events(hass, "test_event")
sequence = cv.SCRIPT_SCHEMA(
[
{"event": "test_event"},
{
"alias": "shorthand and condition",
"and": [
{
"condition": "template",
"value_template": "{{ states('test.entity') == 'hello' }}",
}
],
},
{"event": "test_event"},
]
)
script_obj = script.Script(hass, sequence, "Test Name", "test_domain")

hass.states.async_set("test.entity", "hello")
await script_obj.async_run(context=Context())
await hass.async_block_till_done()

assert "Test condition shorthand and condition: True" in caplog.text
assert len(events) == 2

assert_action_trace(
{
"0": [{"result": {"event": "test_event", "event_data": {}}}],
"1": [{"result": {"result": True}}],
"1/conditions/0": [
{"result": {"entities": ["test.entity"], "result": True}}
],
"2": [{"result": {"event": "test_event", "event_data": {}}}],
}
)


async def test_condition_or_shorthand(hass, caplog):
"""Test if we can use the shorthand or conditions in a script."""
events = async_capture_events(hass, "test_event")
sequence = cv.SCRIPT_SCHEMA(
[
{"event": "test_event"},
{
"alias": "shorthand or condition",
"or": [
{
"condition": "template",
"value_template": "{{ states('test.entity') == 'hello' }}",
}
],
},
{"event": "test_event"},
]
)
script_obj = script.Script(hass, sequence, "Test Name", "test_domain")

hass.states.async_set("test.entity", "hello")
await script_obj.async_run(context=Context())
await hass.async_block_till_done()

assert "Test condition shorthand or condition: True" in caplog.text
assert len(events) == 2

assert_action_trace(
{
"0": [{"result": {"event": "test_event", "event_data": {}}}],
"1": [{"result": {"result": True}}],
"1/conditions/0": [
{"result": {"entities": ["test.entity"], "result": True}}
],
"2": [{"result": {"event": "test_event", "event_data": {}}}],
}
)


async def test_condition_not_shorthand(hass, caplog):
"""Test if we can use the shorthand not conditions in a script."""
events = async_capture_events(hass, "test_event")
sequence = cv.SCRIPT_SCHEMA(
[
{"event": "test_event"},
{
"alias": "shorthand not condition",
"not": [
{
"condition": "template",
"value_template": "{{ states('test.entity') == 'hello' }}",
}
],
},
{"event": "test_event"},
]
)
script_obj = script.Script(hass, sequence, "Test Name", "test_domain")

hass.states.async_set("test.entity", "not hello")
await script_obj.async_run(context=Context())
await hass.async_block_till_done()

assert "Test condition shorthand not condition: True" in caplog.text
assert len(events) == 2

assert_action_trace(
{
"0": [{"result": {"event": "test_event", "event_data": {}}}],
"1": [{"result": {"result": True}}],
"1/conditions/0": [
{"result": {"entities": ["test.entity"], "result": False}}
],
"2": [{"result": {"event": "test_event", "event_data": {}}}],
}
)

0 comments on commit 3ffdbc4

Please sign in to comment.