From fb17aafe53248f52ed91446d2b7be347d234244e Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sun, 23 Feb 2020 23:09:40 +0100 Subject: [PATCH] Support for custom audio volume for alarms --- platypush/backend/alarm.py | 10 ++++++++-- platypush/plugins/alarm.py | 8 +++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/platypush/backend/alarm.py b/platypush/backend/alarm.py index c3e8635a21..8e50a14099 100644 --- a/platypush/backend/alarm.py +++ b/platypush/backend/alarm.py @@ -29,6 +29,7 @@ class Alarm: def __init__(self, when: str, actions: Optional[list] = None, name: Optional[str] = None, audio_file: Optional[str] = None, audio_plugin: Optional[str] = None, + audio_volume: Optional[Union[int, float]] = None, snooze_interval: float = 300.0, enabled: bool = True): with self._id_lock: self._alarms_count += 1 @@ -43,6 +44,7 @@ class Alarm: assert os.path.isfile(self.audio_file), 'No such audio file: {}'.format(self.audio_file) self.audio_plugin = audio_plugin + self.audio_volume = audio_volume self.snooze_interval = snooze_interval self.state: Optional[AlarmState] = None self.timer: Optional[threading.Timer] = None @@ -109,6 +111,8 @@ class Alarm: def play_audio(self): def thread(): self._get_audio_plugin().play(self.audio_file) + if self.audio_volume is not None: + self._get_audio_plugin().set_volume(self.audio_volume) self.state = AlarmState.RUNNING audio_thread = threading.Thread(target=thread) @@ -192,6 +196,8 @@ class AlarmBackend(Backend): morning_alarm: when: '0 7 * * 1-5' # Cron expression format: run every weekday at 7 AM audio_file: ~/path/your_ringtone.mp3 + audio_plugin: media.mplayer + audio_volume: 10 # 10% snooze_interval: 300 # 5 minutes snooze actions: - action: tts.say @@ -226,9 +232,9 @@ class AlarmBackend(Backend): self.alarms: Dict[str, Alarm] = {alarm.name: alarm for alarm in alarms} def add_alarm(self, when: str, actions: list, name: Optional[str] = None, audio_file: Optional[str] = None, - enabled: bool = True) -> Alarm: + audio_volume: Optional[Union[int, float]] = None, enabled: bool = True) -> Alarm: alarm = Alarm(when=when, actions=actions, name=name, enabled=enabled, audio_file=audio_file, - audio_plugin=self.audio_plugin) + audio_plugin=self.audio_plugin, audio_volume=audio_volume) if alarm.name in self.alarms: self.logger.info('Overwriting existing alarm {}'.format(alarm.name)) diff --git a/platypush/plugins/alarm.py b/platypush/plugins/alarm.py index 896c82b6b7..026575db16 100644 --- a/platypush/plugins/alarm.py +++ b/platypush/plugins/alarm.py @@ -1,4 +1,4 @@ -from typing import Optional, Dict, Any, List +from typing import Optional, Dict, Any, List, Union from platypush.backend.alarm import AlarmBackend from platypush.context import get_backend @@ -21,7 +21,8 @@ class AlarmPlugin(Plugin): @action def add(self, when: str, actions: Optional[list] = None, name: Optional[str] = None, - audio_file: Optional[str] = None, enabled: bool = True) -> str: + audio_file: Optional[str] = None, audio_volume: Optional[Union[int, float]] = None, + enabled: bool = True) -> str: """ Add a new alarm. NOTE: alarms that aren't configured in the :class:`platypush.backend.alarm.AlarmBackend` will only run in the current session. If you want an alarm to be permanently stored, you should configure @@ -33,11 +34,12 @@ class AlarmPlugin(Plugin): :param actions: List of actions to be executed. :param name: Alarm name. :param audio_file: Path of the audio file to be played. + :param audio_volume: Volume of the audio. :param enabled: Whether the new alarm should be enabled (default: True). :return: The alarm name. """ alarm = self._get_backend().add_alarm(when=when, audio_file=audio_file, actions=actions or [], - name=name, enabled=enabled) + name=name, enabled=enabled, audio_volume=audio_volume) return alarm.name @action