soco.plugins.plex module

This plugin supports playback from a linked Plex music service. See: https://support.plex.tv/articles/218168898-installing-plex-for-sonos/

Requires:
  • Plex music service must be linked in the Sonos app

  • Use of ‘plexapi’ library (https://github.com/pkkid/python-plexapi)

  • Plex server URI used in ‘plexapi’ must be reachable from Sonos speakers

Example usage:

>>> from plexapi.server import PlexServer
>>> from soco import SoCo
>>> from soco.plugins.plex import PlexPlugin
>>>
>>> s = SoCo("<SPEAKER_IP>")
>>> plugin = PlexPlugin(s)
>>>
>>> plex_uri = "http://1.2.3.4:32400"
>>> plex_token = "<YOUR_PLEX_TOKEN>"
>>> plex = PlexServer(plex_uri, token=plex_token)
>>> music = plex.library.section("Music")
>>> artist = music.get("Stevie Wonder")
>>> album = artist.album("Innervisions")
>>> track = album.tracks()[4]
>>> playlist = plex.playlist("My Playlist")
>>>
>>> plugin.play_now(artist)     # Play all tracks from an artist
>>> plugin.add_to_queue(track)  # Add track to the end of queue
>>> pos = plugin.add_to_queue([album, playlist])  # Enqueue multiple
>>> s.play_from_queue(pos)      # Play items just enqueued
class soco.plugins.plex.PlexPlugin(soco)[source]

A SoCo plugin for playing Plex media using the plexapi library.

Initialize the plugin.

property name

Return the name of the plugin.

property service_name

Return the service name of the Plex music service.

property service_info

Cache and return the service info of the Plex music service.

property service_id

Return the service ID of the Plex music service.

property service_type

Return the service type of the Plex music service.

play_now(plex_media)[source]

Add the media to the end of the queue and immediately begin playback.

add_to_queue(plex_media, position=0, as_next=False, **kwargs)[source]

Add the provided media to the speaker’s playback queue.

Parameters
  • plex_media (plexapi) – The plexapi object representing the Plex media to be enqueued. Can be one of plexapi.audio.Track, plexapi.audio.Album, plexapi.audio.Artist or plexapi.playlist.Playlist. Can also be a list of the above items.

  • position (int) – The index (1-based) at which the media should be added. Default is 0 (append to the end of the queue).

  • as_next (bool) –

    Whether this media should be played as the next track in shuffle mode. This only works if “play_mode=SHUFFLE”.

    Note: Enqueuing multi-track items like albums or playlists will select one track randomly as the next item and shuffle the remaining tracks throughout the queue.

Returns

The index of the first item added to the queue.

Return type

int