@@ -136,15 +136,15 @@ def init(cls, hass):
136
136
cls .hass = hass
137
137
138
138
def wait_until_factory (ast_ctx ):
139
- """Return wapper to call to astFunction with the ast context."""
139
+ """Return wrapper to call to astFunction with the ast context."""
140
140
141
141
async def wait_until_call (* arg , ** kw ):
142
142
return await cls .wait_until (ast_ctx , * arg , ** kw )
143
143
144
144
return wait_until_call
145
145
146
146
def user_task_create_factory (ast_ctx ):
147
- """Return wapper to call to astFunction with the ast context."""
147
+ """Return wrapper to call to astFunction with the ast context."""
148
148
149
149
async def user_task_create (func , * args , ** kwargs ):
150
150
"""Implement task.create()."""
@@ -610,6 +610,7 @@ async def parse_date_time(cls, date_time_str, day_offset, now, startup_time):
610
610
year = now .year
611
611
month = now .month
612
612
day = now .day
613
+ fixed_date = False
613
614
614
615
dt_str_orig = dt_str = date_time_str .strip ().lower ()
615
616
#
@@ -623,6 +624,7 @@ async def parse_date_time(cls, date_time_str, day_offset, now, startup_time):
623
624
else :
624
625
month , day = int (match0 [1 ]), int (match0 [2 ])
625
626
day_offset = 0 # explicit date means no offset
627
+ fixed_date = True
626
628
dt_str = dt_str [len (match0 .group (0 )) :]
627
629
elif match1 :
628
630
skip = True
@@ -632,11 +634,17 @@ async def parse_date_time(cls, date_time_str, day_offset, now, startup_time):
632
634
day_offset = dow - (now .isoweekday () % 7 )
633
635
else :
634
636
day_offset = 7 + dow - (now .isoweekday () % 7 )
637
+ fixed_date = True
635
638
elif match1 [1 ] == "today" :
636
639
day_offset = 0
640
+ fixed_date = True
637
641
elif match1 [1 ] == "tomorrow" :
638
642
day_offset = 1
643
+ fixed_date = True
639
644
else :
645
+ if match1 [1 ] == "now" :
646
+ day_offset = 0
647
+ fixed_date = True
640
648
skip = False
641
649
if skip :
642
650
dt_str = dt_str [len (match1 .group (0 )) :]
@@ -649,7 +657,7 @@ async def parse_date_time(cls, date_time_str, day_offset, now, startup_time):
649
657
now = dt .datetime (year , month , day )
650
658
dt_str = dt_str .strip ()
651
659
if len (dt_str ) == 0 :
652
- return now
660
+ return now , fixed_date
653
661
654
662
#
655
663
# parse the time
@@ -682,7 +690,7 @@ async def parse_date_time(cls, date_time_str, day_offset, now, startup_time):
682
690
except Exception :
683
691
_LOGGER .warning ("'%s' not defined at this latitude" , dt_str )
684
692
# return something in the past so it is ignored
685
- return now - dt .timedelta (days = 100 )
693
+ return now - dt .timedelta (days = 100 ), fixed_date
686
694
now += time_sun .date () - now .date ()
687
695
hour , mins , sec = time_sun .hour , time_sun .minute , time_sun .second
688
696
elif dt_str .startswith ("noon" ):
@@ -707,7 +715,7 @@ async def parse_date_time(cls, date_time_str, day_offset, now, startup_time):
707
715
dt_str = dt_str .strip ()
708
716
if len (dt_str ) > 0 :
709
717
now = now + dt .timedelta (seconds = parse_time_offset (dt_str ))
710
- return now
718
+ return now , fixed_date
711
719
712
720
@classmethod
713
721
async def timer_active_check (cls , time_spec , now , startup_time ):
@@ -737,8 +745,8 @@ async def timer_active_check(cls, time_spec, now, startup_time):
737
745
_LOGGER .error ("Invalid range expression: %s" , exc )
738
746
return False
739
747
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 )
748
+ start , _ = await cls .parse_date_time (dt_start .strip (), 0 , now , startup_time )
749
+ end , _ = await cls .parse_date_time (dt_end .strip (), 0 , start , startup_time )
742
750
743
751
if start <= end :
744
752
this_match = start <= now <= end
@@ -802,20 +810,20 @@ async def timer_trigger_next(cls, time_spec, now, startup_time):
802
810
next_time_adj = now + delta
803
811
804
812
elif len (match1 ) == 3 :
805
- this_t = await cls .parse_date_time (match1 [1 ].strip (), 0 , now , startup_time )
813
+ this_t , _ = await cls .parse_date_time (match1 [1 ].strip (), 0 , now , startup_time )
806
814
day_offset = (now - this_t ).days + 1
807
815
if day_offset != 0 and this_t != startup_time :
808
816
#
809
817
# Try a day offset (won't make a difference if spec has full date)
810
818
#
811
- this_t = await cls .parse_date_time (match1 [1 ].strip (), day_offset , now , startup_time )
819
+ this_t , _ = await cls .parse_date_time (match1 [1 ].strip (), day_offset , now , startup_time )
812
820
startup = now == this_t and now == startup_time
813
821
if (now < this_t or startup ) and (next_time is None or this_t < next_time ):
814
822
next_time_adj = next_time = this_t
815
823
816
824
elif len (match2 ) == 5 :
817
825
start_str , period_str = match2 [1 ].strip (), match2 [2 ].strip ()
818
- start = await cls .parse_date_time (start_str , 0 , now , startup_time )
826
+ start , fixed_date_start = await cls .parse_date_time (start_str , 0 , now , startup_time )
819
827
period = parse_time_offset (period_str )
820
828
if period <= 0 :
821
829
_LOGGER .error ("Invalid non-positive period %s in period(): %s" , period , time_spec )
@@ -832,12 +840,17 @@ async def timer_trigger_next(cls, time_spec, now, startup_time):
832
840
next_time_adj = next_time = this_t
833
841
continue
834
842
end_str = match2 [3 ].strip ()
835
- end = await cls .parse_date_time (end_str , 0 , now , startup_time )
836
- end_offset = 1 if end < start else 0
837
- for day in [- 1 , 0 , 1 ]:
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 )
840
- if now < start or (now == start and now == startup_time ):
843
+ end , fixed_date_end = await cls .parse_date_time (end_str , 0 , now , startup_time )
844
+ if not fixed_date_start and not fixed_date_end :
845
+ end_offset = 1 if end < start else 0
846
+ day_dither = [- 1 , 0 , 1 ]
847
+ else :
848
+ end_offset = 0
849
+ day_dither = [0 ]
850
+ for day in day_dither :
851
+ start , _ = await cls .parse_date_time (start_str , day , now , startup_time )
852
+ end , _ = await cls .parse_date_time (end_str , day + end_offset , now , startup_time )
853
+ if (now < start or (now == start and now == startup_time )) and start <= end :
841
854
if next_time is None or start < next_time :
842
855
next_time_adj = next_time = start
843
856
break
0 commit comments