Skip to content

Commit

Permalink
Merge pull request #14 from confirm/add-next-previous-actions
Browse files Browse the repository at this point in the history
FEATURE: Add next & previous track actions
  • Loading branch information
domibarton authored Apr 15, 2020
2 parents a78c163 + 5858745 commit 7590da3
Show file tree
Hide file tree
Showing 21 changed files with 417 additions and 371 deletions.
7 changes: 5 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,12 @@ Connecting the buttons (optional)
You can connect two buttons to the RPi:

- ``RPi pin 5`` - Power button: Shutdown the Raspberry Pi into halt state & wake it up again from halt state
- ``RPi pin 7`` - Playback button: Pause and resume the playback
- ``RPi pin 29`` - Playback button: Pause and resume the playback
- ``RPi pin 31`` - Stop button: Stops the playback
- ``RPi pin 33`` - Previous button: Changes to the previous track in the playlist
- ``RPi pin 35`` - Next button: Changes to the next track in the playlist

The buttons must shortcut their corresponding pins against ``GND`` (e.g. pin ``6``) when pressed. This means you want to connect one pin of the button (i.e. ``C``) to RPI's ``GND``, and the other one (i.e. ``NO``) to RPi's pin ``5`` or ``7``.
The buttons must shortcut their corresponding pins against ``GND`` (e.g. pin ``6``) when pressed. This means you want to connect one pin of the button (i.e. ``C``) to RPI's ``GND``, and the other one (i.e. ``NO``) to RPi's pin ``5``, ``29``, ``31``, ``33`` or ``35``.

Connecting the status LED (optional)
------------------------------------
Expand Down
12 changes: 7 additions & 5 deletions mopidy_pummeluff/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import mopidy

from .frontend import PummeluffFrontend
from .web import LatestHandler, RegistryHandler, RegisterHandler, TagClassesHandler
from .web import LatestHandler, RegistryHandler, RegisterHandler, UnregisterHandler, \
ActionClassesHandler


def app_factory(config, core): # pylint: disable=unused-argument
Expand All @@ -21,10 +22,11 @@ def app_factory(config, core): # pylint: disable=unused-argument
:rtype: list
'''
return [
('/latest/', LatestHandler, {'core': core}),
('/registry/', RegistryHandler, {'core': core}),
('/register/', RegisterHandler, {'core': core}),
('/tag-classes/', TagClassesHandler, {'core': core}),
('/latest/', LatestHandler),
('/registry/', RegistryHandler),
('/register/', RegisterHandler),
('/unregister/', UnregisterHandler),
('/action-classes/', ActionClassesHandler),
]


Expand Down
88 changes: 0 additions & 88 deletions mopidy_pummeluff/actions.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
'''

__all__ = (
'Tracklist',
'Volume',
'PlayPause',
'Stop',
'PreviousTrack',
'NextTrack',
'Shutdown',
'Tracklist',
'Volume',
)

from .playback import PlayPause, Stop, PreviousTrack, NextTrack
from .shutdown import Shutdown
from .tracklist import Tracklist
from .volume import Volume
from .play_pause import PlayPause
from .stop import Stop
from .shutdown import Shutdown

TAGS = {}
for tag in __all__:
TAGS[tag] = globals()[tag].__doc__.strip()
ACTIONS = {}
for action in __all__:
ACTIONS[action] = globals()[action].__doc__.strip()
46 changes: 24 additions & 22 deletions mopidy_pummeluff/tags/base.py → mopidy_pummeluff/actions/base.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
'''
Python module for Mopidy Pummeluff base tag.
Python module for Mopidy Pummeluff base action.
'''

__all__ = (
'Tag',
'Action',
)

from logging import getLogger
from inspect import getfullargspec

LOGGER = getLogger(__name__)


class Tag:
class Action:
'''
Base RFID tag class, which will implement the factory pattern in Python's
own :py:meth:`__new__` method.
'''
parameterised = True

