Disentangle and optimize some code

Rename method

Simplify some code

Clarify some code
This commit is contained in:
croneter 2021-09-12 16:33:18 +02:00
parent 63bd85d5c8
commit 9da61a059f

View file

@ -30,7 +30,7 @@ class KodiMonitor(xbmc.Monitor):
""" """
def __init__(self): def __init__(self):
self._already_slept = False self._already_slept = False
self._switch_to_plex_streams = None self._switched_to_plex_streams = True
xbmc.Monitor.__init__(self) xbmc.Monitor.__init__(self)
for playerid in app.PLAYSTATE.player_states: for playerid in app.PLAYSTATE.player_states:
app.PLAYSTATE.player_states[playerid] = copy.deepcopy(app.PLAYSTATE.template) app.PLAYSTATE.player_states[playerid] = copy.deepcopy(app.PLAYSTATE.template)
@ -68,7 +68,7 @@ class KodiMonitor(xbmc.Monitor):
self.PlayBackStart(data) self.PlayBackStart(data)
elif method == 'Player.OnAVChange': elif method == 'Player.OnAVChange':
with app.APP.lock_playqueues: with app.APP.lock_playqueues:
self.on_av_change() self._on_av_change(data)
elif method == "Player.OnStop": elif method == "Player.OnStop":
with app.APP.lock_playqueues: with app.APP.lock_playqueues:
_playback_cleanup(ended=data.get('end')) _playback_cleanup(ended=data.get('end'))
@ -359,32 +359,36 @@ class KodiMonitor(xbmc.Monitor):
status['plex_type'] = plex_type status['plex_type'] = plex_type
status['playmethod'] = item.playmethod status['playmethod'] = item.playmethod
status['playcount'] = item.playcount status['playcount'] = item.playcount
try: status['external_player'] = app.APP.player.isExternalPlayer() == 1
status['external_player'] = app.APP.player.isExternalPlayer() == 1
except AttributeError:
# Kodi version < 17
pass
LOG.debug('Set the player state: %s', status) LOG.debug('Set the player state: %s', status)
# Workaround for the Kodi add-on Up Next # Workaround for the Kodi add-on Up Next
if not app.SYNC.direct_paths: if not app.SYNC.direct_paths:
_notify_upnext(item) _notify_upnext(item)
self._switch_to_plex_streams = item self._switched_to_plex_streams = False
def on_av_change(self): def _on_av_change(self, data):
""" """
Will be called when Kodi has a video, audio or subtitle stream. Also Will be called when Kodi has a video, audio or subtitle stream. Also
happens when the stream changes. happens when the stream changes.
Example data as returned by Kodi:
{'item': {'id': 5, 'type': 'movie'},
'player': {'playerid': 1, 'speed': 1}}
""" """
if self._switch_to_plex_streams is not None: if not self._switched_to_plex_streams:
self.switch_to_plex_streams(self._switch_to_plex_streams) self.switch_to_plex_streams()
self._switch_to_plex_streams = None self._switched_to_plex_streams = True
@staticmethod @staticmethod
def switch_to_plex_streams(item): def switch_to_plex_streams():
""" """
Override Kodi audio and subtitle streams with Plex PMS' selection Override Kodi audio and subtitle streams with Plex PMS' selection
""" """
item = app.PLAYSTATE.item
if item is None:
# Player might've quit
return
for typus in ('audio', 'subtitle'): for typus in ('audio', 'subtitle'):
try: try:
plex_index, language_tag = item.active_plex_stream_index(typus) plex_index, language_tag = item.active_plex_stream_index(typus)