diff --git a/homeassistant/components/sonos/media_player.py b/homeassistant/components/sonos/media_player.py index a4d461f289f2..86e306213345 100644 --- a/homeassistant/components/sonos/media_player.py +++ b/homeassistant/components/sonos/media_player.py @@ -15,6 +15,7 @@ from homeassistant.components.media_player.const import ( ATTR_MEDIA_ENQUEUE, MEDIA_TYPE_MUSIC, + MEDIA_TYPE_PLAYLIST, SUPPORT_CLEAR_PLAYLIST, SUPPORT_NEXT_TRACK, SUPPORT_PAUSE, @@ -938,20 +939,35 @@ def play_media(self, media_type, media_id, **kwargs): """ Send the play_media command to the media player. + If media_type is "playlist", media_id should be a Sonos + Playlist name. Otherwise, media_id should be a URI. + If ATTR_MEDIA_ENQUEUE is True, add `media_id` to the queue. """ - if kwargs.get(ATTR_MEDIA_ENQUEUE): + if media_type == MEDIA_TYPE_MUSIC: + if kwargs.get(ATTR_MEDIA_ENQUEUE): + try: + self.soco.add_uri_to_queue(media_id) + except SoCoUPnPException: + _LOGGER.error( + 'Error parsing media uri "%s", ' + "please check it's a valid media resource " + "supported by Sonos", + media_id, + ) + else: + self.soco.play_uri(media_id) + elif media_type == MEDIA_TYPE_PLAYLIST: try: - self.soco.add_uri_to_queue(media_id) - except SoCoUPnPException: - _LOGGER.error( - 'Error parsing media uri "%s", ' - "please check it's a valid media resource " - "supported by Sonos", - media_id, - ) + playlists = self.soco.get_sonos_playlists() + playlist = next(p for p in playlists if p.title == media_id) + self.soco.clear_queue() + self.soco.add_to_queue(playlist) + self.soco.play_from_queue(0) + except StopIteration: + _LOGGER.error('Could not find a Sonos playlist named "%s"', media_id) else: - self.soco.play_uri(media_id) + _LOGGER.error('Sonos does not support a media type of "%s"', media_type) @soco_error() def join(self, slaves):