[#340] Better state management for alarms.

This commit is contained in:
Fabio Manganiello 2023-12-09 14:25:18 +01:00
parent 9d5c755188
commit 4b578c38c8
Signed by: blacklight
GPG key ID: D90FBA7F76362774
2 changed files with 7 additions and 3 deletions

View file

@ -3,7 +3,7 @@ import sys
from threading import RLock from threading import RLock
from typing import Collection, Generator, Optional, Dict, Any, List, Union from typing import Collection, Generator, Optional, Dict, Any, List, Union
from sqlalchemy.orm import Session, make_transient from sqlalchemy.orm import Session
from platypush.context import get_plugin from platypush.context import get_plugin
from platypush.entities import EntityManager from platypush.entities import EntityManager
@ -182,7 +182,6 @@ class AlarmPlugin(RunnablePlugin, EntityManager):
def _clear_alarm(self, alarm: DbAlarm, session: Session): def _clear_alarm(self, alarm: DbAlarm, session: Session):
self.alarms.pop(str(alarm.name), None) self.alarms.pop(str(alarm.name), None)
session.delete(alarm) session.delete(alarm)
make_transient(alarm)
self._bus.post(EntityDeleteEvent(entity=alarm)) self._bus.post(EntityDeleteEvent(entity=alarm))
def _clear_expired_alarms(self, session: Session): def _clear_expired_alarms(self, session: Session):

View file

@ -129,7 +129,11 @@ class Alarm:
return self._enabled return self._enabled
def is_shut_down(self): def is_shut_down(self):
return self.state == AlarmState.SHUTDOWN return self.state in {
AlarmState.SHUTDOWN,
AlarmState.DISMISSED,
AlarmState.UNKNOWN,
}
def is_expired(self): def is_expired(self):
return (self.get_next() or 0) < time.time() return (self.get_next() or 0) < time.time()
@ -283,6 +287,7 @@ class Alarm:
snooze_interval=alarm.snooze_interval, # type: ignore snooze_interval=alarm.snooze_interval, # type: ignore
enabled=bool(alarm.enabled), enabled=bool(alarm.enabled),
static=bool(alarm.static), static=bool(alarm.static),
state=getattr(AlarmState, str(alarm.state)),
**kwargs, **kwargs,
) )