[#340] Added Alarm.is_cron property.

This commit is contained in:
Fabio Manganiello 2023-12-10 20:58:13 +01:00
parent 2d8f6102c1
commit 03c167d6b7
Signed by: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -99,6 +99,17 @@ class Alarm:
return media return media
@property
def is_cron(self) -> bool:
if not isinstance(self.when, str):
return False
try:
croniter.croniter(self.when, time.time()).get_next()
return True
except (AttributeError, croniter.CroniterBadCronError):
return False
def get_next(self) -> Optional[float]: def get_next(self) -> Optional[float]:
now = time.time() now = time.time()
t = 0 t = 0
@ -288,6 +299,7 @@ class Alarm:
'snooze_interval': self.snooze_interval, 'snooze_interval': self.snooze_interval,
'actions': self.actions.requests, 'actions': self.actions.requests,
'static': self.static, 'static': self.static,
'is_cron': self.is_cron,
} }
@classmethod @classmethod
@ -323,6 +335,7 @@ class Alarm:
snooze_interval=self.snooze_interval, snooze_interval=self.snooze_interval,
enabled=self.is_enabled(), enabled=self.is_enabled(),
static=self.static, static=self.static,
is_cron=self.is_cron,
) )