@@ -688,7 +688,8 @@ async def test_state_trigger_check_now(hass, caplog):
688
688
"""Test state trigger."""
689
689
notify_q = asyncio .Queue (0 )
690
690
691
- hass .states .async_set ("pyscript.fstartup" , 1 )
691
+ hass .states .async_set ("pyscript.fstartup0" , 1 )
692
+ hass .states .async_set ("pyscript.fstartup2" , 0 )
692
693
693
694
await setup_script (
694
695
hass ,
@@ -699,27 +700,42 @@ async def test_state_trigger_check_now(hass, caplog):
699
700
from math import sqrt
700
701
from homeassistant.core import Context
701
702
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]
713
720
""" ,
714
721
)
715
- seq_num = 0
716
722
717
- seq_num += 1
718
- # fire event to start triggers, and handshake when they are running
719
723
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
721
731
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