|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +import requests |
| 3 | +from plexapi import CONFIG, X_PLEX_IDENTIFIER |
| 4 | +from plexapi.client import PlexClient |
| 5 | +from plexapi.exceptions import BadRequest |
| 6 | +from plexapi.playqueue import PlayQueue |
| 7 | + |
| 8 | + |
| 9 | +class PlexSonosClient(PlexClient): |
| 10 | + """ Class for interacting with a Sonos speaker via the Plex API. This class |
| 11 | + makes requests to an external Plex API which then forwards the |
| 12 | + Sonos-specific commands back to your Plex server & Sonos speakers. Use |
| 13 | + of this feature requires an active Plex Pass subscription and Sonos |
| 14 | + speakers linked to your Plex account. It also requires remote access to |
| 15 | + be working properly. |
| 16 | +
|
| 17 | + More details on the Sonos integration are avaialble here: |
| 18 | + https://support.plex.tv/articles/218237558-requirements-for-using-plex-for-sonos/ |
| 19 | +
|
| 20 | + The Sonos API emulates the Plex player control API closely: |
| 21 | + https://github.com/plexinc/plex-media-player/wiki/Remote-control-API |
| 22 | +
|
| 23 | + Parameters: |
| 24 | + account (:class:`~plexapi.myplex.PlexAccount`): PlexAccount instance this |
| 25 | + Sonos speaker is associated with. |
| 26 | + data (ElementTree): Response from Plex Sonos API used to build this client. |
| 27 | +
|
| 28 | + Attributes: |
| 29 | + deviceClass (str): "speaker" |
| 30 | + lanIP (str): Local IP address of speaker. |
| 31 | + machineIdentifier (str): Unique ID for this device. |
| 32 | + platform (str): "Sonos" |
| 33 | + platformVersion (str): Build version of Sonos speaker firmware. |
| 34 | + product (str): "Sonos" |
| 35 | + protocol (str): "plex" |
| 36 | + protocolCapabilities (list<str>): List of client capabilities (timeline, playback, |
| 37 | + playqueues, provider-playback) |
| 38 | + server (:class:`~plexapi.server.PlexServer`): Server this client is connected to. |
| 39 | + session (:class:`~requests.Session`): Session object used for connection. |
| 40 | + title (str): Name of this Sonos speaker. |
| 41 | + token (str): X-Plex-Token used for authenication |
| 42 | + _baseurl (str): Address of public Plex Sonos API endpoint. |
| 43 | + _commandId (int): Counter for commands sent to Plex API. |
| 44 | + _token (str): Token associated with linked Plex account. |
| 45 | + _session (obj): Requests session object used to access this client. |
| 46 | + """ |
| 47 | + |
| 48 | + def __init__(self, account, data): |
| 49 | + self._data = data |
| 50 | + self.deviceClass = data.attrib.get("deviceClass") |
| 51 | + self.machineIdentifier = data.attrib.get("machineIdentifier") |
| 52 | + self.product = data.attrib.get("product") |
| 53 | + self.platform = data.attrib.get("platform") |
| 54 | + self.platformVersion = data.attrib.get("platformVersion") |
| 55 | + self.protocol = data.attrib.get("protocol") |
| 56 | + self.protocolCapabilities = data.attrib.get("protocolCapabilities") |
| 57 | + self.lanIP = data.attrib.get("lanIP") |
| 58 | + self.title = data.attrib.get("title") |
| 59 | + self._baseurl = "https://sonos.plex.tv" |
| 60 | + self._commandId = 0 |
| 61 | + self._token = account._token |
| 62 | + self._session = account._session or requests.Session() |
| 63 | + |
| 64 | + # Dummy values for PlexClient inheritance |
| 65 | + self._last_call = 0 |
| 66 | + self._proxyThroughServer = False |
| 67 | + self._showSecrets = CONFIG.get("log.show_secrets", "").lower() == "true" |
| 68 | + |
| 69 | + def playMedia(self, media, offset=0, **params): |
| 70 | + |
| 71 | + if hasattr(media, "playlistType"): |
| 72 | + mediatype = media.playlistType |
| 73 | + else: |
| 74 | + if isinstance(media, PlayQueue): |
| 75 | + mediatype = media.items[0].listType |
| 76 | + else: |
| 77 | + mediatype = media.listType |
| 78 | + |
| 79 | + if mediatype == "audio": |
| 80 | + mediatype = "music" |
| 81 | + else: |
| 82 | + raise BadRequest("Sonos currently only supports music for playback") |
| 83 | + |
| 84 | + server_protocol, server_address, server_port = media._server._baseurl.split(":") |
| 85 | + server_address = server_address.strip("/") |
| 86 | + server_port = server_port.strip("/") |
| 87 | + |
| 88 | + playqueue = ( |
| 89 | + media |
| 90 | + if isinstance(media, PlayQueue) |
| 91 | + else media._server.createPlayQueue(media) |
| 92 | + ) |
| 93 | + self.sendCommand( |
| 94 | + "playback/playMedia", |
| 95 | + **dict( |
| 96 | + { |
| 97 | + "type": "music", |
| 98 | + "providerIdentifier": "com.plexapp.plugins.library", |
| 99 | + "containerKey": "/playQueues/{}?own=1".format( |
| 100 | + playqueue.playQueueID |
| 101 | + ), |
| 102 | + "key": media.key, |
| 103 | + "offset": offset, |
| 104 | + "machineIdentifier": media._server.machineIdentifier, |
| 105 | + "protocol": server_protocol, |
| 106 | + "address": server_address, |
| 107 | + "port": server_port, |
| 108 | + "token": media._server.createToken(), |
| 109 | + "commandID": self._nextCommandId(), |
| 110 | + "X-Plex-Client-Identifier": X_PLEX_IDENTIFIER, |
| 111 | + "X-Plex-Token": media._server._token, |
| 112 | + "X-Plex-Target-Client-Identifier": self.machineIdentifier, |
| 113 | + }, |
| 114 | + **params, |
| 115 | + ), |
| 116 | + ) |
0 commit comments