@classmethod
def execute(cls, core):
'''
Execute the action.
:param mopidy.core.Core core: The mopidy core instance
:raises NotImplementedError: When class method is not implemented
'''
name = cls.__name__
error = 'Missing execute class method in the %s class'
LOGGER.error(error, name)
raise NotImplementedError(error % name)

def __init__(self, uid, alias=None, parameter=None):
'''
Expand Down Expand Up @@ -56,21 +70,7 @@ def __call__(self, core):
args = [core]
if self.parameter:
args.append(self.parameter)
self.action.__func__(*args)

@property
def action(self):
'''
Return an action function defined in the
:py:mod:`mopidy_pummeluff.actions` Python module.
:return: An action
:raises NotImplementedError: When action property isn't defined
'''
cls = self.__class__.__name__
error = 'Missing action property in the %s class'
LOGGER.error(error, cls)
raise NotImplementedError(error % cls)
self.execute(*args)

def as_dict(self, include_scanned=False):
'''
Expand All @@ -82,7 +82,7 @@ def as_dict(self, include_scanned=False):
:rtype: dict
'''
data = {
'tag_class': self.__class__.__name__,
'action_class': self.__class__.__name__,
'uid': self.uid,
'alias': self.alias or '',
'parameter': self.parameter or '',
Expand All @@ -99,8 +99,10 @@ def validate(self):
:raises ValueError: When parameter is not allowed but defined
'''
if self.parameterised and not self.parameter:
parameterised = len(getfullargspec(self.execute).args) > 2

if parameterised and not self.parameter:
raise ValueError('Parameter required for this tag')

if not self.parameterised and self.parameter:
if not parameterised and self.parameter:
raise ValueError('No parameter allowed for this tag')
86 changes: 86 additions & 0 deletions mopidy_pummeluff/actions/playback.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
'''
Python module for Mopidy Pummeluff playback actions.
'''

__all__ = (
'PlayPause',
'Stop',
'PreviousTrack',
'NextTrack',
)

from logging import getLogger

from .base import Action

LOGGER = getLogger(__name__)


class PlayPause(Action):
'''
Pauses or resumes the playback, based on the current state.
'''

@classmethod
def execute(cls, core):
'''
Pause or resume the playback.
:param mopidy.core.Core core: The mopidy core instance
'''
playback = core.playback

if playback.get_state().get() == 'playing':
LOGGER.info('Pausing the playback')
playback.pause()
else:
LOGGER.info('Resuming the playback')
playback.resume()


class Stop(Action):
'''
Stops the playback.
'''

@classmethod
def execute(cls, core):
'''
Stop playback.
:param mopidy.core.Core core: The mopidy core instance
'''
LOGGER.info('Stopping playback')
core.playback.stop()


class PreviousTrack(Action):
'''
Changes to the previous track.
'''

@classmethod
def execute(cls, core):
'''
Change to previous track.
:param mopidy.core.Core core: The mopidy core instance
'''
LOGGER.info('Changing to previous track')
core.playback.previous()


class NextTrack(Action):
'''
Changes to the next track.
'''

@classmethod
def execute(cls, core):
'''
Change to next track.
:param mopidy.core.Core core: The mopidy core instance
'''
LOGGER.info('Changing to next track')
core.playback.next()
30 changes: 30 additions & 0 deletions mopidy_pummeluff/actions/shutdown.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'''
Python module for Mopidy Pummeluff shutdown tag.
'''

__all__ = (
'Shutdown',
)

from logging import getLogger
from os import system

from .base import Action

LOGGER = getLogger(__name__)


class Shutdown(Action):
'''
Shutting down the system.
'''

@classmethod
def execute(cls, core):
'''
Shutdown.
:param mopidy.core.Core core: The mopidy core instance
'''
LOGGER.info('Shutting down')
system('sudo /sbin/shutdown -h now')
Loading

0 comments on commit 7590da3

Please sign in to comment.