Support for custom audio volume for alarms

This commit is contained in:
Fabio Manganiello 2020-02-23 23:09:40 +01:00
parent 2d3c61173d
commit fb17aafe53
2 changed files with 13 additions and 5 deletions

View File

@ -29,6 +29,7 @@ class Alarm:
def __init__(self, when: str, actions: Optional[list] = None, name: Optional[str] = None, def __init__(self, when: str, actions: Optional[list] = None, name: Optional[str] = None,
audio_file: Optional[str] = None, audio_plugin: 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): snooze_interval: float = 300.0, enabled: bool = True):
with self._id_lock: with self._id_lock:
self._alarms_count += 1 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) assert os.path.isfile(self.audio_file), 'No such audio file: {}'.format(self.audio_file)
self.audio_plugin = audio_plugin self.audio_plugin = audio_plugin
self.audio_volume = audio_volume
self.snooze_interval = snooze_interval self.snooze_interval = snooze_interval
self.state: Optional[AlarmState] = None self.state: Optional[AlarmState] = None
self.timer: Optional[threading.Timer] = None self.timer: Optional[threading.Timer] = None
@ -109,6 +111,8 @@ class Alarm:
def play_audio(self): def play_audio(self):
def thread(): def thread():
self._get_audio_plugin().play(self.audio_file) 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 self.state = AlarmState.RUNNING
audio_thread = threading.Thread(target=thread) audio_thread = threading.Thread(target=thread)
@ -192,6 +196,8 @@ class AlarmBackend(Backend):
morning_alarm: morning_alarm:
when: '0 7 * * 1-5' # Cron expression format: run every weekday at 7 AM when: '0 7 * * 1-5' # Cron expression format: run every weekday at 7 AM
audio_file: ~/path/your_ringtone.mp3 audio_file: ~/path/your_ringtone.mp3
audio_plugin: media.mplayer
audio_volume: 10 # 10%
snooze_interval: 300 # 5 minutes snooze snooze_interval: 300 # 5 minutes snooze
actions: actions:
- action: tts.say - action: tts.say
@ -226,9 +232,9 @@ class AlarmBackend(Backend):
self.alarms: Dict[str, Alarm] = {alarm.name: alarm for alarm in alarms} 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, 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, 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: if alarm.name in self.alarms:
self.logger.info('Overwriting existing alarm {}'.format(alarm.name)) self.logger.info('Overwriting existing alarm {}'.format(alarm.name))

View File

@ -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.backend.alarm import AlarmBackend
from platypush.context import get_backend from platypush.context import get_backend
@ -21,7 +21,8 @@ class AlarmPlugin(Plugin):
@action @action
def add(self, when: str, actions: Optional[list] = None, name: Optional[str] = None, 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` 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 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 actions: List of actions to be executed.
:param name: Alarm name. :param name: Alarm name.
:param audio_file: Path of the audio file to be played. :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). :param enabled: Whether the new alarm should be enabled (default: True).
:return: The alarm name. :return: The alarm name.
""" """
alarm = self._get_backend().add_alarm(when=when, audio_file=audio_file, actions=actions or [], 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 return alarm.name
@action @action