Guys, integrating the Alertstream in a sensor, is not possible, since its an endless file, so polling it is a nogo
i developed a python script, change offcourse the variables , like username/pass/ip/bearer token
i fire the script with an automation upon start event on Home Assistant
automation:
- alias: Hikvision Door Sensor
initial_state: 'on'
trigger:
- platform: homeassistant
event: start
action:
- service: shell_command.hikvision_stream_sensor
shell command: (the nohup below, makes the script fire in background
hikvision_stream_sensor: nohup python3 /config/python_scripts/hikvision_stream.py $1 > /dev/null 2>&1 &
script:
from requests.auth import HTTPDigestAuth
from datetime import datetime
import requests
import time
import json
#Variables
username = "admin"
password = "XXX"
url = "http://YOUR_IP/ISAPI/Event/notification/alertStream"
headers = {
'Authorization': 'Bearer ¨XXXXX',
'content-type': 'application/json',
}
url_states = "http://localhost:8123/api/states/"
sensor_name = "sensor.hikvision_door"
#Creating Sensor
try:
timestamp = str(datetime.now())
data = json.dumps({'state': 'off', 'attributes': {'Time': timestamp}})
response = requests.post(url_states + sensor_name, headers=headers, data=data)
print("Creating Sensor on start script")
except:
print("Creating Sensor Failed")
while True:
try:
stream = requests.get(url, stream=True, auth=HTTPDigestAuth(username, password))
print("Status code: " , stream.status_code)
for line in stream.iter_lines(chunk_size=1):
str_line = line.decode("utf-8", "ignore")
print(str_line)
#Check for event
if str_line.find('"subEventType": 25') != -1:
result = str_line.find('eventState')
print("Found event!")
try:
timestamp = str(datetime.now())
data = json.dumps({'state': 'on', 'attributes': {'Time': timestamp}})
response = requests.post(url_states + sensor_name, headers=headers, data=data)
print("Door Open")
#put the sensor "on" for 5 seconds
time.sleep(5)
timestamp = str(datetime.now())
data = json.dumps({'state': 'off', 'attributes': {'Time': timestamp}})
response = requests.post(url_states + sensor_name, headers=headers, data=data)
print("Door Closed")
continue
except:
print("Updating sensor failed")
continue
if stream.status_code == 401 or stream.status_code == 403:
time.sleep(5)
except (ValueError,requests.exceptions.ConnectionError,requests.exceptions.ChunkedEncodingError) as err:
print("Connection Failed")
continue