Workout Script w/ Plex Video Control

Thanks man. I have tried it but it still doesn’t shuffle. This is what I did.

Added this code under class PlexClient(MediaPlayerDevice): in z_plex.py

def playMedia(self, media, **params):
    import plexapi.playqueue
    server_url = media.server.baseurl.split(':')
    playqueue = plexapi.playqueue.PlayQueue.create(self.device.server,media,**params)
    self.device.sendCommand('playback/playMedia', **dict({
        'machineIdentifier': self.device.server.machineIdentifier,
        'address': server_url[1].strip('/'),
        'port': server_url[-1],
        'key': media.key,
        'containerKey': '/playQueues/%s?window=100&own=1' % playqueue.playQueueID,
    }, **params))

@asyncio.coroutine
def async_play_media(self, media_type, media_id, **kwargs):
    src = json.loads(media_id)
    vid = self.device.server.playlist(src['title'])
    self.device.playMedia(vid,shuffle=1)

My script remains the same. When run the script, it plays the playlist but it was never shuffled.

I’ll check out this code to see if there are any differences with what I have.

Meanwhile, can you take a look at your log files and see if you see a log output this looks like this?:

17-02-01 20:08:46 INFO (MainThread) [plexapi] POST http://<server-ip>:32400/playQueues?includeChapters=1&includeRelated=1&playlistID=136659&repeat=1&shuffle=1&type=audio&X-Plex-Token=...

Note the ‘shuffle=1’.

So, instead of using ’self.device.playMedia(vid,shuffle=1)' you need to be calling the new PlexClient 'playMedia' method:

self.playMedia(vid,shuffle=1) # w/o the 'device' part

I know I could have changed the name of the method - to be less confusing. But, it’s just the way I roll.

This is what I have in the log…

17-02-02 00:00:00 INFO (MainThread) [plexapi] GET http://<server-ip>:32400/playlists?X-Plex-Token=<token>
17-02-02 00:00:00 INFO (MainThread) [plexapi] POST http://<server-ip>:32400/playQueues?includeChapters=1&includeRelated=1&playlistID=360771&repeat=0&shuffle=0&type=audio&X-Plex-Token=<token>
17-02-02 00:00:00 INFO (MainThread) [plexapi] GET http://<client-ip>:32500/player/playback/playMedia?address=<server-ip>&commandID=1&containerKey=/playQueues/10868%3Fwindow%3D100%26own%3D1&key=/playlists/360771&machineIdentifier=<machineIdentifier>&port=32400&shuffle=1
17-02-02 00:00:00 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/lib/python3.4/asyncio/tasks.py", line 237, in _step
    result = next(coro)
  File "/usr/local/lib/python3.4/dist-packages/homeassistant/core.py", line 1052, in _event_to_service_call
    yield from service_handler.func(service_call)
  File "/usr/local/lib/python3.4/dist-packages/homeassistant/components/media_player/__init__.py", line 366, in async_service_handler
    yield from getattr(player, method['method'])(**params)
  File "/usr/lib/python3.4/asyncio/coroutines.py", line 141, in coro
    res = func(*args, **kw)
  File "/home/pi/.homeassistant/custom_components/media_player/z_plex.py", line 390, in async_play_media
    self.device.playMedia(vid,shuffle=1)
  File "/home/pi/.homeassistant/deps/plexapi/client.py", line 154, in playMedia
    }, **params))
  File "/home/pi/.homeassistant/deps/plexapi/client.py", line 85, in sendCommand
    return self.query(path, headers=headers)
  File "/home/pi/.homeassistant/deps/plexapi/client.py", line 71, in query
    return ElementTree.fromstring(data) if data else None
  File "/usr/lib/python3.4/xml/etree/ElementTree.py", line 1325, in XML
    parser.feed(text)
  File "<string>", line None
xml.etree.ElementTree.ParseError: syntax error: line 1, column 0

Yep - that makes sense.

If you remove the “.device.” part of the call to playMedia() it should work.

1 Like

Yep. That works! Thanks a lot.

Now I wish this will make into the official Plex component

2 Likes

I’ll try to find some time soon to look into what that would take.

2 Likes

I have modified your code to this…

def async_play_media(self, media_type, media_id, **kwargs):
    src = json.loads(media_id)
    if media_type == 'EPISODE':
        media = self.device.server.library.section(src['library']).get(src['title']).episodes()[src['episode']]
    elif media_type == 'MUSIC':
        media = self.device.server.playlist(src['title'])
    self.playMedia(media,shuffle=src['shuffle'])

Now, the media variable is base on media_content_type in the script. Not sure about other media_type but you can get the idea.

1 Like

might want to have a look here: https://www.plex.tv/blog/alexa-ask-plex-get-party-started/

yeah man. i was super excited because i just got my first Alexa 12 hours before the announcement was made.

anyway, the skill is not really useful to HA. I still need this script for my automation.

I added this functionality (plus a little more) here (scroll to the bottom for the latest link):

Note: I also changed the input json format from your original examples. I documented the new formats in my link above.

2 Likes