This repository has been archived by the owner on Jan 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
phue.sh
executable file
·304 lines (267 loc) · 7.9 KB
/
phue.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
# MIT License
#
# Copyright (c) 2018 Nicola Worthington <nicolaw@tfb.net>.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# https://developers.meethue.com/documentation/important-whitelist-changes
# https://developers.meethue.com/documentation/datatypes-and-time-patterns
# https://developers.meethue.com/philips-hue-api
_phue_curl () {
curl -ksSL -H "Content-Type: application/json" "$@" | {
declare rc=${PIPESTATUS[0]};
if [[ ! ${PHUE_NOJQ:-} ]] && type -P jq >/dev/null ; then
jq .
else
cat
fi
return $rc
}
}
_phue_join () {
local IFS="${1:-/}"; shift
echo "$*"
}
phue_read_credentials () {
declare cfg=""
for cfg in "$@" ~/.bash_phue /etc/bash_phue /usr/local/etc/bash_phue ; do
if [[ -e "$cfg" ]] ; then
eval "$(grep -E '^PHUE_[A-Z0-9_]+=[a-zA-Z0-9\.:]+$' "$cfg")"
export PHUE_BRIDGE PHUE_USERNAME PHUE_NOJQ
break
fi
done
}
phue_username () {
if [[ -n "${1:-}" ]] ; then
export PHUE_USERNAME="$1"
elif [[ -z "${PHUE_USERNAME:-}" ]] ; then
phue_read_credentials
fi
echo "${PHUE_USERNAME:-}"
}
phue_bridge () {
if [[ -n "${1:-}" ]] ; then
shopt -s nocasematch
case "${1:-}" in
discover)
phue_bridge_discover
return $?
;;
*)
export PHUE_BRIDGE="$1"
;;
esac
elif [[ -z "${PHUE_BRIDGE:-}" ]] ; then
phue_read_credentials
fi
echo "${PHUE_BRIDGE:-}"
}
phue_bridge_discover () {
_phue_curl https://www.meethue.com/api/nupnp
}
phue_create_username () {
declare app_name="${1:-bash_phue.sh}"
declare device_name="${2:-$(hostname)}"
PHUE_USERNAME="" phue POST \
"$(printf '{"devicetype":"%s#%s"}' "$app_name" "$device_name")"
}
phue_info () {
phue GET info "$@"
}
phue_capabilities () {
phue GET capabilities
}
phue () {
declare cmd="${1:-}"; shift
shopt -s nocasematch
case "$cmd" in
# Perform basic despatching first.
bridge_discover) phue_bridge discover ;;
bridge) phue_bridge "$@" ;;
username) phue_username "$@" ;;
register|link|create_username) phue_create_username "$@" ;;
info) phue_info "$@" ;;
capabilities) phue_capabilities "$@" ;;
lights|light) phue_lights "$@" ;;
on|off|hue|bri|sat|ct|transitiontime|bri_inc|sat_inc|hue_inc|ct_inc|xy|alert|effect|xy_inc|state) phue_lights "$cmd" "$@" ;;
*)
# Validate global variables required for JSON REST RPC.
if [[ -z "${PHUE_BRIDGE:-}" ]] ; then
>&2 echo "Error; PHUE_BRIDGE is not defined!"
return 1
fi
if [[ -z "${PHUE_USERNAME:-}" \
&& "${FUNCNAME[1]}" != "phue_create_username" ]] ; then
>&2 echo "Error; PHUE_USERNAME is not defined!"
return 1
fi
declare query_url=""
declare query_body=""
declare api_url="$(_phue_join "/" \
"https://${PHUE_BRIDGE:-}" api ${PHUE_USERNAME:-})"
case "$cmd" in
GET|DELETE)
query_url="$(_phue_join "/" "$api_url" "$@")"
_phue_curl -X "$cmd" "$query_url"
;;
PUT|POST)
query_body="${1:-}"; shift
query_url="$(_phue_join "/" "$api_url" "$@")"
_phue_curl -X "$cmd" --data "$query_body" "$query_url"
;;
*)
>&2 echo "Unknown command ${cmd}"
;;
esac
esac
}
# https://developers.meethue.com/documentation/datatypes-and-time-patterns
_phue_bool_invert () {
case "${1:-}" in
true) echo false ;;
false) echo true ;;
*) echo "${1:-}" ;;
esac
}
phue_light () { phue_lights "$@"; }
phue_lights () {
# https://developers.meethue.com/documentation/lights-api
declare method="${1:-}"; shift || true
declare id=""
shopt -s nocasematch
case "$method" in
list)
phue_lights | jq -r '.|to_entries[]|.key + "\t" + .value.name'
;;
get|"")
# GET /api/<username>/lights Get all lights
# GET /api/<username>/lights/<id> Get light attributes and state
if [[ $# -eq 0 || "$*" =~ ^\ *$ ]] ; then
phue GET lights
else
for id in "$@" ; do
phue GET lights "$id"
done
fi
;;
new)
# GET /api/<username>/lights/new Get new lights
# POST* /api/<username>/lights Search for new lights
if [[ $# -eq 0 || "$*" =~ ^\ *$ ]] ; then
phue GET lights new
else
declare body="${1:-}"
phue POST "$body" lights new
fi
;;
delete)
# DELETE /api/<username>/lights/<id> Delete lights
for id in "$@" ; do
phue DELETE lights "$id"
done
;;
name|rename)
# PUT* /api/<username>/lights/<id> Set light attributes (rename)
declare body="${1:-}"; shift || true
for id in "$@" ; do
phue PUT "$body" lights "$id"
break
done
;;
state)
# PUT* /api/<username>/lights/<id>/state Set light state
declare body="${1:-}"; shift || true
for id in "$@" ; do
phue PUT "$body" lights "$id" state
done
;;
on|off)
declare body="true"
if [[ ! "${1:-}" =~ ^[0-9]+$ ]] ; then
body="$1"; shift || true
fi
case "$method" in
off) method="on"; body="$(_phue_bool_invert "${body:-true}")" ;;
esac
phue_lights state "$(printf '{"%s":%s}' "$method" "$body")" "$@"
;;
bri|hue|sat|ct|transitiontime|bri_inc|sat_inc|hue_inc|ct_inc)
declare body="${1:-}"; shift || true
phue_lights state "$(printf '{"%s":%s}' "$method" "$body")" "$@"
;;
xy|alert|effect|xy_inc)
declare body="${1:-}"; shift || true
phue_lights state "$(printf '{"%s":"%s"}' "$method" "$body")" "$@"
;;
*)
>&2 echo "Error; unknown command argument '$method'!"
return 1
;;
esac
}
_phue_help () {
echo "Syntax: phue <command> [arguments] [light-id...n]"
echo ""
echo "Commands: bridge, register, username, lights, info, capabilities."
#echo "Commands: lights, groups, schedules, scenes, sensors, rules,"
#echo " configuration, info, resourcelinks, capabilities."
echo ""
if [[ $# -ge 1 ]] ; then
declare note=""
for note in "$@" ; do
echo "$note"
done
echo ""
fi
echo "See https://github.com/neechbear/bash_phue for help."
}
_phue_main () {
declare arg=""
for arg in "$@" ; do
case "$arg" in
--help|-h|-?)
# Display some command line help.
_phue_help
return $?
;;
esac
done
# Check and load configuration.
phue_bridge >/dev/null
if [[ -z "${PHUE_BRIDGE:-}" \
&& "$*" != "bridge discover"
&& "$*" != "bridge_discover" ]] ; then
_phue_help \
"Environment variable PHUE_BRIDGE and PHUE_USERNAME are unset." \
"Set PHUE_BRIDGE or try '$0 bridge discover'."
return 1
fi
# Passthrough to phue function.
if [[ $# -le 0 ]] && type -P jq >/dev/null ; then
phue lights list
else
phue "$@"
fi
}
_phue_is_sourced () {
[[ "${FUNCNAME[1]}" == "source" ]]
}
if ! _phue_is_sourced ; then
_phue_main "$@"
fi