My setup:
instance 0 - APA102 strips behind the tv
instance 1 - nanoleaf
instance 2 - nanoleaf panels
USB capture device from Roku running plex.
Everything works great.
What I am trying to do is turn the leds on when media plays and off when media stops by using a plex webhook. I am catching the webhook, looking for the play uuid (so it only turns the lights on/off for that player) and the media.start and media.stop events.
from flask import Flask, request, Response
import json
import requests
app = Flask(__name__)
@app.route('/plex', methods=['POST'])
def respond():
info = json.loads(request.form['payload'])
event = info["event"]
player = info["Player"]["uuid"]
title = info["Player"]["title"]
print(event)
print(player)
print(title)
if player == "plex uuid of the connected plex channel": #Roku Streambar
if event == "media.play":
print("hyperion on")
headers = {}
data = '{"command":"componentstate","componentstate":{"component":"LEDDEVICE","state":true}}'
response = requests.post('http://192.168.2.73:19444/json-rpc', data = data, headers = headers)
data = '{"command":"instance","subcommand":"startInstance","instance":1}'
response = requests.post('http://192.168.2.73:19444/json-rpc', data = data, headers = headers)
data = '{"command":"instance","subcommand":"startInstance","instance":2}'
response = requests.post('http://192.168.2.73:19444/json-rpc', data = data, headers = headers)
elif event == "media.stop":
print("hyperion off")
headers = {}
data = '{"command":"componentstate","componentstate":{"component":"LEDDEVICE","state":false}}'
response = requests.post('http://192.168.2.73:19444/json-rpc', data = data, headers = headers)
data = '{"command":"instance","subcommand":"stopInstance","instance":1}'
response = requests.post('http://192.168.2.73:19444/json-rpc', data = data, headers = headers)
data = '{"command":"instance","subcommand":"stopInstance","instance":2}'
response = requests.post('http://192.168.2.73:19444/json-rpc', data = data, headers = headers)
return Response(status=200)
Display More
Every request call I make gives an error from hyperion. Python error:
ConnectionError: ('Connection aborted.', BadStatusLine('{"command":"","error":"Errors during message parsing, please consult the Hyperion Log.","success":false,"tan":0}\n',))
Error from hyperion log:
2021-10-20T11:47:17.838Z [hyperiond JSONCLIENTCONNECTION] (ERROR) Failed to parse json data from JsonRpc@::ffff:192.168.2.73: Error: illegal value at Line: 0, Column: 1
The curl version of the commands all work:
curl -X POST -i 'http://192.168.2.73:8090/json-rpc' --data '{"command":"componentstate","componentstate":{"component":"LEDDEVICE","state":false}}'
curl -X POST -i 'http://192.168.2.73:8090/json-rpc' --data '{"command" : "instance","subcommand" : "stopInstance","instance" : 1}'
curl -X POST -i 'http://192.168.2.73:8090/json-rpc' --data '{"command" : "instance","subcommand" : "stopInstance","instance" : 2}'
curl -X POST -i 'http://192.168.2.73:8090/json-rpc' --data '{"command":"componentstate","componentstate":{"component":"LEDDEVICE","state":true}}'
curl -X POST -i 'http://192.168.2.73:8090/json-rpc' --data '{"command" : "instance","subcommand" : "startInstance","instance" : 1}'
curl -X POST -i 'http://192.168.2.73:8090/json-rpc' --data '{"command" : "instance","subcommand" : "startInstance","instance" : 2}'
Any clue why the python request isn't working the same way as the curl version?
And as an aside, is there a better way to turn the leds on and off? Seems that instance 0 as the default behaves differently from instances 1 and 2. I would need to change the connected instance and then turn the LEDDEVICE off, but I can't figure out how to pass multiple commands, either. I would need to, in a single call
{ "command" : "instance", "subcommand" : "switchTo", "instance" : 1 }
{"command":"componentstate","componentstate":{"component":"LEDDEVICE","state":true}}
Thanks in advance for any pointers.
milhouse
Got it! The http port 8090 worked better than the json-rpc port. This code works:
from flask import Flask, request, Response
import json
import requests
app = Flask(__name__)
@app.route('/plex', methods=['POST'])
def respond():
info = json.loads(request.form['payload'])
event = info["event"]
player = info["Player"]["uuid"]
title = info["Player"]["title"]
print(event)
print(player)
print(title)
if player == "uuid goes here":
if event == "media.play":
print("hyperion on")
headers = {}
data = '{"command":"componentstate","componentstate":{"component":"LEDDEVICE","state":true}}'
response = requests.post('http://192.168.2.73:8090/json-rpc', data = data, headers = headers)
data = '{"command":"instance","subcommand":"startInstance","instance":1}'
response = requests.post('http://192.168.2.73:8090/json-rpc', data = data, headers = headers)
data = '{"command":"instance","subcommand":"startInstance","instance":2}'
response = requests.post('http://192.168.2.73:8090/json-rpc', data = data, headers = headers)
elif event == "media.stop":
print("hyperion off")
headers = {}
data = '{"command":"componentstate","componentstate":{"component":"LEDDEVICE","state":false}}'
response = requests.post('http://192.168.2.73:8090/json-rpc', data = data, headers = headers)
data = '{"command":"instance","subcommand":"stopInstance","instance":1}'
response = requests.post('http://192.168.2.73:8090/json-rpc', data = data, headers = headers)
data = '{"command":"instance","subcommand":"stopInstance","instance":2}'
response = requests.post('http://192.168.2.73:8090/json-rpc', data = data, headers = headers)
return Response(status=200)
Display More