1616from .const import LOGGER_PATH
1717from .eval import AstEval
1818from .event import Event
19- from .mqtt import Mqtt
2019from .function import Function
20+ from .mqtt import Mqtt
2121from .state import STATE_VIRTUAL_ATTRS , State
2222
2323_LOGGER = logging .getLogger (LOGGER_PATH + ".trigger" )
@@ -173,12 +173,8 @@ async def wait_until(
173173 last_state_trig_time = None
174174 state_trig_waiting = False
175175 state_trig_notify_info = [None , None ]
176-
177- #
178- # at startup we start our state_hold_false window,
179- # although it could get updated if state_check_now is set.
180- #
181- state_false_time = time .monotonic ()
176+ state_false_time = None
177+ check_state_expr_on_start = state_check_now or state_hold_false is not None
182178
183179 if state_trigger is not None :
184180 state_trig = []
@@ -214,16 +210,16 @@ async def wait_until(
214210 raise exc
215211
216212 state_trig_ident .update (state_trig_ident_any )
217- if state_check_now and state_trig_eval :
213+ if check_state_expr_on_start and state_trig_eval :
218214 #
219- # check straight away to see if the condition is met (to avoid race conditions)
215+ # check straight away to see if the condition is met
220216 #
221217 new_vars = State .notify_var_get (state_trig_ident , {})
222218 state_trig_ok = await state_trig_eval .eval (new_vars )
223219 exc = state_trig_eval .get_exception_obj ()
224220 if exc is not None :
225221 raise exc
226- if state_hold_false is not None :
222+ if state_hold_false is not None and not state_check_now :
227223 #
228224 # if state_trig_ok we wait until it is false;
229225 # otherwise we consider now to be the start of the false hold time
@@ -817,17 +813,14 @@ async def trigger_watch(self):
817813 Event .notify_add (self .event_trigger [0 ], self .notify_q )
818814 if self .mqtt_trigger is not None :
819815 _LOGGER .debug ("trigger %s adding mqtt_trigger %s" , self .name , self .mqtt_trigger [0 ])
820- await Mqtt .notify_add (self .mqtt_trigger [0 ], self .notify_q )
816+ await Mqtt .notify_add (self .mqtt_trigger [0 ], self .notify_q )
821817
822818 last_trig_time = None
823819 last_state_trig_time = None
824820 state_trig_waiting = False
825821 state_trig_notify_info = [None , None ]
826- #
827- # at startup we start our state_hold_false window,
828- # although it could get updated if state_check_now is set.
829- #
830- state_false_time = time .monotonic ()
822+ state_false_time = None
823+ check_state_expr_on_start = self .state_check_now or self .state_hold_false is not None
831824
832825 while True :
833826 timeout = None
@@ -841,7 +834,7 @@ async def trigger_watch(self):
841834 notify_type = "startup"
842835 notify_info = {"trigger_type" : "time" , "trigger_time" : None }
843836 self .run_on_startup = False
844- elif self . state_check_now :
837+ elif check_state_expr_on_start :
845838 #
846839 # first time only - skip wait and check state trigger
847840 #
@@ -851,7 +844,7 @@ async def trigger_watch(self):
851844 else :
852845 notify_vars = {}
853846 notify_info = [notify_vars , {"trigger_type" : notify_type }]
854- self . state_check_now = False
847+ check_state_expr_on_start = False
855848 else :
856849 if self .time_trigger :
857850 now = dt_now ()
@@ -900,7 +893,7 @@ async def trigger_watch(self):
900893
901894 if not ident_any_values_changed (func_args , self .state_trig_ident_any ):
902895 #
903- # if var_name not in func_args we are state_check_now
896+ # if var_name not in func_args we are check_state_expr_on_start
904897 #
905898 if "var_name" in func_args and not ident_values_changed (
906899 func_args , self .state_trig_ident
@@ -917,23 +910,26 @@ async def trigger_watch(self):
917910 if self .state_hold_false is not None :
918911 if "var_name" not in func_args :
919912 #
920- # this is state_check_now check
913+ # this is check_state_expr_on_start check
921914 # if immediately true, force wait until False
922915 # otherwise start False wait now
923916 #
924917 state_false_time = None if trig_ok else time .monotonic ()
925- continue
918+ if not self .state_check_now :
919+ continue
926920 if state_false_time is None :
927921 if trig_ok :
928922 #
929- # wasn't False, so ignore
923+ # wasn't False, so ignore after initial check
930924 #
931- continue
932- #
933- # first False, so remember when it is
934- #
935- state_false_time = time .monotonic ()
936- elif trig_ok :
925+ if "var_name" in func_args :
926+ continue
927+ else :
928+ #
929+ # first False, so remember when it is
930+ #
931+ state_false_time = time .monotonic ()
932+ elif trig_ok and "var_name" in func_args :
937933 too_soon = time .monotonic () - state_false_time < self .state_hold_false
938934 state_false_time = None
939935 if too_soon :
0 commit comments