Skip to content

Commit 971a9d6

Browse files
authored
Merge pull request #2526 from AppDaemon/start-now
patch for get_next_period and start == "now"
2 parents 943eadd + f33a82a commit 971a9d6

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

appdaemon/adapi.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@
1414
from pathlib import Path
1515
from typing import TYPE_CHECKING, Any, Literal, TypeVar, overload
1616

17-
from appdaemon import dependency
17+
from appdaemon import dependency, utils
1818
from appdaemon import exceptions as ade
19-
from appdaemon import utils
2019
from appdaemon.appdaemon import AppDaemon
2120
from appdaemon.entity import Entity
2221
from appdaemon.events import EventCallback
2322
from appdaemon.logging import Logging
2423
from appdaemon.models.config.app import AppConfig
2524
from appdaemon.parse import resolve_time_str
2625
from appdaemon.state import StateCallbackType
26+
2727
from .types import TimeDeltaLike
2828

2929
T = TypeVar("T")
@@ -3225,6 +3225,7 @@ async def run_every(
32253225
start: str | dt.time | dt.datetime | None = None,
32263226
interval: TimeDeltaLike = 0,
32273227
*args,
3228+
immediate: bool = False,
32283229
random_start: TimeDeltaLike | None = None,
32293230
random_end: TimeDeltaLike | None = None,
32303231
pin: bool | None = None,
@@ -3257,6 +3258,8 @@ async def run_every(
32573258
- If this is a ``timedelta`` object, the current date will be assumed.
32583259
32593260
*args: Arbitrary positional arguments to be provided to the callback function when it is triggered.
3261+
immediate (bool, optional): Whether to immediately fire the callback or wait until the first interval if the
3262+
start time is now.
32603263
random_start (int, optional): Start of range of the random time.
32613264
random_end (int, optional): End of range of the random time.
32623265
pin (bool, optional): Optional setting to override the default thread pinning behavior. By default, this is
@@ -3310,7 +3313,7 @@ def timed_callback(self, **kwargs): ... # example callback
33103313
33113314
"""
33123315
interval = utils.parse_timedelta(interval)
3313-
next_period = await self.AD.sched.get_next_period(interval, start)
3316+
next_period = await self.AD.sched.get_next_period(interval, start, immediate=immediate)
33143317

33153318
self.logger.debug(
33163319
"Registering %s for run_every in %s intervals, starting %s",

appdaemon/scheduler.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,8 @@ async def get_next_period(
483483
self,
484484
interval: TimeDeltaLike,
485485
start: time | datetime | str | None = None,
486+
*,
487+
immediate: bool = False,
486488
) -> datetime:
487489
interval = utils.parse_timedelta(interval)
488490
start = "now" if start is None else start
@@ -493,7 +495,7 @@ async def get_next_period(
493495
assert isinstance(aware_start, datetime) and aware_start.tzinfo is not None
494496

495497
# Skip forward to the next period if start is in the past
496-
while aware_start < now:
498+
while aware_start < now or (immediate and aware_start <= now):
497499
aware_start += interval
498500

499501
return aware_start
@@ -901,7 +903,7 @@ async def parse_datetime(
901903
# Need to force timezone during time-travel mode
902904
if now is None:
903905
now = await self.get_now()
904-
now = now.astimezone(self.AD.tz)
906+
now = utils.ensure_timezone(now, self.AD.tz)
905907
return parse.parse_datetime(
906908
input_=input_,
907909
now=now,

docs/HISTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- Reload modified apps on SIGUSR2 - contributed by [chatziko](https://github.com/chatziko)
1010
- Using urlib to create endpoints from URLs - contributed by [cebtenzzre](https://github.com/cebtenzzre)
1111
- Added {py:meth}`~appdaemon.plugins.hass.hassapi.Hass.process_conversation` and {py:meth}`~appdaemon.plugins.hass.hassapi.Hass.reload_conversation` to the {ref}`Hass API <hass-api-usage>`.
12+
- Added `immediate` kwargs to `run_every` to control semantics around `start == "now"`
1213

1314
**Fixes**
1415

0 commit comments

Comments
 (0)