Skip to content

Commit

Permalink
Add completed to the wait variable when using triggers (`wait_for_t…
Browse files Browse the repository at this point in the history
…rigger`) (home-assistant#123427)

* Add support for the wait.completed variable when using wait with triggers

* Remove junk comment

---------

Co-authored-by: Erik Montnemery <[email protected]>
  • Loading branch information
2 people authored and zxdavb committed Oct 24, 2024
1 parent 4031cb9 commit 15f83df
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 93 deletions.
7 changes: 6 additions & 1 deletion homeassistant/helpers/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -1133,7 +1133,11 @@ async def _async_wait_for_trigger_step(self) -> None:
self._step_log("wait for trigger", timeout)

variables = {**self._variables}
self._variables["wait"] = {"remaining": timeout, "trigger": None}
self._variables["wait"] = {
"remaining": timeout,
"completed": False,
"trigger": None,
}
trace_set_result(wait=self._variables["wait"])

if timeout == 0:
Expand All @@ -1151,6 +1155,7 @@ async def async_done(
variables: dict[str, Any], context: Context | None = None
) -> None:
self._async_set_remaining_time_var(timeout_handle)
self._variables["wait"]["completed"] = True
self._variables["wait"]["trigger"] = variables["trigger"]
_set_result_unless_done(done)

Expand Down
170 changes: 78 additions & 92 deletions tests/helpers/test_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -943,18 +943,9 @@ async def test_wait_basic(hass: HomeAssistant, action_type) -> None:
assert not script_obj.is_running
assert script_obj.last_action is None

if action_type == "template":
assert_action_trace(
{
"0": [
{
"result": {"wait": {"completed": True, "remaining": None}},
"variables": {"wait": {"completed": True, "remaining": None}},
}
],
}
)
else:
expected_var = {"completed": True, "remaining": None}

if action_type == "trigger":
expected_trigger = {
"alias": None,
"attribute": None,
Expand All @@ -967,23 +958,18 @@ async def test_wait_basic(hass: HomeAssistant, action_type) -> None:
"platform": "state",
"to_state": ANY,
}
assert_action_trace(
{
"0": [
{
"result": {
"wait": {
"trigger": expected_trigger,
"remaining": None,
}
},
"variables": {
"wait": {"remaining": None, "trigger": expected_trigger}
},
}
],
}
)
expected_var["trigger"] = expected_trigger

assert_action_trace(
{
"0": [
{
"result": {"wait": expected_var},
"variables": {"wait": expected_var},
}
],
}
)


async def test_wait_for_trigger_variables(hass: HomeAssistant) -> None:
Expand Down Expand Up @@ -1059,28 +1045,21 @@ async def test_wait_basic_times_out(hass: HomeAssistant, action_type) -> None:

assert timed_out

if action_type == "template":
assert_action_trace(
{
"0": [
{
"result": {"wait": {"completed": False, "remaining": None}},
"variables": {"wait": {"completed": False, "remaining": None}},
}
],
}
)
else:
assert_action_trace(
{
"0": [
{
"result": {"wait": {"trigger": None, "remaining": None}},
"variables": {"wait": {"remaining": None, "trigger": None}},
}
],
}
)
expected_var = {"completed": False, "remaining": None}

if action_type == "trigger":
expected_var["trigger"] = None

assert_action_trace(
{
"0": [
{
"result": {"wait": expected_var},
"variables": {"wait": expected_var},
}
],
}
)


@pytest.mark.parametrize("action_type", ["template", "trigger"])
Expand Down Expand Up @@ -1183,30 +1162,22 @@ async def test_cancel_wait(hass: HomeAssistant, action_type) -> None:
assert not script_obj.is_running
assert len(events) == 0

if action_type == "template":
assert_action_trace(
{
"0": [
{
"result": {"wait": {"completed": False, "remaining": None}},
"variables": {"wait": {"completed": False, "remaining": None}},
}
],
},
expected_script_execution="cancelled",
)
else:
assert_action_trace(
{
"0": [
{
"result": {"wait": {"trigger": None, "remaining": None}},
"variables": {"wait": {"remaining": None, "trigger": None}},
}
],
},
expected_script_execution="cancelled",
)
expected_var = {"completed": False, "remaining": None}

if action_type == "trigger":
expected_var["trigger"] = None

assert_action_trace(
{
"0": [
{
"result": {"wait": expected_var},
"variables": {"wait": expected_var},
}
],
},
expected_script_execution="cancelled",
)


async def test_wait_template_not_schedule(hass: HomeAssistant) -> None:
Expand Down Expand Up @@ -1294,10 +1265,11 @@ async def test_wait_timeout(
assert len(events) == 1
assert "(timeout: 0:00:05)" in caplog.text

if action_type == "template":
variable_wait = {"wait": {"completed": False, "remaining": 0.0}}
else:
variable_wait = {"wait": {"trigger": None, "remaining": 0.0}}
variable_wait = {"wait": {"completed": False, "remaining": 0.0}}

if action_type == "trigger":
variable_wait["wait"]["trigger"] = None

expected_trace = {
"0": [
{
Expand Down Expand Up @@ -1345,7 +1317,7 @@ async def test_wait_trigger_with_zero_timeout(
assert len(events) == 1
assert "(timeout: 0:00:00)" in caplog.text

variable_wait = {"wait": {"trigger": None, "remaining": 0.0}}
variable_wait = {"wait": {"completed": False, "trigger": None, "remaining": 0.0}}
expected_trace = {
"0": [
{
Expand Down Expand Up @@ -1393,7 +1365,7 @@ async def test_wait_trigger_matches_with_zero_timeout(
assert len(events) == 1
assert "(timeout: 0:00:00)" in caplog.text

variable_wait = {"wait": {"trigger": None, "remaining": 0.0}}
variable_wait = {"wait": {"completed": False, "trigger": None, "remaining": 0.0}}
expected_trace = {
"0": [
{
Expand Down Expand Up @@ -1533,12 +1505,11 @@ async def test_wait_continue_on_timeout(
assert not script_obj.is_running
assert len(events) == n_events

if action_type == "template":
result_wait = {"wait": {"completed": False, "remaining": 0.0}}
variable_wait = dict(result_wait)
else:
result_wait = {"wait": {"trigger": None, "remaining": 0.0}}
variable_wait = dict(result_wait)
result_wait = {"wait": {"completed": False, "remaining": 0.0}}
if action_type == "trigger":
result_wait["wait"]["trigger"] = None

variable_wait = dict(result_wait)
expected_trace = {
"0": [{"result": result_wait, "variables": variable_wait}],
}
Expand Down Expand Up @@ -1766,8 +1737,12 @@ async def async_attach_trigger_mock(*args, **kwargs):
{
"0": [
{
"result": {"wait": {"trigger": None, "remaining": None}},
"variables": {"wait": {"remaining": None, "trigger": None}},
"result": {
"wait": {"completed": False, "trigger": None, "remaining": None}
},
"variables": {
"wait": {"completed": False, "remaining": None, "trigger": None}
},
}
],
}
Expand Down Expand Up @@ -1807,8 +1782,12 @@ async def async_attach_trigger_mock(*args, **kwargs):
{
"0": [
{
"result": {"wait": {"trigger": None, "remaining": None}},
"variables": {"wait": {"remaining": None, "trigger": None}},
"result": {
"wait": {"completed": False, "trigger": None, "remaining": None}
},
"variables": {
"wait": {"completed": False, "remaining": None, "trigger": None}
},
}
],
}
Expand Down Expand Up @@ -3717,11 +3696,18 @@ async def test_parallel(hass: HomeAssistant, caplog: pytest.LogCaptureFixture) -
{
"result": {
"wait": {
"completed": True,
"remaining": None,
"trigger": expected_trigger,
}
},
"variables": {
"wait": {
"completed": True,
"remaining": None,
"trigger": expected_trigger,
}
},
"variables": {"wait": {"remaining": None, "trigger": expected_trigger}},
}
],
"0/parallel/1/sequence/0": [
Expand Down

0 comments on commit 15f83df

Please sign in to comment.