16
16
from .const import LOGGER_PATH
17
17
from .eval import AstEval
18
18
from .event import Event
19
- from .mqtt import Mqtt
20
19
from .function import Function
20
+ from .mqtt import Mqtt
21
21
from .state import STATE_VIRTUAL_ATTRS , State
22
22
23
23
_LOGGER = logging .getLogger (LOGGER_PATH + ".trigger" )
@@ -173,12 +173,8 @@ async def wait_until(
173
173
last_state_trig_time = None
174
174
state_trig_waiting = False
175
175
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
182
178
183
179
if state_trigger is not None :
184
180
state_trig = []
@@ -214,16 +210,16 @@ async def wait_until(
214
210
raise exc
215
211
216
212
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 :
218
214
#
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
220
216
#
221
217
new_vars = State .notify_var_get (state_trig_ident , {})
222
218
state_trig_ok = await state_trig_eval .eval (new_vars )
223
219
exc = state_trig_eval .get_exception_obj ()
224
220
if exc is not None :
225
221
raise exc
226
- if state_hold_false is not None :
222
+ if state_hold_false is not None and not state_check_now :
227
223
#
228
224
# if state_trig_ok we wait until it is false;
229
225
# otherwise we consider now to be the start of the false hold time
@@ -817,17 +813,14 @@ async def trigger_watch(self):
817
813
Event .notify_add (self .event_trigger [0 ], self .notify_q )
818
814
if self .mqtt_trigger is not None :
819
815
_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 )
821
817
822
818
last_trig_time = None
823
819
last_state_trig_time = None
824
820
state_trig_waiting = False
825
821
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
831
824
832
825
while True :
833
826
timeout = None
@@ -841,7 +834,7 @@ async def trigger_watch(self):
841
834
notify_type = "startup"
842
835
notify_info = {"trigger_type" : "time" , "trigger_time" : None }
843
836
self .run_on_startup = False
844
- elif self . state_check_now :
837
+ elif check_state_expr_on_start :
845
838
#
846
839
# first time only - skip wait and check state trigger
847
840
#
@@ -851,7 +844,7 @@ async def trigger_watch(self):
851
844
else :
852
845
notify_vars = {}
853
846
notify_info = [notify_vars , {"trigger_type" : notify_type }]
854
- self . state_check_now = False
847
+ check_state_expr_on_start = False
855
848
else :
856
849
if self .time_trigger :
857
850
now = dt_now ()
@@ -900,7 +893,7 @@ async def trigger_watch(self):
900
893
901
894
if not ident_any_values_changed (func_args , self .state_trig_ident_any ):
902
895
#
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
904
897
#
905
898
if "var_name" in func_args and not ident_values_changed (
906
899
func_args , self .state_trig_ident
@@ -917,23 +910,26 @@ async def trigger_watch(self):
917
910
if self .state_hold_false is not None :
918
911
if "var_name" not in func_args :
919
912
#
920
- # this is state_check_now check
913
+ # this is check_state_expr_on_start check
921
914
# if immediately true, force wait until False
922
915
# otherwise start False wait now
923
916
#
924
917
state_false_time = None if trig_ok else time .monotonic ()
925
- continue
918
+ if not self .state_check_now :
919
+ continue
926
920
if state_false_time is None :
927
921
if trig_ok :
928
922
#
929
- # wasn't False, so ignore
923
+ # wasn't False, so ignore after initial check
930
924
#
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 :
937
933
too_soon = time .monotonic () - state_false_time < self .state_hold_false
938
934
state_false_time = None
939
935
if too_soon :
0 commit comments