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

View file

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

View file

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

View file

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

View file

@ -139,39 +139,45 @@ class MusicSpotifyPlugin(MusicPlugin, SpotifyMixin):
return self.status.output.get('volume')
@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.
"""
step = step or kwargs.get('delta') or 5
if device:
device = self._get_device(device)['id']
self.spotify_user_call(
'/v1/me/player/volume',
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 {}),
},
)
@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.
"""
step = step or kwargs.get('delta') or 5
if device:
device = self._get_device(device)['id']
self.spotify_user_call(
'/v1/me/player/volume',
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 {}),
},
)