diff --git a/resources/lib/PlexCompanion.py b/resources/lib/PlexCompanion.py index bdbb37b4..6a4b3b7d 100644 --- a/resources/lib/PlexCompanion.py +++ b/resources/lib/PlexCompanion.py @@ -86,10 +86,13 @@ class PlexCompanion(Thread): playqueue = self.mgr.playqueue.get_playqueue_from_type( data['type']) if ID != playqueue.ID: + # playqueue changed somehow self.mgr.playqueue.update_playqueue_from_PMS( playqueue, ID, int(query['repeat']), data['offset']) else: - log.error('This has never happened before!') + # No change to the playqueue + self.mgr.playqueue.start_playqueue_initiated_by_companion( + playqueue, int(query['repeat']), data['offset']) def run(self): httpd = False diff --git a/resources/lib/playlist_func.py b/resources/lib/playlist_func.py index 4bdde005..e4a887a0 100644 --- a/resources/lib/playlist_func.py +++ b/resources/lib/playlist_func.py @@ -375,7 +375,21 @@ def get_PMS_playlist(playlist, playlist_id=None): return xml -def update_playlist_from_PMS(playlist, playlist_id=None, repeat=None): +def refresh_playlist_from_PMS(playlist): + """ + Only updates the selected item from the PMS side (e.g. + playQueueSelectedItemID). Will NOT check whether items still make sense. + """ + xml = get_PMS_playlist(playlist) + try: + xml.attrib['%sVersion' % playlist.kind] + except: + log.error('Could not download Plex playlist.') + return + _get_playlist_details_from_xml(playlist, xml) + + +def update_playlist_from_PMS(playlist, playlist_id=None): """ Updates Kodi playlist using a new PMS playlist. Pass in playlist_id if we need to fetch a new playqueue @@ -390,8 +404,6 @@ def update_playlist_from_PMS(playlist, playlist_id=None, repeat=None): playlist.clear() # Set new values _get_playlist_details_from_xml(playlist, xml) - if repeat: - playlist.repeat = repeat for plex_item in xml: playlist.items.append(add_to_Kodi_playlist(playlist, plex_item)) diff --git a/resources/lib/playqueue.py b/resources/lib/playqueue.py index c969b5d5..bc8eafbb 100644 --- a/resources/lib/playqueue.py +++ b/resources/lib/playqueue.py @@ -93,7 +93,8 @@ class Playqueue(Thread): offset = time offset in Plextime """ log.info('New playqueue received from the PMS, updating!') - PL.update_playlist_from_PMS(playqueue, playqueue_id, repeat) + PL.update_playlist_from_PMS(playqueue, playqueue_id) + playqueue.repeat = repeat log.debug('Updated playqueue: %s' % playqueue) window('plex_customplaylist', value="true") @@ -112,6 +113,34 @@ class Playqueue(Thread): self.player.play(playqueue.kodi_pl) playqueue.log_Kodi_playlist() + @lockmethod.lockthis + def start_playqueue_initiated_by_companion(self, + playqueue, + playqueue_id=None, + repeat=None, + offset=None): + log.info('Plex companion wants to restart playback of playqueue %s' + % playqueue) + # Still need to get new playQueue from the server - don't know what has + # been selected + PL.refresh_playlist_from_PMS(playqueue) + playqueue.repeat = repeat + window('plex_customplaylist', value="true") + if offset not in (None, "0"): + window('plex_customplaylist.seektime', + str(ConvertPlexToKodiTime(offset))) + for startpos, item in enumerate(playqueue.items): + if item.ID == playqueue.selectedItemID: + break + else: + startpos = None + # Start playback + if startpos: + self.player.play(playqueue.kodi_pl, startpos=startpos) + else: + self.player.play(playqueue.kodi_pl) + playqueue.log_Kodi_playlist() + @lockmethod.lockthis def kodi_onadd(self, data): """