Just wanted to post a thankyou… Your script helped me out with a custom home theater masking system I am working on. I was trying to figure out how to get the aspect ratio from a movie playing into home assistant and your script helped out alot!.. below is my script incase anyone else is interested. I followed the same setup instructions to get the script running in home assistant. I also changed the sensor name to AspectRatio so now its visible in home assistant as such. The idea is to trigger my masking system to cover the white parts of my screen when watching movies with black bars on top and bottom and when the state changes back to idle the masking system will open again.
import json
import requests
################################
# Start user-defined settings
################################
plexToken = <X-PLEX-TOKEN>
# Same as in PlexDevices
plexURL = <IP to plex>
plexPORT = <port to plex>
trackedPlayer = <machineIdentifier of device you want to track>
# Name of machineIdentifier can be found in Status-page at plex web. Case-Sensitive!
# exemple use:
# plexToken = 'gd887gd9fgfd7cf8'
# plexURL = 'http://192.168.0.2'
# plexPORT = '32400'
# trackedPlayer = '305872y0tbay7996-com-plexapp-android'
################################
# End user-defined settings
################################
url = plexURL + ':' + plexPORT + '/status/sessions/'
headers = {'Accept': 'application/json', 'X-Plex-Token': plexToken}
r = requests.get(url,headers = headers)
myjSon = json.loads(r.text)
nbrOfSessions = myjSon['MediaContainer']['size']
trackedPlayerStatus = "idle"
if nbrOfSessions == 0:
print(trackedPlayerStatus)
if nbrOfSessions > 0:
videoStates = myjSon['MediaContainer']['Video']
trackedPlayerStatus = "idle"
for i in range(0, len(videoStates)):
player = videoStates[i]['Player']['machineIdentifier']
if player == trackedPlayer:
mediaId = videoStates[i]['ratingKey']
if nbrOfSessions > 0:
videoStates = myjSon['MediaContainer']['Video']
trackedPlayerStatus = "idle"
for i in range(0, len(videoStates)):
player = videoStates[i]['Player']['machineIdentifier']
if player == trackedPlayer:
trackedPlayerStatus = videoStates[i]['Player']['state']
if nbrOfSessions > 0:
aspectUrl = plexURL + ':' + plexPORT + '/library/metadata/' + mediaId
headers = {'Accept': 'application/json', 'X-Plex-Token': plexToken}
request = requests.get(aspectUrl,headers = headers)
aspectjSon = json.loads(request.text)
if trackedPlayerStatus != "idle":
movieAspect = aspectjSon['MediaContainer']['Metadata']
trackedPlayerStatus = "idle"
for i in range(0, len(movieAspect)):
if player == trackedPlayer:
movieRatio = movieAspect[i]['Media'][0]['aspectRatio']
break
print(movieRatio)