Sonos playlists play media (#26054)

* Add handling for Sonos playlists in media_player.play_media

* Avoid breaking change by falling back to previous behavior

* Use the proper MEDIA_TYPE_PLAYLIST constant.

* Addressed comments, restricting media_type to music or playlist
This commit is contained in:
ahertz 2019-08-20 13:53:45 -04:00 committed by Paulus Schoutsen
parent cf2d927f14
commit 7fd53ac912

View File

@ -15,6 +15,7 @@ from homeassistant.components.media_player import MediaPlayerDevice
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 @@ class SonosEntity(MediaPlayerDevice):
"""
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):