-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhue.py
executable file
·134 lines (125 loc) · 4.64 KB
/
hue.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
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/python
import sys, os, re, string, datetime, logging, time, random
sys.path.append('/home/house/src/house/hue/python-hue')
import hue
hue.logger.setLevel(logging.WARNING)
#hue.logger.setLevel(logging.INFO)
#hue.logger.setLevel(logging.DEBUG)
logger = hue.logger
def time_to_color():
hour = datetime.datetime.now().hour + datetime.datetime.now().minute/60.0
temp = 2000
if hour > 4 and hour <= 7:
# ramp from 2000K up to 6500K from 4:00 to 7:00
temp = max(2000, min(6500,2000+(4500/3)*(hour-4)))
elif hour > 7 and hour < 23:
# ramp from 6500K down to 2000K from 12:00 to 21:00
temp = max(2000, min(6500, 5000-(3000/6)*(hour-15)))
return int(temp)
h = hue.Hue() # Initialize the class
h.station_ip = "hue.example.com" # Your base station IP
h.client_identifier = 'ffff__put_station_id_here_ffff' # put your client identifier / auth token here
h.get_state()
def update_lightset(arg):
hue.logger.warn("Using lightset "+arg)
if arg == 'O':
return ('l1','l2','l3')
elif arg == 'N':
return ('l4','l5','l6')
elif arg == 'L':
return ('l7',)
else:
return ('l1','l2','l3')
lightset = ('l1','l2','l3')
lightsetall = ('l1','l2','l3','l4','l5','l6','l7')
colortemp = time_to_color()
if sys.argv[1] == 'state' or sys.argv[1] == 'status':
for x in lightsetall:
light = h.lights.get(x)
light.update_state_cache()
#print light, dir(light), light.state
state = light.state['state']
print "%s name='%s' on=%s reachable=%s colormode=%s bri=%d sat=%d ct=%d xy=%s"%(x,
light.state['name'],
str(state['on']),
str(state['reachable']),
str(state['colormode']),
int(state['bri']),
int(state['sat']),
int(1e6/(state['ct']+1)),
str(state['xy']))
elif sys.argv[1] == 'redshift':
for x in lightsetall:
light = h.lights.get(x)
light.update_state_cache()
state = light.state['state']
if state['reachable'] and state['on'] and state['colormode'] == 'ct':
logger.info("Shifting %s from %d to %d"%(x, int(1e6/state['ct']), colortemp))
light.cct(colortemp)
else:
logger.info("Skipping %s due to %s and %s"%(x, state['on'], state['colormode']))
elif sys.argv[1] == 'on':
if len(sys.argv) > 2:
lightset = update_lightset(sys.argv[2])
for x in lightset:
h.lights.get(x).on().cct(colortemp).bri(255)
elif sys.argv[1] == 'dim':
if len(sys.argv) > 2:
lightset = update_lightset(sys.argv[2])
if len(sys.argv) == 3:
dim = int(sys.argv[2])
else:
dim = 100
for x in lightset:
h.lights.get(x).on().bri(dim)
elif sys.argv[1] == 'candle':
if len(sys.argv) > 2:
lightset = update_lightset(sys.argv[2])
#for x in ('l1','l2'):
# h.lights.get(x).off()
for x in lightset:
h.lights.get(x).on().xy(.5767, 0.383).bri(20)
ctr = 10000
while ctr > 0:
ctr -= 1
x = lightset[random.randrange(0,len(lightset))]
h.lights.get(x).on().bri(int(20+30*random.random()))
time.sleep(random.random()/5.0)
h.lights.get(x).on().bri(20)
time.sleep(random.random()/3.0)
# bri=20 sat=236
elif sys.argv[1] == 'red':
if len(sys.argv) > 2:
lightset = update_lightset(sys.argv[2])
for x in lightset:
#h.lights.get(x).on().rgb('#ff0000').bri(10).on().bri(10)
h.lights.get(x).on().bri(5).xy(0.674, 0.322).bri(10).on().bri(5)
elif sys.argv[1] == 'ramp':
if len(sys.argv) > 2:
lightset = update_lightset(sys.argv[2])
ramptime = 250.0
newct = 2000
rampincr = 2
for x in lightset:
h.lights.get(x).on().cct(newct).bri(5)
for bri in range(10,255,rampincr):
newct = int((colortemp-2000)/255.*bri+2000)
print bri, newct
for x in lightset:
h.lights.get(x).bri(bri).cct(newct)
time.sleep(ramptime/(245./rampincr))
elif sys.argv[1] == 'color':
if len(sys.argv) == 3:
color = sys.argv[2]
else:
color = "#ff0000"
for x in lightset:
h.lights.get(x).on().rgb('#'+color)
elif sys.argv[1] == 'off':
if len(sys.argv) > 2:
lightset = update_lightset(sys.argv[2])
for x in lightset:
h.lights.get(x).off()
else:
print "Unknown command"
sys.exit(-1)