[#340] Added condition_type field to alarm objects.

This commit is contained in:
Fabio Manganiello 2023-12-10 21:52:50 +01:00
parent e6e4396e49
commit b4be56ec2c
Signed by: blacklight
GPG key ID: D90FBA7F76362774
2 changed files with 45 additions and 5 deletions

View file

@ -89,9 +89,9 @@ class AlarmPlugin(RunnablePlugin, EntityManager):
:class:`platypush.plugins.media.MediaPlugin`) that will be used to :class:`platypush.plugins.media.MediaPlugin`) that will be used to
play the alarm audio. It needs to be a supported local media play the alarm audio. It needs to be a supported local media
plugin, e.g. ``media.mplayer``, ``media.vlc``, ``media.mpv``, plugin, e.g. ``media.mplayer``, ``media.vlc``, ``media.mpv``,
``media.gstreamer`` etc. If not specified, the first available ``media.gstreamer``, ``sound``, etc. If not specified, the first
configured local media plugin will be used. This only applies to available configured local media plugin will be used. This only
alarms that are configured to play an audio resource. applies to alarms that are configured to play an audio resource.
:param poll_interval: Poll interval in seconds (default: 5). :param poll_interval: Poll interval in seconds (default: 5).
:param snooze_interval: Default snooze interval in seconds (default: 300). :param snooze_interval: Default snooze interval in seconds (default: 300).
""" """

View file

@ -36,6 +36,16 @@ class AlarmState(enum.IntEnum):
UNKNOWN = -1 UNKNOWN = -1
class AlarmConditionType(enum.Enum):
"""
Alarm condition types.
"""
CRON = 'cron'
INTERVAL = 'interval'
TIMESTAMP = 'timestamp'
class Alarm: class Alarm:
""" """
Alarm model and controller. Alarm model and controller.
@ -110,6 +120,36 @@ class Alarm:
except (AttributeError, croniter.CroniterBadCronError): except (AttributeError, croniter.CroniterBadCronError):
return False 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]: def get_next(self) -> Optional[float]:
now = time.time() now = time.time()
t = 0 t = 0
@ -299,7 +339,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, 'condition_type': self.condition_type.value,
} }
@classmethod @classmethod
@ -335,7 +375,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, condition_type=self.condition_type.value,
) )