From 5008b25a2d2ca859922e78097d5be4e048c3bbe5 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Mon, 8 Jun 2015 23:06:41 -0700 Subject: [PATCH] Fix MPD media player support --- homeassistant/components/media_player/demo.py | 2 +- homeassistant/components/media_player/mpd.py | 115 +++++++++--------- 2 files changed, 60 insertions(+), 57 deletions(-) diff --git a/homeassistant/components/media_player/demo.py b/homeassistant/components/media_player/demo.py index 5d8c5fd635b..24f1e91c3ec 100644 --- a/homeassistant/components/media_player/demo.py +++ b/homeassistant/components/media_player/demo.py @@ -221,7 +221,7 @@ class DemoMusicPlayer(AbstractDemoPlayer): return self.tracks[self._cur_track][0] @property - def media_album(self): + def media_album_name(self): """ Album of current playing media. (Music track only) """ # pylint: disable=no-self-use return "Bounzz" diff --git a/homeassistant/components/media_player/mpd.py b/homeassistant/components/media_player/mpd.py index 53faa37a605..64b927999be 100644 --- a/homeassistant/components/media_player/mpd.py +++ b/homeassistant/components/media_player/mpd.py @@ -32,16 +32,22 @@ Location of your Music Player Daemon. import logging import socket +from homeassistant.const import ( + STATE_PLAYING, STATE_PAUSED, STATE_OFF) + from homeassistant.components.media_player import ( - MediaPlayerDevice, STATE_NO_APP, ATTR_MEDIA_STATE, - ATTR_MEDIA_CONTENT_ID, ATTR_MEDIA_TITLE, ATTR_MEDIA_ARTIST, - ATTR_MEDIA_ALBUM, ATTR_MEDIA_DATE, ATTR_MEDIA_DURATION, - ATTR_MEDIA_VOLUME, MEDIA_STATE_PAUSED, MEDIA_STATE_PLAYING, - MEDIA_STATE_STOPPED, MEDIA_STATE_UNKNOWN) + MediaPlayerDevice, + SUPPORT_PAUSE, SUPPORT_VOLUME_SET, SUPPORT_TURN_OFF, + SUPPORT_PREVIOUS_TRACK, SUPPORT_NEXT_TRACK, + MEDIA_TYPE_MUSIC) _LOGGER = logging.getLogger(__name__) +SUPPORT_MPD = SUPPORT_PAUSE | SUPPORT_VOLUME_SET | SUPPORT_TURN_OFF | \ + SUPPORT_PREVIOUS_TRACK | SUPPORT_NEXT_TRACK + + # pylint: disable=unused-argument def setup_platform(hass, config, add_devices, discovery_info=None): """ Sets up the MPD platform. """ @@ -81,13 +87,15 @@ def setup_platform(hass, config, add_devices, discovery_info=None): class MpdDevice(MediaPlayerDevice): """ Represents a MPD server. """ + # MPD confuses pylint + # pylint: disable=no-member, abstract-method + def __init__(self, server, port, location): from mpd import MPDClient self.server = server self.port = port self._name = location - self.state_attr = {ATTR_MEDIA_STATE: MEDIA_STATE_STOPPED} self.client = MPDClient() self.client.timeout = 10 @@ -99,67 +107,66 @@ class MpdDevice(MediaPlayerDevice): """ Returns the name of the device. """ return self._name - # pylint: disable=no-member @property 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. """ - 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() + + 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() + return current_song['id'] - if not status and not current_song: - state_attr = {} + @property + def media_content_type(self): + """ Content type of current playing media. """ + return MEDIA_TYPE_MUSIC - if current_song['id']: - state_attr[ATTR_MEDIA_CONTENT_ID] = current_song['id'] + @property + def media_duration(self): + """ Duration of current playing media in seconds. """ + current_song = self.client.currentsong() + return current_song['time'] - if current_song['date']: - state_attr[ATTR_MEDIA_DATE] = current_song['date'] + @property + def media_title(self): + """ Title of current playing media. """ + current_song = self.client.currentsong() + return current_song['title'] - if current_song['title']: - state_attr[ATTR_MEDIA_TITLE] = current_song['title'] + @property + 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']: - state_attr[ATTR_MEDIA_DURATION] = current_song['time'] + @property + 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']: - state_attr[ATTR_MEDIA_ARTIST] = current_song['artist'] - - if current_song['album']: - state_attr[ATTR_MEDIA_ALBUM] = current_song['album'] - - state_attr[ATTR_MEDIA_VOLUME] = status['volume'] - - return state_attr + @property + def volume_level(self): + status = self.client.status() + return int(status['volume'])/100 def turn_off(self): """ Service to exit the running MPD. """ self.client.stop() + def set_volume_level(self, volume): + """ Sets volume """ + self.client.setvol(int(volume * 100)) + def volume_up(self): """ Service to send the MPD the command for volume up. """ current_volume = self.client.status()['volume'] @@ -174,10 +181,6 @@ class MpdDevice(MediaPlayerDevice): if int(current_volume) >= 0: 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): """ Service to send the MPD the command for play/pause. """ self.client.start() @@ -190,6 +193,6 @@ class MpdDevice(MediaPlayerDevice): """ Service to send the MPD the command for next track. """ self.client.next() - def media_prev_track(self): + def media_previous_track(self): """ Service to send the MPD the command for previous track. """ self.client.previous()