[#340] Alarm model enhancements.

- Allow initialization of alarm actions from `Procedure` objects too.

- Force `state=WAITING` when an alarm is disabled.
This commit is contained in:
Fabio Manganiello 2023-12-10 17:56:47 +01:00
parent 686085750f
commit 8ddd9879f2
Signed by untrusted user: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -44,7 +44,7 @@ class Alarm:
def __init__( def __init__(
self, self,
when: Union[str, int, float], when: Union[str, int, float],
actions: Optional[list] = None, actions: Optional[Union[list, Procedure]] = None,
name: Optional[str] = None, name: Optional[str] = None,
media: Optional[str] = None, media: Optional[str] = None,
media_plugin: Optional[str] = None, media_plugin: Optional[str] = None,
@ -67,9 +67,13 @@ class Alarm:
self.state = AlarmState.UNKNOWN self.state = AlarmState.UNKNOWN
self.timer: Optional[threading.Timer] = None self.timer: Optional[threading.Timer] = None
self.static = static self.static = static
self.actions = Procedure.build( self.actions = (
actions
if isinstance(actions, Procedure)
else Procedure.build(
name=name, _async=False, requests=actions or [], id=self.id name=name, _async=False, requests=actions or [], id=self.id
) )
)
self._enabled = enabled self._enabled = enabled
self._runtime_snooze_interval = snooze_interval self._runtime_snooze_interval = snooze_interval
@ -219,6 +223,9 @@ class Alarm:
self.play_audio() self.play_audio()
self.actions.execute() self.actions.execute()
elif self.state != AlarmState.WAITING:
self.state = AlarmState.WAITING
self._on_change()
self.wait_stop(self.poll_interval) self.wait_stop(self.poll_interval)
sleep_time = None sleep_time = None