-
Notifications
You must be signed in to change notification settings - Fork 1
/
hue_brightness_alter.py
executable file
·124 lines (103 loc) · 3.37 KB
/
hue_brightness_alter.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
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python3
"""Alters brightness of the Hue Lamp(s)."""
import sys
import termios
import tty
import math
from termcolor import colored
from hue_class import HueLamp
from hue_config import lamp_dict
for a, n in lamp_dict.items():
globals()[n] = HueLamp(a, n)
def get_key():
"""function to get keyboard key without enter."""
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(fd)
key = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return key
def brightness_alter(hl):
bri = 1
already_send = "no"
bri_sum = 0
for lamp in hl:
hl_obj = globals()[lamp]
print('The current brightness of lamp %s = %s.' % (colored(hl_obj.name, "yellow"), colored(hl_obj.brightness_get(), "green")))
bri_sum += hl_obj.brightness_get()
print()
bri = int(math.floor(bri_sum / len(hl)))
print('Changing brightness of selected lamps start at their average of %s.\n' % (colored(bri, "cyan")))
print("Please input [+/-/q]: ")
while True:
key = get_key()
if key == "+":
bri += 5
if bri >= 254:
bri = 254
print("%s = maximum brightness" % colored(bri, 'red'))
if already_send == "no":
for lamp in hl:
hl_obj = globals()[lamp]
hl_obj.brightness_set(bri)
already_send = "yes"
else:
print(colored(bri, 'green'))
for lamp in hl:
hl_obj = globals()[lamp]
hl_obj.brightness_set(bri)
already_send = "no"
elif key == "-":
bri -= 5
if bri <= 1:
bri = 1
print("%s = minimum brightness" % colored(bri, 'red'))
if already_send == "no":
for lamp in hl:
hl_obj = globals()[lamp]
hl_obj.brightness_set(bri)
already_send = "yes"
else:
print(colored(bri, 'green'))
for lamp in hl:
hl_obj = globals()[lamp]
hl_obj.brightness_set(bri)
already_send = "no"
elif key == "q":
print("\nbye bye (;-)\n")
break
else:
print("Please just use + or - or (q)uit.")
print("\n###### Alter the lamp's brightness ######\n")
s = "Where to send ? "
for name in lamp_dict.values():
y = name[5:]
s = s + "%s " % y.replace(y[0], "(%s)" % y[0], 1)
if len(lamp_dict) > 1:
s = s + "or (a)ll : "
else:
s = s + " : "
while True:
kbd_inp = input(s)
print()
first_letter_list = [y[5] for y in lamp_dict.values()]
if kbd_inp in first_letter_list:
k = [y for y in lamp_dict.values() if kbd_inp == y[5]]
brightness_alter(k)
break
elif kbd_inp == "a":
brightness_alter([y for y in lamp_dict.values()])
break
else:
s = "Please just input "
for name in lamp_dict.values():
s = s + "(%s) " % name[5]
if len(lamp_dict) > 1:
s = s + "or (a) : "
else:
s = s + " : "
for lamp in lamp_dict.values():
hl_obj = globals()[lamp]
hl_obj.prop_chg_notify.kill()