Fix MPD media player support

This commit is contained in:
Paulus Schoutsen 2015-06-08 23:06:41 -07:00
parent 90919a66d9
commit 5008b25a2d
2 changed files with 60 additions and 57 deletions

View file

@ -221,7 +221,7 @@ class DemoMusicPlayer(AbstractDemoPlayer):
return self.tracks[self._cur_track][0] return self.tracks[self._cur_track][0]
@property @property
def media_album(self): def media_album_name(self):
""" Album of current playing media. (Music track only) """ """ Album of current playing media. (Music track only) """
# pylint: disable=no-self-use # pylint: disable=no-self-use
return "Bounzz" return "Bounzz"

View file

@ -32,16 +32,22 @@ Location of your Music Player Daemon.
import logging import logging
import socket import socket
from homeassistant.const import (
STATE_PLAYING, STATE_PAUSED, STATE_OFF)
from homeassistant.components.media_player import ( from homeassistant.components.media_player import (
MediaPlayerDevice, STATE_NO_APP, ATTR_MEDIA_STATE, MediaPlayerDevice,
ATTR_MEDIA_CONTENT_ID, ATTR_MEDIA_TITLE, ATTR_MEDIA_ARTIST, SUPPORT_PAUSE, SUPPORT_VOLUME_SET, SUPPORT_TURN_OFF,
ATTR_MEDIA_ALBUM, ATTR_MEDIA_DATE, ATTR_MEDIA_DURATION, SUPPORT_PREVIOUS_TRACK, SUPPORT_NEXT_TRACK,
ATTR_MEDIA_VOLUME, MEDIA_STATE_PAUSED, MEDIA_STATE_PLAYING, MEDIA_TYPE_MUSIC)
MEDIA_STATE_STOPPED, MEDIA_STATE_UNKNOWN)
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
SUPPORT_MPD = SUPPORT_PAUSE | SUPPORT_VOLUME_SET | SUPPORT_TURN_OFF | \
SUPPORT_PREVIOUS_TRACK | SUPPORT_NEXT_TRACK
# pylint: disable=unused-argument # pylint: disable=unused-argument
def setup_platform(hass, config, add_devices, discovery_info=None): def setup_platform(hass, config, add_devices, discovery_info=None):
""" Sets up the MPD platform. """ """ Sets up the MPD platform. """
@ -81,13 +87,15 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
class MpdDevice(MediaPlayerDevice): class MpdDevice(MediaPlayerDevice):
""" Represents a MPD server. """ """ Represents a MPD server. """
# MPD confuses pylint
# pylint: disable=no-member, abstract-method
def __init__(self, server, port, location): def __init__(self, server, port, location):
from mpd import MPDClient from mpd import MPDClient
self.server = server self.server = server
self.port = port self.port = port
self._name = location self._name = location
self.state_attr = {ATTR_MEDIA_STATE: MEDIA_STATE_STOPPED}
self.client = MPDClient() self.client = MPDClient()
self.client.timeout = 10 self.client.timeout = 10
@ -99,67 +107,66 @@ class MpdDevice(MediaPlayerDevice):
""" Returns the name of the device. """ """ Returns the name of the device. """
return self._name return self._name
# pylint: disable=no-member
@property @property
def state(self): def state(self):
""" Returns the state of the device. """
status = self.client.status()
if status is None:
return STATE_NO_APP
else:
return self.client.currentsong()['artist']
@property
def media_state(self):
""" Returns the media state. """ """ Returns the media state. """
media_controller = self.client.status()
if media_controller['state'] == 'play':
return MEDIA_STATE_PLAYING
elif media_controller['state'] == 'pause':
return MEDIA_STATE_PAUSED
elif media_controller['state'] == 'stop':
return MEDIA_STATE_STOPPED
else:
return MEDIA_STATE_UNKNOWN
# pylint: disable=no-member
@property
def state_attributes(self):
""" Returns the state attributes. """
status = self.client.status() status = self.client.status()
if status['state'] == 'play':
return STATE_PLAYING
elif status['state'] == 'pause':
return STATE_PAUSED
else:
return STATE_OFF
@property
def media_content_id(self):
""" Content ID of current playing media. """
current_song = self.client.currentsong() current_song = self.client.currentsong()
return current_song['id']
if not status and not current_song: @property
state_attr = {} def media_content_type(self):
""" Content type of current playing media. """
return MEDIA_TYPE_MUSIC
if current_song['id']: @property
state_attr[ATTR_MEDIA_CONTENT_ID] = current_song['id'] def media_duration(self):
""" Duration of current playing media in seconds. """
current_song = self.client.currentsong()
return current_song['time']
if current_song['date']: @property
state_attr[ATTR_MEDIA_DATE] = current_song['date'] def media_title(self):
""" Title of current playing media. """
current_song = self.client.currentsong()
return current_song['title']
if current_song['title']: @property
state_attr[ATTR_MEDIA_TITLE] = current_song['title'] def media_artist(self):
""" Artist of current playing media. (Music track only) """
current_song = self.client.currentsong()
return current_song['artist']
if current_song['time']: @property
state_attr[ATTR_MEDIA_DURATION] = current_song['time'] def media_album_name(self):
""" Album of current playing media. (Music track only) """
current_song = self.client.currentsong()
return current_song['album']
if current_song['artist']: @property
state_attr[ATTR_MEDIA_ARTIST] = current_song['artist'] def volume_level(self):
status = self.client.status()
if current_song['album']: return int(status['volume'])/100
state_attr[ATTR_MEDIA_ALBUM] = current_song['album']
state_attr[ATTR_MEDIA_VOLUME] = status['volume']
return state_attr
def turn_off(self): def turn_off(self):
""" Service to exit the running MPD. """ """ Service to exit the running MPD. """
self.client.stop() self.client.stop()
def set_volume_level(self, volume):
""" Sets volume """
self.client.setvol(int(volume * 100))
def volume_up(self): def volume_up(self):
""" Service to send the MPD the command for volume up. """ """ Service to send the MPD the command for volume up. """
current_volume = self.client.status()['volume'] current_volume = self.client.status()['volume']
@ -174,10 +181,6 @@ class MpdDevice(MediaPlayerDevice):
if int(current_volume) >= 0: if int(current_volume) >= 0:
self.client.setvol(int(current_volume) - 5) self.client.setvol(int(current_volume) - 5)
def media_play_pause(self):
""" Service to send the MPD the command for play/pause. """
self.client.pause()
def media_play(self): def media_play(self):
""" Service to send the MPD the command for play/pause. """ """ Service to send the MPD the command for play/pause. """
self.client.start() self.client.start()
@ -190,6 +193,6 @@ class MpdDevice(MediaPlayerDevice):
""" Service to send the MPD the command for next track. """ """ Service to send the MPD the command for next track. """
self.client.next() self.client.next()
def media_prev_track(self): def media_previous_track(self):
""" Service to send the MPD the command for previous track. """ """ Service to send the MPD the command for previous track. """
self.client.previous() self.client.previous()