Skip to content

Commit

Permalink
allow once() to have offsets that fall before or after today
Browse files Browse the repository at this point in the history
  • Loading branch information
craigbarratt committed Mar 7, 2022
1 parent a216aea commit 6b4ea76
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
7 changes: 4 additions & 3 deletions custom_components/pyscript/trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,11 +717,12 @@ def timer_trigger_next(cls, time_spec, now, startup_time):

elif len(match1) == 3:
this_t = cls.parse_date_time(match1[1].strip(), 0, now, startup_time)
if this_t <= now and this_t != startup_time:
day_offset = (now - this_t).days + 1
if day_offset != 0 and this_t != startup_time:
#
# Try tomorrow (won't make a difference if spec has full date)
# Try a day offset (won't make a difference if spec has full date)
#
this_t = cls.parse_date_time(match1[1].strip(), 1, now, startup_time)
this_t = cls.parse_date_time(match1[1].strip(), day_offset, now, startup_time)
startup = now == this_t and now == startup_time
if (now < this_t or startup) and (next_time is None or this_t < next_time):
next_time = this_t
Expand Down
4 changes: 4 additions & 0 deletions tests/test_unit_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ def test_timer_active_check(hass, spec, now, expected):
[["once(2019/9/1 8:00)"], [None]],
[["once(2019/9/1 15:00)"], [dt(2019, 9, 1, 15, 0, 0, 0)]],
[["once(15:00)"], [dt(2019, 9, 1, 15, 0, 0, 0)]],
[["once(15:00 + 2d)"], [dt(2019, 9, 1, 15, 0, 0, 0), dt(2019, 9, 2, 15, 0, 0, 0)]],
[["once(15:00 - 48h)"], [dt(2019, 9, 1, 15, 0, 0, 0), dt(2019, 9, 2, 15, 0, 0, 0)]],
[["once(11:00 + 2d)"], [dt(2019, 9, 2, 11, 0, 0, 0), dt(2019, 9, 3, 11, 0, 0, 0)]],
[["once(11:00 - 48h)"], [dt(2019, 9, 2, 11, 0, 0, 0), dt(2019, 9, 3, 11, 0, 0, 0)]],
[["once(13:00:0.09)"], [dt(2019, 9, 2, 13, 0, 0, 90000)]],
[["once(9:00)"], [dt(2019, 9, 2, 9, 0, 0, 0)]],
[["once(wed 9:00)"], [dt(2019, 9, 4, 9, 0, 0, 0)]],
Expand Down

0 comments on commit 6b4ea76

Please sign in to comment.