From 4b578c38c832db2382ee554cf8ae11e278184359 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sat, 9 Dec 2023 14:25:18 +0100 Subject: [PATCH] [#340] Better state management for alarms. --- platypush/plugins/alarm/__init__.py | 3 +-- platypush/plugins/alarm/_model.py | 7 ++++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/platypush/plugins/alarm/__init__.py b/platypush/plugins/alarm/__init__.py index 567dd622d9..0ede4105bf 100644 --- a/platypush/plugins/alarm/__init__.py +++ b/platypush/plugins/alarm/__init__.py @@ -3,7 +3,7 @@ import sys from threading import RLock 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.entities import EntityManager @@ -182,7 +182,6 @@ class AlarmPlugin(RunnablePlugin, EntityManager): def _clear_alarm(self, alarm: DbAlarm, session: Session): self.alarms.pop(str(alarm.name), None) session.delete(alarm) - make_transient(alarm) self._bus.post(EntityDeleteEvent(entity=alarm)) def _clear_expired_alarms(self, session: Session): diff --git a/platypush/plugins/alarm/_model.py b/platypush/plugins/alarm/_model.py index 63078d0170..5fa46237bb 100644 --- a/platypush/plugins/alarm/_model.py +++ b/platypush/plugins/alarm/_model.py @@ -129,7 +129,11 @@ class Alarm: return self._enabled def is_shut_down(self): - return self.state == AlarmState.SHUTDOWN + return self.state in { + AlarmState.SHUTDOWN, + AlarmState.DISMISSED, + AlarmState.UNKNOWN, + } def is_expired(self): return (self.get_next() or 0) < time.time() @@ -283,6 +287,7 @@ class Alarm: snooze_interval=alarm.snooze_interval, # type: ignore enabled=bool(alarm.enabled), static=bool(alarm.static), + state=getattr(AlarmState, str(alarm.state)), **kwargs, )