From 38c1ebb90c7ed6450d5392bd41397b1c9257d4b9 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Mon, 23 Sep 2024 03:20:55 +0200 Subject: [PATCH] [alarm] Don't fail if no audio file is provided. --- platypush/plugins/alarm/__init__.py | 23 ++++++++++++++--------- platypush/plugins/alarm/_model.py | 3 +++ 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/platypush/plugins/alarm/__init__.py b/platypush/plugins/alarm/__init__.py index 7a5c48d3a3..f98ebe53b5 100644 --- a/platypush/plugins/alarm/__init__.py +++ b/platypush/plugins/alarm/__init__.py @@ -179,13 +179,13 @@ class AlarmPlugin(RunnablePlugin, EntityManager): else: # If the alarm record on the db is static, but the alarm is no # longer present in the configuration, then we want to delete it - if alarm.static: + if bool(alarm.static): self._clear_alarm(alarm, session) else: self.alarms[name] = Alarm.from_db( alarm, stop_event=self._should_stop, - media_plugin=alarm.media_plugin or self.media_plugin, + media_plugin=str(alarm.media_plugin) or self.media_plugin, on_change=self._on_alarm_update, ) @@ -215,7 +215,12 @@ class AlarmPlugin(RunnablePlugin, EntityManager): def _clear_alarm(self, alarm: DbAlarm, session: Session): alarm_obj = self.alarms.pop(str(alarm.name), None) if alarm_obj: - alarm_obj.stop() + try: + alarm_obj.stop() + except Exception as e: + self.logger.warning( + f'Error while stopping alarm {alarm.name}: {e}', exc_info=True + ) session.delete(alarm) self._bus.post(EntityDeleteEvent(entity=alarm)) @@ -439,15 +444,15 @@ class AlarmPlugin(RunnablePlugin, EntityManager): when=when or alarm.when, media=media or alarm.media, media_plugin=media_plugin or alarm.media_plugin or self.media_plugin, - media_repeat=media_repeat - if media_repeat is not None - else alarm.media_repeat, + media_repeat=( + media_repeat if media_repeat is not None else alarm.media_repeat + ), actions=actions if actions is not None else (alarm.actions or []), name=new_name or name, enabled=enabled if enabled is not None else alarm.is_enabled(), - audio_volume=audio_volume - if audio_volume is not None - else alarm.audio_volume, + audio_volume=( + audio_volume if audio_volume is not None else alarm.audio_volume + ), snooze_interval=snooze_interval or alarm.snooze_interval, dismiss_interval=dismiss_interval or alarm.dismiss_interval, ).to_dict() diff --git a/platypush/plugins/alarm/_model.py b/platypush/plugins/alarm/_model.py index 81799154b6..5441885d45 100644 --- a/platypush/plugins/alarm/_model.py +++ b/platypush/plugins/alarm/_model.py @@ -274,6 +274,9 @@ class Alarm: if self.audio_volume is not None: self._get_media_plugin().set_volume(self.audio_volume) + if not self.media: + return + audio_thread = threading.Thread(target=thread) audio_thread.start()