python-plexapi
python-plexapi copied to clipboard
Playback event listening
What is your feature request?
To display on an e-paper display, I'd like to listen for media playback start events rather than polling sessions every X minutes. I noticed the API supports events/notifications but the documentation appears to be light on detail and there are no examples. Please can you update the documentation and/or point me in the right area for the API to listen for playback events?
Are there any workarounds?
No response
Code Snippets
No response
Additional Context
No response
This is possible using the alerts
module: https://github.com/pkkid/python-plexapi/blob/master/plexapi/alert.py. It sets up a websocket connection where you provide a callback method, and that method is called with a dict of data received from the PMS instance when interesting events occur. It's partially documented, but you may need to do some experimentation to determine the important payloads for your use case.
I needed an async implementation of this, so I created https://github.com/jjlawren/python-plexwebsocket. It has some special handling for client sessions, including some throttling so the consuming app doesn't need to ignore unnecessary updates.
That is exactly what I'm doing:
def listener(alert):
global log, server, broker
if "playing" in alert["type"]:
n = alert["PlaySessionStateNotification"][0]
key = int(n["key"].split("/")[-1])
state = n["state"]
alert = {**alert, **fetchItem(key), "state": state}
c = findClient(n["clientIdentifier"])
if c: # Plexamp provides information, but Alexa doesn't
alert.update(c)
log.debug(alert)
broker.publish("plex/playback", dumps(alert))
else:
log.debug(alert)
server.startAlertListener(listener)