diff --git a/homeassistant/components/media_player/itunes.py b/homeassistant/components/media_player/itunes.py index ecbb144e033..70def719146 100644 --- a/homeassistant/components/media_player/itunes.py +++ b/homeassistant/components/media_player/itunes.py @@ -35,9 +35,10 @@ URL of your running version of iTunes-API. Example: http://192.168.1.50:8181 import logging from homeassistant.components.media_player import ( - MediaPlayerDevice, MEDIA_TYPE_MUSIC, SUPPORT_PAUSE, SUPPORT_SEEK, - SUPPORT_VOLUME_SET, SUPPORT_VOLUME_MUTE, SUPPORT_PREVIOUS_TRACK, - SUPPORT_NEXT_TRACK, SUPPORT_TURN_ON, SUPPORT_TURN_OFF, + MediaPlayerDevice, MEDIA_TYPE_MUSIC, MEDIA_TYPE_PLAYLIST, SUPPORT_PAUSE, + SUPPORT_SEEK, SUPPORT_VOLUME_SET, SUPPORT_VOLUME_MUTE, + SUPPORT_PREVIOUS_TRACK, SUPPORT_NEXT_TRACK, SUPPORT_TURN_ON, + SUPPORT_TURN_OFF, SUPPORT_PLAY_MEDIA, ATTR_ENTITY_PICTURE, ATTR_SUPPORTED_MEDIA_COMMANDS) from homeassistant.const import ( STATE_IDLE, STATE_PLAYING, STATE_PAUSED, STATE_OFF, STATE_ON) @@ -47,7 +48,8 @@ import requests _LOGGER = logging.getLogger(__name__) SUPPORT_ITUNES = SUPPORT_PAUSE | SUPPORT_VOLUME_SET | SUPPORT_VOLUME_MUTE | \ - SUPPORT_PREVIOUS_TRACK | SUPPORT_NEXT_TRACK | SUPPORT_SEEK + SUPPORT_PREVIOUS_TRACK | SUPPORT_NEXT_TRACK | SUPPORT_SEEK | \ + SUPPORT_PLAY_MEDIA SUPPORT_AIRPLAY = SUPPORT_VOLUME_SET | SUPPORT_TURN_ON | SUPPORT_TURN_OFF @@ -118,6 +120,20 @@ class Itunes(object): """ Skips back and returns the current state. """ return self._command('previous') + def play_playlist(self, playlist_id_or_name): + """ Sets a playlist to be current and returns the current state. """ + response = self._request('GET', '/playlists') + playlists = response.get('playlists', []) + + found_playlists = \ + [playlist for playlist in playlists if + (playlist_id_or_name in [playlist["name"], playlist["id"]])] + + if len(found_playlists) > 0: + playlist = found_playlists[0] + path = '/playlists/' + playlist['id'] + '/play' + return self._request('PUT', path) + def artwork_url(self): """ Returns a URL of the current track's album art. """ return self._base_url + '/artwork' @@ -294,6 +310,11 @@ class ItunesDevice(MediaPlayerDevice): """ Album of current playing media. (Music track only) """ return self.current_album + @property + def media_playlist(self): + """ Title of the currently playing playlist. """ + return self.current_playlist + @property def supported_media_commands(self): """ Flags of media commands that are supported. """ @@ -329,6 +350,12 @@ class ItunesDevice(MediaPlayerDevice): response = self.client.previous() self.update_state(response) + def play_media(self, media_type, media_id): + """ play_media media player. """ + if media_type == MEDIA_TYPE_PLAYLIST: + response = self.client.play_playlist(media_id) + self.update_state(response) + class AirPlayDevice(MediaPlayerDevice): """ Represents an AirPlay device via an iTunes-API instance. """