Skip to content

Commit 9be9b12

Browse files
committed
time methods
1 parent 51fa8d7 commit 9be9b12

File tree

1 file changed

+58
-76
lines changed

1 file changed

+58
-76
lines changed

appdaemon/adapi.py

Lines changed: 58 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -2295,7 +2295,7 @@ async def fire_event(self, event: str, namespace: str | None = None, **kwargs) -
22952295
#
22962296

22972297
def parse_utc_string(self, utc_string: str) -> float:
2298-
"""Converts a UTC to its string representation.
2298+
"""Convert a UTC to its string representation.
22992299
23002300
Args:
23012301
utc_string (str): A string that contains a date and time to convert.
@@ -2306,7 +2306,7 @@ def parse_utc_string(self, utc_string: str) -> float:
23062306
"""
23072307
nums = list(map(
23082308
int,
2309-
re.split(r"[^\d]", utc_string)[:-1] # skip the last one for AM/PM
2309+
re.split(r"[^\d]", utc_string)[:-1] # split by anything that's not a number and skip the last part for AM/PM
23102310
))[:7] # Use a max of 7 parts
23112311
return dt.datetime(*nums).timestamp() + self.get_tz_offset() * 60
23122312

@@ -2414,10 +2414,9 @@ async def parse_time(
24142414
05:33:17
24152415
24162416
"""
2417-
name = name or self.name
24182417
return await self.AD.sched.parse_time(
24192418
time_str=time_str,
2420-
name=name,
2419+
name=name or self.name,
24212420
aware=aware,
24222421
today=today,
24232422
days_offset=days_offset
@@ -2480,10 +2479,9 @@ async def parse_datetime(
24802479
>>> self.parse_datetime("sunrise + 01:00:00")
24812480
2019-08-16 06:33:17
24822481
"""
2483-
name = name or self.name
24842482
return await self.AD.sched.parse_datetime(
24852483
time_str=time_str,
2486-
name=name,
2484+
name=name or self.name,
24872485
aware=aware,
24882486
today=today,
24892487
days_offset=days_offset
@@ -2495,14 +2493,11 @@ async def get_now(self, aware: bool = True) -> dt.datetime:
24952493
24962494
Examples:
24972495
>>> self.get_now()
2498-
2019-08-16 21:17:41.098813+00:00
2496+
2019-08-16 21:17:41.098813-04:00
24992497
25002498
"""
2501-
now = await self.AD.sched.get_now()
2502-
now = now.astimezone(self.AD.tz)
2503-
if not aware:
2504-
now = now.replace(tzinfo=None)
2505-
return now
2499+
now = await self.AD.sched.get_now()
2500+
return now.astimezone(self.AD.tz) if aware else self.AD.sched.make_naive(now)
25062501

25072502
@utils.sync_decorator
25082503
async def get_now_ts(self, aware: bool = False) -> float:
@@ -2516,54 +2511,40 @@ async def get_now_ts(self, aware: bool = False) -> float:
25162511
return (await self.get_now(aware)).timestamp()
25172512

25182513
@overload
2519-
async def now_is_between(
2520-
self,
2521-
start_time: str,
2522-
end_time: str,
2523-
name: str,
2524-
now: str
2525-
) -> bool: ... # fmt: skip
2514+
@utils.sync_decorator
2515+
async def now_is_between(self, start_time: str, end_time: str) -> bool: ...
25262516

25272517
@overload
2528-
async def now_is_between(
2529-
self,
2530-
start_time: str,
2531-
end_time: str,
2532-
name: str
2533-
) -> bool: ... # fmt: skip
2518+
@utils.sync_decorator
2519+
async def now_is_between(self, start_time: str, end_time: str, name: str) -> bool: ...
25342520

25352521
@overload
2536-
async def now_is_between(
2537-
self,
2538-
start_time: str,
2539-
end_time: str,
2540-
now: str
2541-
) -> bool: ... # fmt: skip
2522+
@utils.sync_decorator
2523+
async def now_is_between(self, start_time: str, end_time: str, now: str) -> bool: ...
25422524

2543-
@overload
2525+
@utils.sync_decorator
25442526
async def now_is_between(
25452527
self,
2546-
start_time: str,
2547-
end_time: str
2548-
) -> bool: ... # fmt: skip
2549-
2550-
@utils.sync_decorator
2551-
async def now_is_between(self, *args, **kwargs) -> bool:
2552-
"""Determines if the current `time` is within the specified start and end times.
2528+
start_time: str | dt.datetime,
2529+
end_time: str | dt.datetime,
2530+
name: str | None = None,
2531+
now: str | None = None
2532+
) -> bool:
2533+
"""Determine if the current `time` is within the specified start and end times.
25532534
2554-
This function takes two string representations of a ``time``, or ``sunrise`` or ``sunset``
2555-
offset and returns ``true`` if the current time is between those 2 times. Its
2556-
implementation can correctly handle transitions across midnight.
2535+
This function takes two string representations of a ``time`` ()or ``sunrise`` or ``sunset`` offset) and returns
2536+
``true`` if the current time is between those 2 times. Its implementation can correctly handle transitions
2537+
across midnight.
25572538
25582539
Args:
25592540
start_time (str): A string representation of the start time.
25602541
end_time (str): A string representation of the end time.
25612542
name (str, optional): Name of the calling app or module. It is used only for logging purposes.
2562-
now (str, optional): If specified, `now` is used as the time for comparison instead of the current time. Useful for testing.
2543+
now (str, optional): If specified, `now` is used as the time for comparison instead of the current time.
2544+
Useful for testing.
25632545
25642546
Returns:
2565-
bool: ``True`` if the current time is within the specified start and end times,
2566-
``False`` otherwise.
2547+
bool: ``True`` if the current time is within the specified start and end times, otherwise ``False``.
25672548
25682549
Notes:
25692550
The string representation of the ``start_time`` and ``end_time`` should follows
@@ -2583,23 +2564,24 @@ async def now_is_between(self, *args, **kwargs) -> bool:
25832564
>>> #do something
25842565
25852566
"""
2586-
return await self.AD.sched.now_is_between(*args, **kwargs)
2567+
return await self.AD.sched.now_is_between(start_time, end_time, name or self.name, now)
25872568

25882569
@utils.sync_decorator
25892570
async def sunrise(self, aware: bool = False, today: bool = False, days_offset: int = 0) -> dt.datetime:
2590-
"""Returns a `datetime` object that represents the next time Sunrise will occur.
2571+
"""Return a `datetime` object that represent when a sunrise will occur.
25912572
25922573
Args:
2593-
aware (bool, optional): Specifies if the created datetime object will be
2594-
`aware` of timezone or `not`.
2595-
today (bool, optional): Instead of the default behavior which is to return the next sunrise that will occur, setting this flag to true will return
2596-
today's sunrise even if it is in the past
2597-
days_offset (int, optional): Specify the number of days (positive or negative) for the sunset. This can only be used in combination with the today
2598-
flag
2574+
aware (bool, optional): Whether the resulting datetime object will be aware of timezone.
2575+
today (bool, optional): Defaults to ``False``, which will return the first sunrise in the future,
2576+
regardless of the day. If set to ``True``, the function will return the sunrise for the current day,
2577+
even if it is in the past.
2578+
days_offset (int, optional): Specify the number of days (positive or negative) for the sunrise. This can
2579+
only be used in combination with the today flag
25992580
26002581
Examples:
26012582
>>> self.sunrise()
26022583
2023-02-02 07:11:50.150554
2584+
26032585
>>> self.sunrise(today=True)
26042586
2023-02-01 07:12:20.272403
26052587
@@ -2608,20 +2590,20 @@ async def sunrise(self, aware: bool = False, today: bool = False, days_offset: i
26082590

26092591
@utils.sync_decorator
26102592
async def sunset(self, aware: bool = False, today: bool = False, days_offset: int = 0) -> dt.datetime:
2611-
"""Returns a `datetime` object that represents the next time Sunset will occur.
2593+
"""Return a `datetime` object that represent when a sunset will occur.
26122594
26132595
Args:
2614-
aware (bool, optional): Specifies if the created datetime object will be
2615-
`aware` of timezone or `not`.
2616-
today (bool, optional): Instead of the default behavior which is to return
2617-
the next sunset that will occur, setting this flag to true will return
2618-
today's sunset even if it is in the past
2619-
days_offset (int, optional): Specify the number of days (positive or negative)
2620-
for the sunset. This can only be used in combination with the today flag
2596+
aware (bool, optional): Whether the resulting datetime object will be aware of timezone.
2597+
today (bool, optional): Defaults to ``False``, which will return the first sunset in the future,
2598+
regardless of the day. If set to ``True``, the function will return the sunset for the current day,
2599+
even if it is in the past.
2600+
days_offset (int, optional): Specify the number of days (positive or negative) for the sunset. This can
2601+
only be used in combination with the today flag
26212602
26222603
Examples:
26232604
>>> self.sunset()
26242605
2023-02-01 18:09:00.730704
2606+
26252607
>>> self.sunset(today=True, days_offset=1)
26262608
2023-02-02 18:09:46.252314
26272609
@@ -2630,49 +2612,48 @@ async def sunset(self, aware: bool = False, today: bool = False, days_offset: in
26302612

26312613
@utils.sync_decorator
26322614
async def datetime(self, aware: bool = False) -> dt.datetime:
2633-
"""Returns a `datetime` object representing the current Local Date and Time.
2615+
"""Get a ``datetime`` object representing the current local date and time.
26342616
2635-
Use this in preference to the standard Python ways to discover the current
2636-
datetime, especially when using the "Time Travel" feature for testing.
2617+
Use this instead of the standard Python methods in order to correctly account for the time when using the time
2618+
travel feature, which is usually done for testing.
26372619
26382620
Args:
2639-
aware (bool, optional): Specifies if the created datetime object will be
2640-
`aware` of timezone or `not`.
2621+
aware (bool, optional): Whether the resulting datetime object will be aware of timezone.
26412622
26422623
Examples:
26432624
>>> self.datetime()
26442625
2019-08-15 20:15:55.549379
26452626
26462627
"""
2647-
return await self.get_now(aware)
2628+
return await self.get_now(aware=aware)
26482629

26492630
@utils.sync_decorator
26502631
async def time(self) -> dt.time:
2651-
"""Returns a localised `time` object representing the current Local Time.
2632+
"""Get a ``time`` object representing the current local time.
26522633
2653-
Use this in preference to the standard Python ways to discover the current time,
2654-
especially when using the "Time Travel" feature for testing.
2634+
Use this instead of the standard Python methods in order to correctly account for the time when using the time
2635+
travel feature, which is usually done for testing.
26552636
26562637
Examples:
26572638
>>> self.time()
26582639
20:15:31.295751
26592640
26602641
"""
2661-
return (await self.datetime(aware=True)).time()
2642+
return (await self.get_now(aware=True)).time()
26622643

26632644
@utils.sync_decorator
26642645
async def date(self) -> dt.date:
2665-
"""Returns a localised `date` object representing the current Local Date.
2646+
"""Get a ``date`` object representing the current local date.
26662647
2667-
Use this in preference to the standard Python ways to discover the current date,
2668-
especially when using the "Time Travel" feature for testing.
2648+
Use this instead of the standard Python methods in order to correctly account for the time when using the time
2649+
travel feature, which is usually done for testing.
26692650
26702651
Examples:
26712652
>>> self.date()
26722653
2019-08-15
26732654
26742655
"""
2675-
return (await self.datetime(aware=True)).date()
2656+
return (await self.get_now(aware=True)).date()
26762657

26772658
def get_timezone(self) -> str:
26782659
"""Returns the current time zone."""
@@ -2734,6 +2715,7 @@ async def reset_timer(self, handle: str) -> bool:
27342715
27352716
Examples:
27362717
>>> self.reset_timer(handle)
2718+
True
27372719
27382720
"""
27392721
self.logger.debug("Resetting timer with handle %s for %s", handle, self.name)
@@ -3428,7 +3410,7 @@ async def run_at_sunrise(
34283410
>>> self.run_at_sunrise(self.sun, random_start = -60*60, random_end = 30*60)
34293411
34303412
"""
3431-
sunrise = await self.AD.sched.next_sunrise()
3413+
sunrise = self.AD.sched.get_sunrise(today=False, aware=True)
34323414
td = timedelta(seconds=float(offset)) if offset is not None else timedelta()
34333415
self.logger.debug(f"Registering run_at_sunrise at {sunrise+td} with {args}, {kwargs}")
34343416
return await self.AD.sched.insert_schedule(

0 commit comments

Comments
 (0)