Renamed parameter of volup/voldown for music plugins.

To be consistent with all other media plugins, the parameter name should
be `step`, not `delta`.

A back-compatibility layer has however been introduced for plugins
(namely `music.mpd` and `music.spotify`) that already exposed the old
interface.
This commit is contained in:
Fabio Manganiello 2024-01-13 22:35:18 +01:00
parent f0dcb94862
commit 641a2fd135
Signed by untrusted user: blacklight
GPG key ID: D90FBA7F76362774
5 changed files with 76 additions and 42 deletions

View file

@ -389,42 +389,42 @@ class MediaPlugin(Plugin, ABC):
@action @action
@abstractmethod @abstractmethod
def play(self, resource, *args, **kwargs): def play(self, resource, **kwargs):
raise self._NOT_IMPLEMENTED_ERR raise self._NOT_IMPLEMENTED_ERR
@action @action
@abstractmethod @abstractmethod
def pause(self, *args, **kwargs): def pause(self, **kwargs):
raise self._NOT_IMPLEMENTED_ERR raise self._NOT_IMPLEMENTED_ERR
@action @action
@abstractmethod @abstractmethod
def stop(self, *args, **kwargs): def stop(self, **kwargs):
raise self._NOT_IMPLEMENTED_ERR raise self._NOT_IMPLEMENTED_ERR
@action @action
@abstractmethod @abstractmethod
def quit(self, *args, **kwargs): def quit(self, **kwargs):
raise self._NOT_IMPLEMENTED_ERR raise self._NOT_IMPLEMENTED_ERR
@action @action
@abstractmethod @abstractmethod
def voldown(self, *args, **kwargs): def voldown(self, step: Optional[float] = 5.0, **kwargs):
raise self._NOT_IMPLEMENTED_ERR raise self._NOT_IMPLEMENTED_ERR
@action @action
@abstractmethod @abstractmethod
def volup(self, *args, **kwargs): def volup(self, step: Optional[float] = 5.0, **kwargs):
raise self._NOT_IMPLEMENTED_ERR raise self._NOT_IMPLEMENTED_ERR
@action @action
@abstractmethod @abstractmethod
def back(self, *args, **kwargs): def back(self, **kwargs):
raise self._NOT_IMPLEMENTED_ERR raise self._NOT_IMPLEMENTED_ERR
@action @action
@abstractmethod @abstractmethod
def forward(self, *args, **kwargs): def forward(self, **kwargs):
raise self._NOT_IMPLEMENTED_ERR raise self._NOT_IMPLEMENTED_ERR
@action @action
@ -440,42 +440,42 @@ class MediaPlugin(Plugin, ABC):
@action @action
@abstractmethod @abstractmethod
def toggle_subtitles(self, *args, **kwargs): def toggle_subtitles(self, **kwargs):
raise self._NOT_IMPLEMENTED_ERR raise self._NOT_IMPLEMENTED_ERR
@action @action
@abstractmethod @abstractmethod
def set_subtitles(self, filename, *args, **kwargs): def set_subtitles(self, filename, **kwargs):
raise self._NOT_IMPLEMENTED_ERR raise self._NOT_IMPLEMENTED_ERR
@action @action
@abstractmethod @abstractmethod
def remove_subtitles(self, *args, **kwargs): def remove_subtitles(self, **kwargs):
raise self._NOT_IMPLEMENTED_ERR raise self._NOT_IMPLEMENTED_ERR
@action @action
@abstractmethod @abstractmethod
def is_playing(self, *args, **kwargs): def is_playing(self, **kwargs):
raise self._NOT_IMPLEMENTED_ERR raise self._NOT_IMPLEMENTED_ERR
@action @action
@abstractmethod @abstractmethod
def load(self, resource, *args, **kwargs): def load(self, resource, **kwargs):
raise self._NOT_IMPLEMENTED_ERR raise self._NOT_IMPLEMENTED_ERR
@action @action
@abstractmethod @abstractmethod
def mute(self, *args, **kwargs): def mute(self, **kwargs):
raise self._NOT_IMPLEMENTED_ERR raise self._NOT_IMPLEMENTED_ERR
@action @action
@abstractmethod @abstractmethod
def seek(self, *args, **kwargs): def seek(self, **kwargs):
raise self._NOT_IMPLEMENTED_ERR raise self._NOT_IMPLEMENTED_ERR
@action @action
@abstractmethod @abstractmethod
def set_position(self, *args, **kwargs): def set_position(self, **kwargs):
raise self._NOT_IMPLEMENTED_ERR raise self._NOT_IMPLEMENTED_ERR
@action @action

