-
Notifications
You must be signed in to change notification settings - Fork 0
/
ambi.py
executable file
·83 lines (64 loc) · 2.14 KB
/
ambi.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
#!/opt/homebrew/bin/python3
import time
import click
from executor import WindowExecutor
from api import HaApi
from image import image_grab_mss
from process import get_average_color, get_color_diff, get_whites, rgba_to_hsl
import subprocess
import os
BASE_URL = os.environ.get('HA_API_URL')
TOKEN = os.environ.get('HA_API_TOKEN')
NETWORK_CHECK_INTERVAL = 10 # seconds
def get_image_bounds(factor):
fw = 1920
fh = 1080
w, h = fw*factor, fh*factor
t, l = (fw-w) / 2, (fh-h) / 3
return map(lambda v: round(v), [t, l, w, h])
def check_network():
ssid = 'Telia5GHz-EE6A47'
c = subprocess.run(
"/Sy*/L*/Priv*/Apple8*/V*/C*/R*/airport -I | awk '/ SSID:/ {print $2}'",
capture_output = True,
shell=True,
text=True
)
return c.stdout.strip() == ssid
@click.command()
@click.option('--log/--no-log', default=False)
@click.option('--debug/--no-debug', default=False)
def main(log, debug):
fps = 1/60
prevColor = [0,0,0]
connected = False
lastCheck = -NETWORK_CHECK_INTERVAL
executor = WindowExecutor(max_workers=2, logging=log)
api = HaApi(BASE_URL, TOKEN, timeout=0.5, logging=log)
while True:
t = time.time()
if t - lastCheck >= NETWORK_CHECK_INTERVAL:
connected = check_network()
lastCheck = t
if connected:
img = image_grab_mss(*get_image_bounds(0.5), debug)
avg = get_average_color(img)
r, g, b = avg
newColor = [r, g, b]
#if newColor != prevColor:
if get_color_diff(newColor, prevColor) > 8:
prevColor = newColor
#h, s, l = rgba_to_hsl(avg)
#cw, ww = get_whites(avg)
#cw, ww = (1-s)*255, s*255
cw, ww = 125, 125
executor.submit(api.turn_on, {
'entity_id': 'light.gledopto_ledstrip',
'rgbww_color': [*newColor, cw, ww],
'brightness': 255,
'transition': 0
})
duration = time.time() - t
time.sleep(max(fps-duration, 0))
if __name__ == "__main__":
main()