From 030f6064089268ab9e8475de9d15e3cbd452f0cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexey=20ALERT=20Rubash=D1=91ff?= Date: Fri, 4 Dec 2026 10:37:13 +0200 Subject: [PATCH 1/3] Give .old as None in a @state_trigger with watch, as documented With watch, a @state_trigger expression gets values only for the watched names. The .old of a variable that did not cause the evaluation is not among them. pyscript then reads the current state of that variable and looks for an attribute named old on it, which raises AttributeError: 'StateVal' object has no attribute 'old', and the trigger does not run. The check when the trigger starts (state_check_now or state_hold_false) fails the same way, also with one watched variable. Without watch, such a .old is None, and the docs say that it is None. The trigger now collects the X.old and X.old.attr names of its expression when it is created. Before each evaluation, _is_trig_ok() gives them to State.notify_var_get(), which already gives None for them. A .old that the change sets keeps its value. Only the .old names get a value: a value for each name of the expression would hide functions such as state.get(). Fixes #831. --- .../pyscript/decorators/state.py | 10 +++ tests/test_function.py | 65 +++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/custom_components/pyscript/decorators/state.py b/custom_components/pyscript/decorators/state.py index 5b5eb78..e3baa10 100644 --- a/custom_components/pyscript/decorators/state.py +++ b/custom_components/pyscript/decorators/state.py @@ -160,6 +160,7 @@ class StateTriggerDecorator(TriggerDecorator, ExpressionDecorator, AutoKwargsDec state_trig_ident: set[str] state_trig_ident_any: set[str] + state_trig_old_names: set[str] true_entered_at: float | None false_entered_at: float | None @@ -171,6 +172,7 @@ async def validate(self) -> None: await super().validate() self.state_trig_ident = set() self.state_trig_ident_any = set() + self.state_trig_old_names = set() self.in_wait_until_function = isinstance(self.dm, WaitUntilDecoratorManager) @@ -194,6 +196,11 @@ async def validate(self) -> None: state_trig_expr = any_trigger_exprs(state_trig) self.create_expression(state_trig_expr) + self.state_trig_old_names = { + name + for name in await self._ast_expression.get_names() + if name.count(".") in (2, 3) and name.split(".")[2] == "old" + } if self.kwargs.get("watch") is not None: self.state_trig_ident = set(self.kwargs.get("watch", [])) @@ -359,6 +366,9 @@ async def _cycle(self) -> None: async def _is_trig_ok(self, new_vars: dict[str, Any]) -> bool: if self.has_expression(): + # with watch, new_vars only has the watched names; as without watch, the .old of + # each state variable that didn't cause this evaluation is None + new_vars = State.notify_var_get(self.state_trig_old_names, new_vars) return await self.check_expression_vars(new_vars) return True diff --git a/tests/test_function.py b/tests/test_function.py index 310b737..10ca01a 100644 --- a/tests/test_function.py +++ b/tests/test_function.py @@ -842,6 +842,71 @@ def func1(value=None, /, **kwargs): await pyscript.wait_done([None, "1", "pyscript.var1"]) +@pytest.mark.asyncio +async def test_state_trigger_watch_other_old_is_none(pyscript): + """With watch, the .old of a variable that didn't cause the evaluation is None, as without watch.""" + await pyscript.start( + """ +@state_trigger( + "pyscript.var1 == 'on' and pyscript.var1.old != 'on'", + "pyscript.var2 == 'on' and pyscript.var2.old != 'on'", + watch=["pyscript.var1", "pyscript.var2"], +) +def func1(var_name=None): + pyscript.done = var_name + +@state_trigger( + "pyscript.var3 == '1' and pyscript.var4.old is None and pyscript.var4.old.attr1 is None", + watch=["pyscript.var3"], +) +def func2(var_name=None): + pyscript.done2 = var_name +""" + ) + + pyscript.hass.states.async_set("pyscript.var2", "on") + await pyscript.wait_done("pyscript.var2") + + # pyscript.var2 is on, so the expression reads pyscript.var2.old, which this change doesn't set + pyscript.hass.states.async_set("pyscript.var1", "on") + await pyscript.wait_done("pyscript.var1") + + pyscript.hass.states.async_set("pyscript.var2", "off") + pyscript.hass.states.async_set("pyscript.var2", "on") + await pyscript.wait_done("pyscript.var2") + + # pyscript.var4 isn't watched by any trigger + pyscript.hass.states.async_set("pyscript.var4", "1", {"attr1": 2}) + pyscript.hass.states.async_set("pyscript.var3", "1") + await pyscript.wait_done2("pyscript.var3") + + +@pytest.mark.asyncio +async def test_state_trigger_watch_old_check_on_start(pyscript): + """The check when the trigger starts gives .old as None with watch (state_check_now, state_hold_false).""" + pyscript.hass.states.async_set("pyscript.var1", "on") + pyscript.hass.states.async_set("pyscript.var2", "on") + await pyscript.start( + """ +@state_trigger("pyscript.var1 == 'on' and pyscript.var1.old is None", watch=["pyscript.var1"], state_check_now=True) +def func1(trigger_type=None, var_name=None): + pyscript.done = [trigger_type, var_name] + +@state_trigger( + "pyscript.var2 == 'on' and pyscript.var2.old == 'off'", watch=["pyscript.var2"], state_hold_false=0 +) +def func2(var_name=None): + pyscript.done2 = var_name +""" + ) + + await pyscript.wait_done(["state", None]) + + pyscript.hass.states.async_set("pyscript.var2", "off") + pyscript.hass.states.async_set("pyscript.var2", "on") + await pyscript.wait_done2("pyscript.var2") + + @pytest.mark.asyncio async def test_time_active_hold_off_send_last(hass): """Test hold_off_send_last runs with the latest suppressed trigger data.""" From 627eff0f390a4a692bbfffc3efa095a5f6619c49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexey=20ALERT=20Rubash=D1=91ff?= Date: Fri, 4 Dec 2026 11:06:20 +0200 Subject: [PATCH 2/3] Make the watch .old test catch a run that should not happen The test used .old != 'on', which is true when the .old of the other entity is None, so the trigger also ran when an entity went off. The next run wrote the same value to pyscript.done, which fires no state change, so the test did not see the extra run. The test now uses .old == 'off', which runs only for a change from off to on, and writes the new value with the entity name. An extra run or a missing run now gives a different pyscript.done value and fails the test. --- tests/test_function.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/tests/test_function.py b/tests/test_function.py index 10ca01a..aa46aec 100644 --- a/tests/test_function.py +++ b/tests/test_function.py @@ -845,15 +845,17 @@ def func1(value=None, /, **kwargs): @pytest.mark.asyncio async def test_state_trigger_watch_other_old_is_none(pyscript): """With watch, the .old of a variable that didn't cause the evaluation is None, as without watch.""" + pyscript.hass.states.async_set("pyscript.var1", "off") + pyscript.hass.states.async_set("pyscript.var2", "off") await pyscript.start( """ @state_trigger( - "pyscript.var1 == 'on' and pyscript.var1.old != 'on'", - "pyscript.var2 == 'on' and pyscript.var2.old != 'on'", + "pyscript.var1 == 'on' and pyscript.var1.old == 'off'", + "pyscript.var2 == 'on' and pyscript.var2.old == 'off'", watch=["pyscript.var1", "pyscript.var2"], ) -def func1(var_name=None): - pyscript.done = var_name +def func1(var_name=None, value=None): + pyscript.done = f"{var_name} {value}" @state_trigger( "pyscript.var3 == '1' and pyscript.var4.old is None and pyscript.var4.old.attr1 is None", @@ -865,15 +867,16 @@ def func2(var_name=None): ) pyscript.hass.states.async_set("pyscript.var2", "on") - await pyscript.wait_done("pyscript.var2") + await pyscript.wait_done("pyscript.var2 on") # pyscript.var2 is on, so the expression reads pyscript.var2.old, which this change doesn't set pyscript.hass.states.async_set("pyscript.var1", "on") - await pyscript.wait_done("pyscript.var1") + await pyscript.wait_done("pyscript.var1 on") + # pyscript.var1.old is None for a change of pyscript.var2, so going off doesn't run func1 pyscript.hass.states.async_set("pyscript.var2", "off") pyscript.hass.states.async_set("pyscript.var2", "on") - await pyscript.wait_done("pyscript.var2") + await pyscript.wait_done("pyscript.var2 on") # pyscript.var4 isn't watched by any trigger pyscript.hass.states.async_set("pyscript.var4", "1", {"attr1": 2}) From 128dabdd38aa42c1ebddb0692c4f572903d1644a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexey=20ALERT=20Rubash=D1=91ff?= Date: Fri, 4 Dec 2026 11:30:40 +0200 Subject: [PATCH 3/3] Collect the .old names only with watch, and test task.wait_until Without watch, the .old names of the expression are already among the names of the trigger, so the trigger now collects them only when it has watch, and _is_trig_ok() adds nothing when there are none. The names are split into parts as the code near it does. task.wait_until(state_trigger=..., watch=[...]) uses the same trigger code, and a new test shows that it gives the .old of an entity that it does not watch as None. The comment and the test docstrings are shorter. --- .../pyscript/decorators/state.py | 17 +++++------ tests/test_function.py | 28 +++++++++++++++++-- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/custom_components/pyscript/decorators/state.py b/custom_components/pyscript/decorators/state.py index e3baa10..e2f846a 100644 --- a/custom_components/pyscript/decorators/state.py +++ b/custom_components/pyscript/decorators/state.py @@ -196,14 +196,15 @@ async def validate(self) -> None: state_trig_expr = any_trigger_exprs(state_trig) self.create_expression(state_trig_expr) - self.state_trig_old_names = { - name - for name in await self._ast_expression.get_names() - if name.count(".") in (2, 3) and name.split(".")[2] == "old" - } if self.kwargs.get("watch") is not None: self.state_trig_ident = set(self.kwargs.get("watch", [])) + if self.has_expression(): + # watch replaces the expression's names, so keep its .old names to give them a value + for name in await self._ast_expression.get_names(): + parts = name.split(".") + if len(parts) in (3, 4) and parts[2] == "old": + self.state_trig_old_names.add(name) else: if self.has_expression(): self.state_trig_ident = await self._ast_expression.get_names() @@ -366,9 +367,9 @@ async def _cycle(self) -> None: async def _is_trig_ok(self, new_vars: dict[str, Any]) -> bool: if self.has_expression(): - # with watch, new_vars only has the watched names; as without watch, the .old of - # each state variable that didn't cause this evaluation is None - new_vars = State.notify_var_get(self.state_trig_old_names, new_vars) + if self.state_trig_old_names: + # with watch, new_vars lacks the expression's .old names; add them, as without watch + new_vars = State.notify_var_get(self.state_trig_old_names, new_vars) return await self.check_expression_vars(new_vars) return True diff --git a/tests/test_function.py b/tests/test_function.py index aa46aec..fa748da 100644 --- a/tests/test_function.py +++ b/tests/test_function.py @@ -844,7 +844,7 @@ def func1(value=None, /, **kwargs): @pytest.mark.asyncio async def test_state_trigger_watch_other_old_is_none(pyscript): - """With watch, the .old of a variable that didn't cause the evaluation is None, as without watch.""" + """With watch, .old of a variable that didn't cause the evaluation is None.""" pyscript.hass.states.async_set("pyscript.var1", "off") pyscript.hass.states.async_set("pyscript.var2", "off") await pyscript.start( @@ -886,7 +886,7 @@ def func2(var_name=None): @pytest.mark.asyncio async def test_state_trigger_watch_old_check_on_start(pyscript): - """The check when the trigger starts gives .old as None with watch (state_check_now, state_hold_false).""" + """With watch, the start check (state_check_now, state_hold_false) gives .old as None.""" pyscript.hass.states.async_set("pyscript.var1", "on") pyscript.hass.states.async_set("pyscript.var2", "on") await pyscript.start( @@ -910,6 +910,30 @@ def func2(var_name=None): await pyscript.wait_done2("pyscript.var2") +@pytest.mark.asyncio +async def test_wait_until_watch_other_old_is_none(pyscript): + """task.wait_until with watch also gives .old of another variable as None.""" + pyscript.hass.states.async_set("pyscript.var1", "off") + pyscript.hass.states.async_set("pyscript.var2", "on") + await pyscript.start( + """ +@time_trigger("startup") +def func1(): + res = task.wait_until( + state_trigger="pyscript.var1 == 'on' and pyscript.var2.old is None", + watch=["pyscript.var1"], + timeout=5, + __test_handshake__=["pyscript.done2", "ready"], + ) + pyscript.done = [res["trigger_type"], res["var_name"]] +""" + ) + + await pyscript.wait_done2("ready") + pyscript.hass.states.async_set("pyscript.var1", "on") + await pyscript.wait_done(["state", "pyscript.var1"]) + + @pytest.mark.asyncio async def test_time_active_hold_off_send_last(hass): """Test hold_off_send_last runs with the latest suppressed trigger data."""