View file

@ -338,13 +338,13 @@ class MediaMplayerPlugin(MediaPlugin):
return self.status() return self.status()
@action @action
def voldown(self, *_, step=10.0, **__): def voldown(self, *_, step=5.0, **__):
"""Volume down by (default: 10)%""" """Volume down by (default: 10)%"""
self._exec('volume', -step * 10) self._exec('volume', -step * 10)
return self.status() return self.status()
@action @action
def volup(self, *_, step=10.0, **__): def volup(self, *_, step=5.0, **__):
"""Volume up by (default: 10)%""" """Volume up by (default: 10)%"""
self._exec('volume', step * 10) self._exec('volume', step * 10)
return self.status() return self.status()

View file

@ -1,84 +1,110 @@
from abc import ABC, abstractmethod
from typing import Optional
from platypush.plugins import Plugin, action from platypush.plugins import Plugin, action
class MusicPlugin(Plugin): class MusicPlugin(Plugin, ABC):
def __init__(self, *args, **kwargs): """
Base class for music player plugins.
"""
def __init__(self, **kwargs):
super().__init__(**kwargs) super().__init__(**kwargs)
@action @action
@abstractmethod
def play(self, **kwargs): def play(self, **kwargs):
raise NotImplementedError() raise NotImplementedError()
@action @action
@abstractmethod
def pause(self, **kwargs): def pause(self, **kwargs):
raise NotImplementedError() raise NotImplementedError()
@action @action
@abstractmethod
def stop(self, **kwargs): def stop(self, **kwargs):
raise NotImplementedError() raise NotImplementedError()
@action @action
@abstractmethod
def next(self, **kwargs): def next(self, **kwargs):
raise NotImplementedError() raise NotImplementedError()
@action @action
@abstractmethod
def previous(self, **kwargs): def previous(self, **kwargs):
raise NotImplementedError() raise NotImplementedError()
@action @action
def set_volume(self, volume, **kwargs): @abstractmethod
def set_volume(self, volume: float, **kwargs):
raise NotImplementedError() raise NotImplementedError()
@action @action
def volup(self, delta, **kwargs): @abstractmethod
def volup(self, step: Optional[float] = None, **kwargs):
raise NotImplementedError() raise NotImplementedError()
@action @action
def voldown(self, delta, **kwargs): @abstractmethod
def voldown(self, step: Optional[float] = None, **kwargs):
raise NotImplementedError() raise NotImplementedError()
@action @action
@abstractmethod
def seek(self, position, **kwargs): def seek(self, position, **kwargs):
raise NotImplementedError() raise NotImplementedError()
@action @action
@abstractmethod
def add(self, resource, **kwargs): def add(self, resource, **kwargs):
raise NotImplementedError() raise NotImplementedError()
@action @action
@abstractmethod
def clear(self, **kwargs): def clear(self, **kwargs):
raise NotImplementedError() raise NotImplementedError()
@action @action
@abstractmethod
def status(self, **kwargs): def status(self, **kwargs):
raise NotImplementedError() raise NotImplementedError()
@action @action
@abstractmethod
def current_track(self, **kwargs): def current_track(self, **kwargs):
raise NotImplementedError() raise NotImplementedError()
@action @action
@abstractmethod
def get_playlists(self, **kwargs): def get_playlists(self, **kwargs):
raise NotImplementedError() raise NotImplementedError()
@action @action
@abstractmethod
def get_playlist(self, playlist, **kwargs): def get_playlist(self, playlist, **kwargs):
raise NotImplementedError() raise NotImplementedError()
@action @action
@abstractmethod
def add_to_playlist(self, playlist, resources, **kwargs): def add_to_playlist(self, playlist, resources, **kwargs):
raise NotImplementedError() raise NotImplementedError()
@action @action
@abstractmethod
def remove_from_playlist(self, playlist, resources, **kwargs): def remove_from_playlist(self, playlist, resources, **kwargs):
raise NotImplementedError() raise NotImplementedError()
@action @action
@abstractmethod
def playlist_move(self, playlist, from_pos: int, to_pos: int, **kwargs): def playlist_move(self, playlist, from_pos: int, to_pos: int, **kwargs):
raise NotImplementedError() raise NotImplementedError()
@action @action
def search(self, query, *args, **kwargs): @abstractmethod
def search(self, query, **kwargs):
raise NotImplementedError() raise NotImplementedError()

View file

