-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathilluddc.py
85 lines (73 loc) · 2.76 KB
/
illuddc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/bin/python
# author: Claudio Bräuer
# mail: contact at cvbrauer dot de
# python dependencies: requests, subprocess, time, json, sys
# system dependencies: dccutil
import subprocess
import requests
import time
import json
import sys
# set your private home assistant rest token
token = "SECRETTOKEN"
# set the sensor name that you want to query
sensor = "sensor.balcony_light_illuminance_lux"
# set the relevant json field name
field = "illuminance_lux"
# set host ip and port
host = "192.168.0.1:8123"
# set illuminance value at which the monitor is set to maximum brightness
illuminance_max = 10000
# set illuminance value at which the monitor is set to minimum brightness
illuminance_min = 500
# set maximum monitor brightness
brightness_max = 100
# set minimum monitor brightness
brightness_min = 20
# set query interval in seconds
interval = 60
# get the id of the corresponding ddc brightness parameter, typically it is 10
ddcattr = "10"
# verify TLS certificate? Set to false because of reasons
verifyTLS = False
headers = {'Authorization': 'Bearer ' + token, 'Content-Type': "application/json"}
target = "https://" + host + "/api/states/" + sensor
while True:
# lazy catchall
try:
response = requests.get(target, headers=headers, verify=False).text
except:
continue
# json to dict
responseJson = json.loads(response)
# for debugging purposes, print json to understand structure:
#print(responseJson)
# set corresponding dictionary path to field here, e.g "attributes"-> "illuminance_lux"
illuminance_cur = responseJson['attributes'][field]
# clamping the values to min or max
if illuminance_cur <= illuminance_min:
illuminance_cur = illuminance_min
if illuminance_cur >= illuminance_max:
illuminance_cur = illuminance_max
# linear interpolation for target value
brightness_target = int(((illuminance_cur - illuminance_min) / (illuminance_max - illuminance_min)) * (brightness_max - brightness_min) + brightness_min)
# get current brightness
spargs = ['ddcutil', 'getvcp', ddcattr]
ret = subprocess.Popen(spargs, stdout=subprocess.PIPE)
output = ret.communicate()[0].decode('UTF-8').replace(" ","")
# validate output
try:
brightness_cur = int(re.findall("currentvalue=(\d+),", output)[0])
except:
continue
# do nothing if brightness stays the same
if brightness_cur == brightness_target:
time.sleep(interval)
continue
# smooth brightness transition
steps = brightness_target-brightness_cur
for i in range(0, abs(steps)):
brightness_cur += -1 if steps < 0 else 1
spargs = ['ddcutil', '--sleep-multiplier', '.1', 'setvcp', ddcattr, str(brightness_cur)]
subprocess.call(spargs)
time.sleep(interval)