Hi all,
I am working with a number of POE Camera devices that periodically lose feed and require being toggled to resolve.
While in a state of having no feed the devices are pingable but get no response from a HTTP request.
I have wrote a Python script that uses a JSON file with all device names/entity _ids and ip addresses.
My plan is iterate through JSON file as python dictionary, send HTTP request, then, if request fails then toggle the POE device.
import requests
import json
# import excel2json
import os
import re
def getHTTPReq(value):
# HTTP Req False until else proven otherwise
result = False
# Strip IP to identify empty strings
if(value.strip() == ""):
#print("\nInvalid IP Address")
return False
else:
address = 'https://'+ value +'/'
#print("\nSent request to " + address)
# Try-Catch Request Exceptions
try:
result = requests.get(address)
except requests.exceptions.RequestException as e:
#print("Caught RequestException....")
# print(str(e))
# Result is 'requests.models.Response' Class
return str(result) == "<Response [200]>"
failedList = []
# Open JSON File
with open('Camera ports.json') as json_file:
jsonFile = json.load(json_file)
# Itterate through each JSON Object
for eachCamera in jsonFile:
reqAddress = (eachCamera['Full IP'])
isSuccess = getHTTPReq(reqAddress)
if isSuccess:
#print("Cam:" + eachCamera['Location'] + " HTTP Request " + "Success")
else:
lowername = ("switch.cam:" + eachCamera['Location'].lower())
regexname = re.sub('[^0-9a-zA-Z]+', '_', lowername)
failedList.append(regexname)
#print("Cam:" + eachCamera['Location'] + " HTTP Request " + "Failed")
for fail in failedList:
#print(fail = "\n")
entity_idx = str(fail)
service_data = {"entity_id": entity_idx}
hass.services.call("switch", "turn_on", service_data)
However, my script fails to run… Failed to call service python_script.http_request. Unknown error