@ -172,7 +172,7 @@ class MusicMpdPlugin(MusicPlugin):
return self._exec('next') return self._exec('next')
@action @action
def previous(self, *_, **__): def previous(self, **__):
"""Play the previous track""" """Play the previous track"""
return self._exec('previous') return self._exec('previous')
@ -188,7 +188,7 @@ class MusicMpdPlugin(MusicPlugin):
return self.set_volume(vol) return self.set_volume(vol)
@action @action
def set_volume(self, volume: int, *_, **__): def set_volume(self, volume: int, **__):
""" """
Set the volume. Set the volume.
@ -197,25 +197,27 @@ class MusicMpdPlugin(MusicPlugin):
return self._exec('setvol', str(volume)) return self._exec('setvol', str(volume))
@action @action
def volup(self, *_, delta: int = 10, **__): def volup(self, step: Optional[float] = None, **kwargs):
""" """
Turn up the volume. Turn up the volume.
:param delta: Volume up delta (default: +10%). :param step: Volume up step (default: 5%).
""" """
step = step or kwargs.get('delta') or 5
volume = int(self._status()['volume']) volume = int(self._status()['volume'])
new_volume = min(volume + delta, 100) new_volume = min(volume + step, 100)
return self.setvol(new_volume) return self.setvol(new_volume)
@action @action
def voldown(self, *_, delta: int = 10, **__): def voldown(self, step: Optional[float] = None, **kwargs):
""" """
Turn down the volume. Turn down the volume.
:param delta: Volume down delta (default: -10%). :param step: Volume down step (default: 5%).
""" """
step = step or kwargs.get('delta') or 5
volume = int(self._status()['volume']) volume = int(self._status()['volume'])
new_volume = max(volume - delta, 0) new_volume = max(volume - step, 0)
return self.setvol(new_volume) return self.setvol(new_volume)
def _toggle(self, key: str, value: Optional[bool] = None): def _toggle(self, key: str, value: Optional[bool] = None):
@ -390,7 +392,7 @@ class MusicMpdPlugin(MusicPlugin):
return ret return ret
@action @action
def clear(self, *_, **__): def clear(self, **__):
"""Clear the current playlist""" """Clear the current playlist"""
return self._exec('clear') return self._exec('clear')
@ -405,7 +407,7 @@ class MusicMpdPlugin(MusicPlugin):
return self.seek(value) return self.seek(value)
@action @action
def seek(self, position: float, *_, **__): def seek(self, position: float, **__):
""" """
Seek to the specified position Seek to the specified position

View file

@ -139,39 +139,45 @@ class MusicSpotifyPlugin(MusicPlugin, SpotifyMixin):
return self.status.output.get('volume') return self.status.output.get('volume')
@action @action
def volup(self, delta: int = 5, device: Optional[str] = None): def volup(
self, step: Optional[float] = None, device: Optional[str] = None, **kwargs
):
""" """
Set the volume up by a certain delta. Set the volume up by a certain step.
:param delta: Increase the volume by this percentage amount (between 0 and 100). :param step: Increase the volume by this percentage amount (between 0 and 100).
:param device: Device ID or name. If none is specified then the currently active device will be used. :param device: Device ID or name. If none is specified then the currently active device will be used.
""" """
step = step or kwargs.get('delta') or 5
if device: if device:
device = self._get_device(device)['id'] device = self._get_device(device)['id']
self.spotify_user_call( self.spotify_user_call(
'/v1/me/player/volume', '/v1/me/player/volume',
params={ params={
'volume_percent': min(100, (self._get_volume() or 0) + delta), 'volume_percent': min(100, (self._get_volume() or 0) + step),
**({'device_id': device} if device else {}), **({'device_id': device} if device else {}),
}, },
) )
@action @action
def voldown(self, delta: int = 5, device: Optional[str] = None): def voldown(
self, step: Optional[float] = None, device: Optional[str] = None, **kwargs
):
""" """
Set the volume down by a certain delta. Set the volume down by a certain step.
:param delta: Decrease the volume by this percentage amount (between 0 and 100). :param step: Decrease the volume by this percentage amount (between 0 and 100). Default: 5%.
:param device: Device ID or name. If none is specified then the currently active device will be used. :param device: Device ID or name. If none is specified then the currently active device will be used.
""" """
step = step or kwargs.get('delta') or 5
if device: if device:
device = self._get_device(device)['id'] device = self._get_device(device)['id']
self.spotify_user_call( self.spotify_user_call(
'/v1/me/player/volume', '/v1/me/player/volume',
params={ params={
'volume_percent': max(0, (self._get_volume() or 0) - delta), 'volume_percent': max(0, (self._get_volume() or 0) - step),
**({'device_id': device} if device else {}), **({'device_id': device} if device else {}),
}, },
) )