-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredshift-tasmota-RGBW.py
executable file
·73 lines (61 loc) · 2.66 KB
/
redshift-tasmota-RGBW.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
#!/usr/bin/python3
import json
import math
import os
import subprocess
import sys
import time
import urllib.request
import psutil
# FIXME: Config or cmdline switch this
sonoff_hostname = 'sonoff-6810'
def get_randr_rgb_diff():
xrandr_stdout = subprocess.check_output(['xrandr', '--verbose'], universal_newlines=True)
found_gamma = False
for line in xrandr_stdout.splitlines():
if line.strip().startswith('Gamma:'):
if found_gamma:
raise NotImplementedError("Can't handle multiple gamma results")
else:
found_gamma = True
# At this point line is something like this:
# "\tGamma: 0.80:1.0:1.2\n"
r, g, b = (1 - float(g) for g in line.replace(' ', '').split(':')[1:])
# FIXME: Return a dict or named tuple
return r, g, b
# Thrown behind an if statement because testing & debugging was getting annoying with this
if psutil.Process(psutil.Process().ppid()).cmdline()[0].endswith('redshift'):
# Redshift eats stdout, but I want some logging via the systemd journal so use stderr instead
output_file = sys.stderr
print("Forking light controller", sys.argv, file=output_file)
# Redshift runs this *before* setting the gamma values in X11, so we need to fork and detach so that Redshift can continue,
# then wait ~3 seconds for Redshift to finish before querying X11
# Dual-fork technique adapted from:
# https://stackoverflow.com/questions/19369671/launching-a-daemon-from-python-then-detaching-the-parent-from-the-child
if os.fork() > 0:
sys.exit()
os.setsid()
if os.fork() > 0:
sys.exit()
time.sleep(4)
else:
output_file = sys.stdout
base_color_intensity = 255 / 2
red, green, blue = (
# Constrain the results between 0 & 255
max(0, min(255,
# Apply the percentage to our base colour
math.floor(base_color_intensity + (base_color_intensity * c)))
) for c in get_randr_rgb_diff())
if red > 255 or green > 255 or blue > 255:
raise NotImplementedError(f"Gamma value {red}:{green}:{blue} too high")
## White is done by 100% R, G, & B
# white = math.floor(base_color_intensity / 2)
white = 0
command = f"Color2 {red},{green},{blue},{white}"
print("Sending command to tastmota:", command, file=output_file)
# NOTE: Tasmota has been told not to power the globe on when setting the color via the SetOption20 config command
# NOTE: "Color2" = Set color adjusted to current Dimmer value
req = urllib.request.Request(f"http://{sonoff_hostname}/cm",
data=f"cmnd={command}".encode('ascii'))
print(json.load(urllib.request.urlopen(req)), file=output_file)