Skip to content

Commit a1c02d1

Browse files
committed
added a couple more tests and docs for state_check_now
1 parent 5cd60b7 commit a1c02d1

File tree

2 files changed

+41
-22
lines changed

2 files changed

+41
-22
lines changed

docs/reference.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,9 @@ Optional arguments are:
227227
to ``True`` or non-zero. Normally the expression is only evaluated when a state variable
228228
changes, and not when the trigger function is first defined. This option is the same as
229229
in the ``task.wait_until`` function, except the default value is ``True`` in that case.
230+
Note that if you specify ``state_check_now=True``, entries in ``state_trigger`` that are
231+
plain state variable names (which mean trigger on any change) are ignored during the initial
232+
check - only expressions are checked.
230233

231234
``state_hold=None``
232235
A numeric duration in seconds that delays executing the trigger function for this amount of time.
@@ -761,8 +764,8 @@ It takes the following keyword arguments (all are optional):
761764
immediately to see if it is already ``True``, and will return immediately if so. If
762765
``state_check_now=False``, ``task.wait_until()`` waits until a state variable change occurs,
763766
before checking the expression. Using ``True`` is safer to help avoid race conditions, although
764-
``False`` makes ``task.wait_until()`` behave like ``@state_trigger``, which doesn’t check at
765-
startup. However, if you use the default of ``True``, and your function will call
767+
``False`` makes ``task.wait_until()`` behave like ``@state_trigger``, which by default doesn’t check
768+
at startup. However, if you use the default of ``True``, and your function will call
766769
``task.wait_until()`` again, it’s recommended you set that state variable to some other value
767770
immediately after ``task.wait_until()`` returns. Otherwise the next call will also return
768771
immediately. Note that entries in ``state_trigger`` that are plain state variable names

tests/test_function.py

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,8 @@ async def test_state_trigger_check_now(hass, caplog):
688688
"""Test state trigger."""
689689
notify_q = asyncio.Queue(0)
690690

691-
hass.states.async_set("pyscript.fstartup", 1)
691+
hass.states.async_set("pyscript.fstartup0", 1)
692+
hass.states.async_set("pyscript.fstartup2", 0)
692693

693694
await setup_script(
694695
hass,
@@ -699,27 +700,42 @@ async def test_state_trigger_check_now(hass, caplog):
699700
from math import sqrt
700701
from homeassistant.core import Context
701702
702-
seq_num = 0
703-
704-
pyscript.fstartup = 1
705-
706-
@state_trigger("pyscript.fstartup == '1'", state_check_now=True)
707-
def func_startup_sync(trigger_type=None, var_name=None):
708-
global seq_num
709-
710-
seq_num += 1
711-
log.info(f"func_startup_sync setting pyscript.done={seq_num}, trigger_type={trigger_type}, var_name={var_name}")
712-
pyscript.done = [seq_num, trigger_type, var_name]
703+
# should trigger immediately
704+
@state_trigger("pyscript.fstartup0 == '1'", state_check_now=True)
705+
def func_startup_sync0(trigger_type=None, var_name=None):
706+
log.info(f"func_startup_sync0 setting pyscript.done=0, trigger_type={trigger_type}, var_name={var_name}")
707+
pyscript.done = [0, trigger_type, var_name]
708+
709+
# should trigger immediately
710+
@state_trigger("pyscript.fstartup2 == '0'", state_check_now=True)
711+
def func_startup_sync1(trigger_type=None, var_name=None):
712+
log.info(f"func_startup_sync1 setting pyscript.done=1, trigger_type={trigger_type}, var_name={var_name}")
713+
pyscript.done = [1, trigger_type, var_name]
714+
715+
# shouldn't trigger immediately
716+
@state_trigger("pyscript.fstartup2 == '1'", state_check_now=True)
717+
def func_startup_sync2(trigger_type=None, var_name=None):
718+
log.info(f"func_startup_sync2 setting pyscript.done=2, trigger_type={trigger_type}, var_name={var_name}")
719+
pyscript.done = [2, trigger_type, var_name]
713720
""",
714721
)
715-
seq_num = 0
716722

717-
seq_num += 1
718-
# fire event to start triggers, and handshake when they are running
719723
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
720-
assert literal_eval(await wait_until_done(notify_q)) == [seq_num, "state", None]
724+
#
725+
# we should get two results, although they could be in any order
726+
#
727+
results = [None, None]
728+
for _ in range(2):
729+
res = literal_eval(await wait_until_done(notify_q))
730+
results[res[0]] = res
721731

722-
seq_num += 1
723-
hass.states.async_set("pyscript.fstartup", 0)
724-
hass.states.async_set("pyscript.fstartup", 1)
725-
assert literal_eval(await wait_until_done(notify_q)) == [seq_num, "state", "pyscript.fstartup"]
732+
assert results == [[0, "state", None], [1, "state", None]]
733+
734+
for _ in range(2):
735+
hass.states.async_set("pyscript.fstartup2", 10)
736+
hass.states.async_set("pyscript.fstartup2", 1)
737+
assert literal_eval(await wait_until_done(notify_q)) == [2, "state", "pyscript.fstartup2"]
738+
739+
hass.states.async_set("pyscript.fstartup0", 0)
740+
hass.states.async_set("pyscript.fstartup0", 1)
741+
assert literal_eval(await wait_until_done(notify_q)) == [0, "state", "pyscript.fstartup0"]

0 commit comments

Comments
 (0)