forked from hvanderlaan/philips-hue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hue.sh
executable file
·383 lines (313 loc) · 12.5 KB
/
hue.sh
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#!/bin/bash
# hue.sh: script for interacting with the philips hue light.
#
# author : Harald van der Laan
# version : v0.6.0
# date : 12/jul/2016
#
# inplemented features:
# - powering a hue lightbulb or group
# - changing the saturation of the lightbulb or group
# - changing the brightness of the lightbulb or group
# - changing the hue of a lightbulb or group
# - changing the xy gamut of a lightbulb or group
# - changing the ct temperature of a lightbulb or group
# - demo the colors of the hue system
#
# usage: hue.sh <light|group> <number> <action> <value> [<value>]
# power usage: hue.sh light 1 state <on|off>
# saturation : hue.sh light 1 sat <0-255>
# brightness : hue.sh light 1 bri <0-255>
# hue : hue.sh light 1 hue <0-65535>
# xy : hue.sh light 1 xy <0.0-1.0> <0.0-1.0>
# ct : hue.sh light 1 ct <153-500>
#
# changelog:
# - v0.1 (initial release) (HLA)
#
# - v0.2 Added hue cycle mode, this will cycle
# through the color spectrum of the hue
# lightbulb or group (HLA)
#
# - v0.3 Added xy gamut change option. for more
# info about gamut please go to the hue
# api development page. (HLA)
#
# - v0.3.1 Added extra check for curl package (HLA)
#
# - v0.4 Added ct (color temperature) (HLA)
#
# - v0.5 Fixed hueJsonData layout and redirect
# curl output > /dev/nulll
#
# - v0.5.1 Changed brightness setting in cycle (HLA)
#
# - v0.6.0 Added huePort for enter a no standard
# port for the hue bridge (HLA)
# global variables
hueBridge='localhost'
huePort='80'
hueApiHash='newdeveloper'
hueBaseUrl="http://${hueBridge}:${huePort}/api/${hueApiHash}"
hueTimeOut='5'
# functions
function usage() {
clear
echo "Usage: hue.sh <light|group> <number> <action> <value> [<value>]"
echo "=========================================================================="
echo "power usage : hue.sh light 1 state <on|off>"
echo "saturation : hue.sh light 1 sat <0-255>"
echo "brightness : hue.sh light 1 bri <0-255>"
echo "hue : hue.sh light 1 hue <0-65535>"
echo "xy gamut : hue.sh light 1 xy <0.0-1.0> <0.0-1.0>"
echo "ct color temp : hue.sh light 1 ct <153-500>"
echo "color cycle : hue.sh light 1 cycle <0-65535> <0-65535>"
exit 1
}
function huePower() {
local hueType=${1}
local hueTypeNumber=${2}
local hueState=${3}
if [[ ${hueTypeNumber} != *[[:digit:]]* ]]; then
echo "[-] Hue: ${hueType} number: ${hueTypeNumber} is not a number."
exit 1
fi
case ${hueType} in
light) hueUrl="${hueBaseUrl}/lights/${hueTypeNumber}/state" ;;
group) hueUrl="${hueBaseUrl}/groups/${hueTypeNumber}/state" ;;
*) echo "[-] Hue: The hue device mode is not light or group."; exit 1 ;;
esac
case ${hueState} in
on) hueJsonData='{"on":true}' ;;
off) hueJsonData='{"on":false}' ;;
*) echo "[-] Hue: The hue state can only be on or off."; exit 1 ;;
esac
curl --max-time ${hueTimeOut} --silent --request PUT --data ${hueJsonData} ${hueUrl} &> /dev/null
if [ ${?} -ne 0 ]; then
echo "[-] Hue: Failed to send power command to ${hueType}/${hueTypeNumber}."
exit 1
fi
echo "[+] Hue: Power command send successfully to ${hueType}/${hueTypeNumber}."
}
function hueSaturation() {
local hueType=${1}
local hueTypeNumber=${2}
local hueState=${3}
if [[ ${hueTypeNumber} != *[[:digit:]]* ]]; then
echo "[-] Hue: ${hueType} number: ${hueTypeNumber} is not a number."
exit 1
fi
case ${hueType} in
light) hueUrl="${hueBaseUrl}/lights/${hueTypeNumber}/state" ;;
group) hueUrl="${hueBaseUrl}/groups/${hueTypeNumber}/state" ;;
*) echo "[-] Hue: The Hue device mode is not light or group."; exit 1 ;;
esac
if [[ ${hueState} != *[[:digit:]]* ]]; then
echo "[-] Hue: Saturation value: ${hueState} is not a number."
exit 1
fi
if [ ${hueState} -lt 0 -o ${hueState} -gt 255 ]; then
echo "[-] Hue: Saturation value must be between 0 and 255."
exit 1
fi
curl --max-time ${hueTimeOut} --silent --request PUT --data '{"sat":'${hueState}'}' ${hueUrl} &> /dev/null
if [ ${?} -ne 0 ]; then
echo "[-] Hue: Failed to send saturation command to ${hueType}/${hueTypeNumber}."
exit 1
fi
echo "[+] Hue: Saturation command send successfully to ${hueType}/${hueTypeNumber}."
}
function hueBrightness() {
local hueType=${1}
local hueTypeNumber=${2}
local hueState=${3}
if [[ ${hueTypeNumber} != *[[:digit:]]* ]]; then
echo "[-] Hue: ${hueType} number: ${hueTypeNumber} is not a number."
exit 1
fi
case ${hueType} in
light) hueUrl="${hueBaseUrl}/lights/${hueTypeNumber}/state" ;;
group) hueUrl="${hueBaseUrl}/groups/${hueTypeNumber}/state" ;;
*) echo "[-] Hue: The Hue device mode is not light or group."; exit 1 ;;
esac
if [[ ${hueState} != *[[:digit:]]* ]]; then
echo "[-] Hue: Brightness value: ${hueState} is not a number."
exit 1
fi
if [ ${hueState} -lt 0 -o ${hueState} -gt 255 ]; then
echo "[-] Hue: Brightness value must be between 0 and 255."
exit 1
fi
curl --max-time ${hueTimeOut} --silent --request PUT --data '{"bri":'${hueState}'}' ${hueUrl} &> /dev/null
if [ ${?} -ne 0 ]; then
echo "[-] Hue: Failed to send brightness command to ${hueType}/${hueTypeNumber}."
exit 1
fi
echo "[+] Hue: Brightness command send successfully to ${hueType}/${hueTypeNumber}."
}
function hueHue() {
local hueType=${1}
local hueTypeNumber=${2}
local hueState=${3}
if [[ ${hueTypeNumber} != *[[:digit:]]* ]]; then
echo "[-] Hue: ${hueType} number: ${hueTypeNumber} is not a number."
exit 1
fi
case ${hueType} in
light) hueUrl="${hueBaseUrl}/lights/${hueTypeNumber}/state" ;;
group) hueUrl="${hueBaseUrl}/groups/${hueTypeNumber}/state" ;;
*) echo "[-] Hue: The Hue device mode is not light or group."; exit 1 ;;
esac
if [[ ${hueState} != *[[:digit:]]* ]]; then
echo "[-] Hue: Hue value: ${hueState} is not a number."
exit 1
fi
if [ ${hueState} -lt 0 -o ${hueState} -gt 65535 ]; then
echo "[-] Hue: Hue value must be between 0 and 65535."
exit 1
fi
curl --max-time ${hueTimeOut} --silent --request PUT --data '{"hue":'${hueState}'}' ${hueUrl} &> /dev/null
if [ ${?} -ne 0 ]; then
echo "[-] Hue: Failed to send hue command to ${hueType}/${hueTypeNumber}."
exit 1
fi
echo "[+] Hue: Hue command send successfully to ${hueType}/${hueTypeNumber}."
}
function hueXy() {
local hueType=${1}
local hueTypeNumber=${2}
local hueState1=${3}
local hueState2=${4}
if [[ ${hueTypeNumber} != *[[:digit:]]* ]]; then
echo "[-] Hue: ${hueType} number: ${hueTypeNumber} is not a number."
exit 1
fi
case ${hueType} in
light) hueUrl="${hueBaseUrl}/lights/${hueTypeNumber}/state" ;;
group) hueUrl="${hueBaseUrl}/groups/${hueTypeNumber}/state" ;;
*) echo "[-] Hue: The xy device mode is not light or group."; exit 1 ;;
esac
if [[ ${hueState1} != *[[:digit:]]* ]]; then
echo "[-] Hue: Xy value1: ${hueState1} is not a number."
exit 1
fi
if [[ ${hueState2} != *[[:digit:]]* ]]; then
echo "[-] Hue: Xy value2: ${hueState2} is not a number."
exit 1
fi
if (( $(bc <<< "${hueState1} <= 0") == 1 )) || (( $(bc <<< "${hueState1} >= 1") == 1 )); then
echo "[-] Hue: Xy value1 must be between 0.0 and 1.0."
exit 1
fi
if (( $(bc <<< "${hueState2} <= 0") == 1 )) || (( $(bc <<< "${hueState2} >= 1") == 1 )); then
echo "[-] Hue: Xy value2 must be between 0.0 and 1.0."
exit 1
fi
curl --max-time ${hueTimeOut} --silent --request PUT --data '{"xy":['${hueState1}','${hueState2}']}' ${hueUrl} &> /dev/null
if [ ${?} -ne 0 ]; then
echo "[-] Hue: Failed to send xy command to ${hueType}/${hueTypeNumber}."
exit 1
fi
echo "[+] Hue: Xy command send successfully to ${hueType}/${hueTypeNumber}."
}
function hueCt() {
local hueType=${1}
local hueTypeNumber=${2}
local hueState=${3}
if [[ ${hueTypeNumber} != *[[:digit:]]* ]]; then
echo "[-] Hue: ${hueType} number: ${hueTypeNumber} is not a number."
exit 1
fi
case ${hueType} in
light) hueUrl="${hueBaseUrl}/lights/${hueTypeNumber}/state" ;;
group) hueUrl="${hueBaseUrl}/groups/${hueTypeNumber}/state" ;;
*) echo "[-] Hue: The Hue device mode is not light or group."; exit 1 ;;
esac
if [[ ${hueState} != *[[:digit:]]* ]]; then
echo "[-] Hue: Ct value: ${hueState} is not a number."
exit 1
fi
if [ ${hueState} -lt 153 -o ${hueState} -gt 500 ]; then
echo "[-] Hue: Ct value must be between 0 and 255."
exit 1
fi
curl --max-time ${hueTimeOut} --silent --request PUT --data '{"ct":'${hueState}'}' ${hueUrl} &> /dev/null
if [ ${?} -ne 0 ]; then
echo "[-] Hue: Failed to send ct command to ${hueType}/${hueTypeNumber}."
exit 1
fi
echo "[+] Hue: Ct command send successfully to ${hueType}/${hueTypeNumber}."
}
function hueCycle() {
local hueType=${1}
local hueTypeNumber=${2}
local hueState1=${3}
local hueState2=${4}
if [[ ${hueTypeNumber} != *[[:digit:]]* ]]; then
echo "[-] Hue: ${hueType} number: ${hueTypeNumber} is not a number."
exit 1
fi
case ${hueType} in
light) hueUrl="${hueBaseUrl}/lights/${hueTypeNumber}/state" ;;
group) hueUrl="${hueBaseUrl}/groups/${hueTypeNumber}/state" ;;
*) echo "[-] Hue: The cycle device mode is not light or group."; exit 1 ;;
esac
if [[ ${hueState1} != *[[:digit:]]* ]]; then
echo "[-] Hue: Cycle value1: ${hueState1} is not a number."
exit 1
fi
if [[ ${hueState2} != *[[:digit:]]* ]]; then
echo "[-] Hue: Cycle value2: ${hueState2} is not a number."
exit 1
fi
if [ ${hueState1} -lt 0 -o ${hueState1} -gt 65535 ]; then
echo "[-] Hue: Cycle value1 must be between 0 and 65535."
exit 1
fi
if [ ${hueState2} -lt 0 -o ${hueState2} -gt 65535 ]; then
echo "[-] Hue: Cycle value2 must be between 0 and 65535."
exit 1
fi
if [ ${hueState1} -ge ${hueState2} ]; then
echo "[-] Hue: Cycle value1 must be smaller then cycle value2."
exit 1
fi
curl --max-time ${hueTimeOut} --silent --request PUT --data '{"on":true,"bri":254,"hue":54000,"sat":255}' ${hueUrl} &> /dev/null
if [ ${?} -ne 0 ]; then
echo "[-] Hue: Failed to send reset command to ${hueType}/${hueTypeNumber}."
fi
for (( hueValue=${hueState1}; hueValue<=${hueState2}; hueValue+=1000 )); do
curl --max-time ${hueTimeOut} --silent --request PUT --data '{"hue":'${hueValue}'}' ${hueUrl} &> /dev/null
if [ ${?} -ne 0 ]; then
echo "[-] Hue: Failed to send cycle command to ${hueType}/${hueTypeNumber}, Hue is: ${hueValue}."
else
echo "[ ] Hue: Cycle command successfully send to ${hueType}/${hueTypeNumber}, Hue is: ${hueValue}."
fi
sleep 1
done
}
# main script
if [ ${#} -lt 4 ]; then
usage
fi
if [ -z $(which curl) ]; then
echo "[-] Hue: curl is not installed. This script needs curl to communicate with the hue api."
echo "[-] Hue: Please install curl and try again."
exit 1
fi
hueDevice=${1}
hueDeviceNumber=${2}
hueDeviceAction=${3}
hueDeviceActionValue1=${4}
hueDeviceActionValue2=${5}
case ${hueDeviceAction} in
state) huePower ${hueDevice} ${hueDeviceNumber} ${hueDeviceActionValue1} ;;
sat) hueSaturation ${hueDevice} ${hueDeviceNumber} ${hueDeviceActionValue1} ;;
bri) hueBrightness ${hueDevice} ${hueDeviceNumber} ${hueDeviceActionValue1} ;;
hue) hueHue ${hueDevice} ${hueDeviceNumber} ${hueDeviceActionValue1} ;;
xy) hueXy ${hueDevice} ${hueDeviceNumber} ${hueDeviceActionValue1} ${hueDeviceActionValue2} ;;
ct) hueCt ${hueDevice} ${hueDeviceNumber} ${hueDeviceActionValue1} ;;
cycle) hueCycle ${hueDevice} ${hueDeviceNumber} ${hueDeviceActionValue1} ${hueDeviceActionValue2} ;;
*) usage ;;
esac
exit 0