Skip to content

Instantly share code, notes, and snippets.

@nshores
Last active October 8, 2018 23:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nshores/08b4d9089c950efb85bba90cfff491b2 to your computer and use it in GitHub Desktop.
Save nshores/08b4d9089c950efb85bba90cfff491b2 to your computer and use it in GitHub Desktop.
onkyo_reboot.py
#Reboot receiver when eISCP commands fail
#https://github.com/miracle2k/onkyo-eiscp
import eiscp
import requests
import time
import urllib
from influxdb import InfluxDBClient
#Define influxdb stuff
host='192.168.99.118'
port='8086'
dbname = 'cacti2'
client = InfluxDBClient(host=host, port=port, database=dbname)
#Define Vera URL
url = 'http://vera.local:49451/data_request?'
#Define receiver IP
receiver = eiscp.eISCP('192.168.99.130')
#Define vera request params
vera_switch_on = {
'id': 'action',
'DeviceNum': '77',
'serviceId': 'urn:upnp-org:serviceId:SwitchPower1',
'action': 'SetTarget',
'newTargetValue': '1',
}
vera_switch_off = {
'id': 'action',
'DeviceNum': '77',
'serviceId': 'urn:upnp-org:serviceId:SwitchPower1',
'action': 'SetTarget',
'newTargetValue': '0',
}
#Fixup vera request url encoding
vera_switch_on_qry = urllib.parse.urlencode(vera_switch_on).replace('%3A', ':')
vera_switch_off_qry = urllib.parse.urlencode(vera_switch_off).replace('%3A', ':')
def vera_request(qry):
s = requests.Session()
req = requests.Request(method='GET', url=url)
prep = req.prepare()
prep.url = url + qry
r = s.send(prep)
#Main loop
receiver_reachable = None
try:
#Try to send a command to the reciever
receiver_status = receiver.command('dimmer-level=bright')
except:
print("Cant connect to onkyo")
receiver_reachable = 0
json_body = [
{
"tags": {
"host": "onkyo",
},
"measurement": "reachable",
"fields": {
"int_value": receiver_reachable
}
}
]
client.write_points(json_body)
print("Turning Receiver off")
vera_request(vera_switch_off_qry)
#Sleep 5
time.sleep(5)
print("Turning Receiver on")
vera_request(vera_switch_on_qry)
else:
print('Success - Receiver Conencted!')
receiver_reachable = 1
json_body = [
{
"tags": {
"host": "onkyo",
},
"measurement": "reachable",
"fields": {
"int_value": receiver_reachable
}
}
]
client.write_points(json_body)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment