From b4be56ec2c61b252df733fee09d6f43174c23107 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sun, 10 Dec 2023 21:52:50 +0100 Subject: [PATCH] [#340] Added `condition_type` field to alarm objects. --- platypush/plugins/alarm/__init__.py | 6 ++-- platypush/plugins/alarm/_model.py | 44 +++++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/platypush/plugins/alarm/__init__.py b/platypush/plugins/alarm/__init__.py index 45e187f98c..ff84914223 100644 --- a/platypush/plugins/alarm/__init__.py +++ b/platypush/plugins/alarm/__init__.py @@ -89,9 +89,9 @@ class AlarmPlugin(RunnablePlugin, EntityManager): :class:`platypush.plugins.media.MediaPlugin`) that will be used to play the alarm audio. It needs to be a supported local media plugin, e.g. ``media.mplayer``, ``media.vlc``, ``media.mpv``, - ``media.gstreamer`` etc. If not specified, the first available - configured local media plugin will be used. This only applies to - alarms that are configured to play an audio resource. + ``media.gstreamer``, ``sound``, etc. If not specified, the first + available configured local media plugin will be used. This only + applies to alarms that are configured to play an audio resource. :param poll_interval: Poll interval in seconds (default: 5). :param snooze_interval: Default snooze interval in seconds (default: 300). """ diff --git a/platypush/plugins/alarm/_model.py b/platypush/plugins/alarm/_model.py index 8ec2d5f370..401c92b7e4 100644 --- a/platypush/plugins/alarm/_model.py +++ b/platypush/plugins/alarm/_model.py @@ -36,6 +36,16 @@ class AlarmState(enum.IntEnum): UNKNOWN = -1 +class AlarmConditionType(enum.Enum): + """ + Alarm condition types. + """ + + CRON = 'cron' + INTERVAL = 'interval' + TIMESTAMP = 'timestamp' + + class Alarm: """ Alarm model and controller. @@ -110,6 +120,36 @@ class Alarm: except (AttributeError, croniter.CroniterBadCronError): return False + @property + def is_interval(self) -> bool: + try: + float(self.when) + return True + except (TypeError, ValueError): + return False + + @property + def is_timestamp(self) -> bool: + if not isinstance(self.when, str): + return False + + try: + datetime.datetime.fromisoformat(self.when) + return True + except Exception: + return False + + @property + def condition_type(self) -> AlarmConditionType: + if self.is_cron: + return AlarmConditionType.CRON + if self.is_interval: + return AlarmConditionType.INTERVAL + if self.is_timestamp: + return AlarmConditionType.TIMESTAMP + + raise ValueError(f'Invalid alarm condition {self.when}') + def get_next(self) -> Optional[float]: now = time.time() t = 0 @@ -299,7 +339,7 @@ class Alarm: 'snooze_interval': self.snooze_interval, 'actions': self.actions.requests, 'static': self.static, - 'is_cron': self.is_cron, + 'condition_type': self.condition_type.value, } @classmethod @@ -335,7 +375,7 @@ class Alarm: snooze_interval=self.snooze_interval, enabled=self.is_enabled(), static=self.static, - is_cron=self.is_cron, + condition_type=self.condition_type.value, )