Skip to content
Open
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
11 changes: 11 additions & 0 deletions custom_components/pyscript/decorators/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)

Expand All @@ -197,6 +199,12 @@ async def validate(self) -> None:

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()
Expand Down Expand Up @@ -359,6 +367,9 @@ async def _cycle(self) -> None:

async def _is_trig_ok(self, new_vars: dict[str, Any]) -> bool:
if self.has_expression():
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

Expand Down
92 changes: 92 additions & 0 deletions tests/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,98 @@ 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, .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(
"""
@state_trigger(
"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, 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",
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 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 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 on")

# 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):
"""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(
"""
@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_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."""
Expand Down
Loading