Skip to content

Allow control of Sonos speakers using Plex API#471

Merged
Hellowlol merged 8 commits intopushingkarmaorg:masterfrom
jjlawren:sonos_controls
May 10, 2020
Merged

Allow control of Sonos speakers using Plex API#471
Hellowlol merged 8 commits intopushingkarmaorg:masterfrom
jjlawren:sonos_controls

Conversation

@jjlawren
Copy link
Copy Markdown
Collaborator

Plex offers a Plex Pass feature where Plex clients can control music playback on Sonos speakers directly, bypassing the need for using the Sonos app: https://support.plex.tv/articles/218237558-requirements-for-using-plex-for-sonos/.

These calls go through a Plex cloud service so remote access must be open for the control commands to work properly. However, playback will be direct from Plex server -> speaker unless network issues prevent it (ie, broken rebind protection).

Note: Playback will always be reported as "remote" in the Plex Web status page as playback progress is reported by the Plex service, not the speaker directly.

Requirements:

  1. Active Plex Pass subscription
  2. Sonos linked to Plex account
  3. Remote access working on Plex
  4. (Possible) Rebind protection mitigated for plex.direct DNS domains

This will allow you to control Plex music on your Sonos speakers like so:

from plexapi.server import PlexServer
from plexapi.myplex import MyPlexAccount

account = MyPlexAccount(token)
for speaker in account.sonos_speakers():
    print(speaker.title)

server = PlexServer(server_url, token)

album = server.library.section('Music').get('Stevie Wonder').album('Innervisions')

speaker = account.sonos_speaker("Kitchen")
speaker.playMedia(album)

#speaker.pause()
#speaker.setVolume(10)
#speaker.skipNext()

@Hellowlol
Copy link
Copy Markdown
Collaborator

Can you document examples and the requirements? Also tests would be great

@jjlawren
Copy link
Copy Markdown
Collaborator Author

jjlawren commented May 2, 2020

Docs updated. Any suggestions on testing the public Plex APIs used? The tests like in test_myplex.py don't really seem to test the actual functionality expected, just failure cases.

Perhaps we could mock return some expected XML examples.

@Hellowlol
Copy link
Copy Markdown
Collaborator

Mock would be nice.

@jjlawren
Copy link
Copy Markdown
Collaborator Author

jjlawren commented May 7, 2020

@Hellowlol anything else you'd like here?

@Hellowlol
Copy link
Copy Markdown
Collaborator

Nope. Everything seems to be in order. I’ll see if I can’t get it merges this weekend.

@Hellowlol Hellowlol merged commit 6daaa85 into pushingkarmaorg:master May 10, 2020
@Hellowlol
Copy link
Copy Markdown
Collaborator

Hellowlol commented May 11, 2020

Mind checking https://readthedocs.org/projects/python-plexapi/builds/11011580/

looking for now-outdated files... none found
pickling environment... WARNING: autodoc: failed to import module u'myplex'; the following exception was raised:
Traceback (most recent call last):
  File "/home/docs/checkouts/readthedocs.org/user_builds/python-plexapi/envs/stable/lib/python2.7/site-packages/sphinx/ext/autodoc/importer.py", line 154, in import_module
    __import__(modname)
  File "/home/docs/checkouts/readthedocs.org/user_builds/python-plexapi/checkouts/stable/plexapi/myplex.py", line 15, in <module>
    from plexapi.sonos import PlexSonosClient
  File "/home/docs/checkouts/readthedocs.org/user_builds/python-plexapi/checkouts/stable/plexapi/sonos.py", line 114
    **params,
            ^
SyntaxError: invalid syntax

WARNING: autodoc: failed to import module u'myplex' from module u'plexapi'; the following exception was raised:
Traceback (most recent call last):
  File "/home/docs/checkouts/readthedocs.org/user_builds/python-plexapi/envs/stable/lib/python2.7/site-packages/sphinx/ext/autodoc/importer.py", line 154, in import_module
    __import__(modname)
  File "/home/docs/checkouts/readthedocs.org/user_builds/python-plexapi/checkouts/stable/plexapi/myplex.py", line 15, in <module>
    from plexapi.sonos import PlexSonosClient
  File "/home/docs/checkouts/readthedocs.org/user_builds/python-plexapi/checkouts/stable/plexapi/sonos.py", line 114
    **params,
            ^
SyntaxError: invalid syntax

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants