-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathautolitt.py
111 lines (95 loc) · 3.19 KB
/
autolitt.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import json
import sys
import os
import traceback
import pyscreenshot as image_grab
from tuyapy import TuyaApi
from colorthief import ColorThief
api = TuyaApi()
def turn_off(device):
if not isinstance(device, list):
return device.turn_off()
else:
return [i.turn_off() for i in device]
def turn_on(device):
if not isinstance(device, list):
return device.turn_on()
else:
return [i.turn_on() for i in device]
def change_color(device, r, g, b):
# Manually adjusting for black white and gray
h, s, v = rgb_to_hsv(r, g, b)
if v < 5 or s < 10:
print("Adjusting from {} to {}".format(str(v),str((210, 4 , 19))))
h = 210
s = 4
v = 19
change_color_from_hsv(device, h, s, v)
def change_color_from_rgb(device, r, g, b):
if not isinstance(device, list):
return device.set_color(rgb_to_hsv(r, g, b))
else:
return [i.set_color(rgb_to_hsv(r, g, b)) for i in device]
def change_color_from_hsv(device, h, s, v):
if not isinstance(device, list):
return device.set_color((h, s, v))
else:
return [i.set_color((h, s, v)) for i in device]
def rgb_to_hsv(r, g, b):
# Reference: https://www.w3resource.com/python-exercises/math/python-math-exercise-77.php
r, g, b = r / 255.0, g / 255.0, b / 255.0
mx = max(r, g, b)
mn = min(r, g, b)
df = mx - mn
h = 0
if mx == mn:
h = 0
elif mx == r:
h = (60 * ((g - b) / df) + 360) % 360
elif mx == g:
h = (60 * ((b - r) / df) + 120) % 360
elif mx == b:
h = (60 * ((r - g) / df) + 240) % 360
if mx == 0:
s = 0
else:
s = (df / mx) * 100
v = mx * 100
return h, s, v
if __name__ == '__main__':
with open('config.json') as config:
data = json.load(config)
username = data['username']
password = data['password']
country_code = data['country_code']
application = data['application']
print("Logging in for {}".format(username))
api.init(username, password, country_code, application)
device_ids = api.get_all_devices()
lights = dict(sorted(dict((i.name(), i) for i in device_ids if i.obj_type == 'light').items()))
devices = {**lights}
print(devices)
devices_list = [*devices.values()]
old_color = (0, 0, 0)
while True:
try:
# TODO get screensize and set dynamic margin
fss = image_grab.grab(bbox=(204, 115, 1162, 653))
color_thief = ColorThief(fss, is_obj=True)
# get the dominant color
dominant_color = color_thief.get_color(quality=5, ignore_white=True, ignore_black=True)
if old_color == dominant_color:
continue
else:
change_color(devices_list, dominant_color[0], dominant_color[1], dominant_color[2])
print("Changed colour to " + str(dominant_color))
old_color = dominant_color
except KeyboardInterrupt:
print('OK bye!')
try:
sys.exit(0)
except SystemExit:
os._exit(0)
except Exception as e:
print("Failed to due to " + repr(e))
print(traceback.format_exc())