From 6b4ea768563784d0fd9c47970d32df82f12b1660 Mon Sep 17 00:00:00 2001 From: Craig Barratt <19445341+craigbarratt@users.noreply.github.com> Date: Mon, 7 Mar 2022 14:06:34 -0800 Subject: [PATCH] allow once() to have offsets that fall before or after today --- custom_components/pyscript/trigger.py | 7 ++++--- tests/test_unit_trigger.py | 4 ++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/custom_components/pyscript/trigger.py b/custom_components/pyscript/trigger.py index f0372a7..15a524b 100644 --- a/custom_components/pyscript/trigger.py +++ b/custom_components/pyscript/trigger.py @@ -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 diff --git a/tests/test_unit_trigger.py b/tests/test_unit_trigger.py index 1e68cb9..4550fd8 100644 --- a/tests/test_unit_trigger.py +++ b/tests/test_unit_trigger.py @@ -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)]],