@@ -400,7 +400,7 @@ async def wait_until(
400400 if startup_time is None :
401401 startup_time = now
402402 if time_trigger is not None :
403- time_next , time_next_adj = cls .timer_trigger_next (time_trigger , now , startup_time )
403+ time_next , time_next_adj = await cls .timer_trigger_next (time_trigger , now , startup_time )
404404 _LOGGER .debug (
405405 "trigger %s wait_until time_next = %s, now = %s" ,
406406 ast_ctx .name ,
@@ -605,7 +605,7 @@ async def user_task_executor(cls, func, *args, **kwargs):
605605 return await cls .hass .async_add_executor_job (functools .partial (func , ** kwargs ), * args )
606606
607607 @classmethod
608- def parse_date_time (cls , date_time_str , day_offset , now , startup_time ):
608+ async def parse_date_time (cls , date_time_str , day_offset , now , startup_time ):
609609 """Parse a date time string, returning datetime."""
610610 year = now .year
611611 month = now .month
@@ -670,10 +670,14 @@ def parse_date_time(cls, date_time_str, day_offset, now, startup_time):
670670 location = location [0 ]
671671 try :
672672 if dt_str .startswith ("sunrise" ):
673- time_sun = location .sunrise (dt .date (year , month , day ))
673+ time_sun = await cls .hass .async_add_executor_job (
674+ location .sunrise , dt .date (year , month , day )
675+ )
674676 dt_str = dt_str [7 :]
675677 else :
676- time_sun = location .sunset (dt .date (year , month , day ))
678+ time_sun = await cls .hass .async_add_executor_job (
679+ location .sunset , dt .date (year , month , day )
680+ )
677681 dt_str = dt_str [6 :]
678682 except Exception :
679683 _LOGGER .warning ("'%s' not defined at this latitude" , dt_str )
@@ -706,7 +710,7 @@ def parse_date_time(cls, date_time_str, day_offset, now, startup_time):
706710 return now
707711
708712 @classmethod
709- def timer_active_check (cls , time_spec , now , startup_time ):
713+ async def timer_active_check (cls , time_spec , now , startup_time ):
710714 """Check if the given time matches the time specification."""
711715 results = {"+" : [], "-" : []}
712716 for entry in time_spec if isinstance (time_spec , list ) else [time_spec ]:
@@ -733,8 +737,8 @@ def timer_active_check(cls, time_spec, now, startup_time):
733737 _LOGGER .error ("Invalid range expression: %s" , exc )
734738 return False
735739
736- start = cls .parse_date_time (dt_start .strip (), 0 , now , startup_time )
737- end = cls .parse_date_time (dt_end .strip (), 0 , start , startup_time )
740+ start = await cls .parse_date_time (dt_start .strip (), 0 , now , startup_time )
741+ end = await cls .parse_date_time (dt_end .strip (), 0 , start , startup_time )
738742
739743 if start <= end :
740744 this_match = start <= now <= end
@@ -755,7 +759,7 @@ def timer_active_check(cls, time_spec, now, startup_time):
755759 return result
756760
757761 @classmethod
758- def timer_trigger_next (cls , time_spec , now , startup_time ):
762+ async def timer_trigger_next (cls , time_spec , now , startup_time ):
759763 """Return the next trigger time based on the given time and time specification."""
760764 next_time = None
761765 next_time_adj = None
@@ -798,20 +802,20 @@ def timer_trigger_next(cls, time_spec, now, startup_time):
798802 next_time_adj = now + delta
799803
800804 elif len (match1 ) == 3 :
801- this_t = cls .parse_date_time (match1 [1 ].strip (), 0 , now , startup_time )
805+ this_t = await cls .parse_date_time (match1 [1 ].strip (), 0 , now , startup_time )
802806 day_offset = (now - this_t ).days + 1
803807 if day_offset != 0 and this_t != startup_time :
804808 #
805809 # Try a day offset (won't make a difference if spec has full date)
806810 #
807- this_t = cls .parse_date_time (match1 [1 ].strip (), day_offset , now , startup_time )
811+ this_t = await cls .parse_date_time (match1 [1 ].strip (), day_offset , now , startup_time )
808812 startup = now == this_t and now == startup_time
809813 if (now < this_t or startup ) and (next_time is None or this_t < next_time ):
810814 next_time_adj = next_time = this_t
811815
812816 elif len (match2 ) == 5 :
813817 start_str , period_str = match2 [1 ].strip (), match2 [2 ].strip ()
814- start = cls .parse_date_time (start_str , 0 , now , startup_time )
818+ start = await cls .parse_date_time (start_str , 0 , now , startup_time )
815819 period = parse_time_offset (period_str )
816820 if period <= 0 :
817821 _LOGGER .error ("Invalid non-positive period %s in period(): %s" , period , time_spec )
@@ -828,11 +832,11 @@ def timer_trigger_next(cls, time_spec, now, startup_time):
828832 next_time_adj = next_time = this_t
829833 continue
830834 end_str = match2 [3 ].strip ()
831- end = cls .parse_date_time (end_str , 0 , now , startup_time )
835+ end = await cls .parse_date_time (end_str , 0 , now , startup_time )
832836 end_offset = 1 if end < start else 0
833837 for day in [- 1 , 0 , 1 ]:
834- start = cls .parse_date_time (start_str , day , now , startup_time )
835- end = cls .parse_date_time (end_str , day + end_offset , now , startup_time )
838+ start = await cls .parse_date_time (start_str , day , now , startup_time )
839+ end = await cls .parse_date_time (end_str , day + end_offset , now , startup_time )
836840 if now < start or (now == start and now == startup_time ):
837841 if next_time is None or start < next_time :
838842 next_time_adj = next_time = start
@@ -1108,7 +1112,7 @@ async def trigger_watch(self):
11081112 check_state_expr_on_start = False
11091113 else :
11101114 if self .time_trigger :
1111- time_next , time_next_adj = TrigTime .timer_trigger_next (
1115+ time_next , time_next_adj = await TrigTime .timer_trigger_next (
11121116 self .time_trigger , now , startup_time
11131117 )
11141118 _LOGGER .debug (
@@ -1279,7 +1283,7 @@ async def trigger_watch(self):
12791283 self .active_expr .get_logger ().error (exc )
12801284 trig_ok = False
12811285 if trig_ok and self .time_active :
1282- trig_ok = TrigTime .timer_active_check (self .time_active , now , startup_time )
1286+ trig_ok = await TrigTime .timer_active_check (self .time_active , now , startup_time )
12831287
12841288 if not trig_ok :
12851289 _LOGGER .debug (
0 commit comments