-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwink.sh
executable file
·312 lines (261 loc) · 6.69 KB
/
wink.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
#!/usr/bin/env bash
# This script uses the Wink API to control IOT enabled devices
# Requires jq, curl
# Set credentials
. ./set-credentials.sh
# Device menu
. ./menu.sh
# Functions
function check_command {
type -P $1 &>/dev/null || fail "Unable to find $1, please install it and run this script again."
}
function fail(){
tput setaf 1; echo "Failure: $*" && tput sgr0
exit 1
}
function failwithoutexit(){
tput setaf 1; echo "Failure: $*" && tput sgr0
}
function pause(){
read -p "Press any key to continue..."
echo
}
function linkedServices(){
linkedServices=$(curl -s "$APIURL"/users/me/linked_services \
-H "Authorization: Bearer $access_token")
echo $linkedServices | jq .
}
function retrieveAllDevices(){
retrieveAllDevices=$(curl -s "$APIURL"/users/me/wink_devices \
-H "Authorization: Bearer $access_token")
echo $retrieveAllDevices | jq .
}
function retrieveAirConditioners(){
retrieveAirConditioners=$(curl -s "$APIURL"/users/me/air_conditioners \
-H "Authorization: Bearer $access_token")
# echo $retrieveAllDevices | jq .
}
function getRobots(){
getRobots=$(curl -s "$APIURL"/users/me/robots \
-H "Authorization: Bearer $access_token")
echo $getRobots | jq .
}
function retrieveLightBulbIDs(){
retrieveLightBulbIDs=$(curl -s "$APIURL"/users/me/light_bulbs \
-H "Authorization: Bearer $access_token")
bulbIDs=$(echo $retrieveLightBulbIDs | jq . | grep light_bulb_id | cut -d \" -f4)
}
# Turn a light bulb on or off and set brightness level
function updateLightbulb(){
updateLightbulb=$(curl -sX PUT "$APIURL"/light_bulbs/"$1" \
-H "Authorization: Bearer $access_token" \
-H "Content-Type: application/json" \
--data-binary '{
"desired_state": {
"powered": "'"$2"'",
"brightness": "'"$3"'"
}
}')
result=$(echo $updateLightbulb | jq '.errors != null')
if [ $result = "true" ]; then
name=$(echo $updateLightbulb | jq '.data | .name')
tput setaf 2; echo $name Successful && tput sgr0
else
failwithoutexit $(echo $updateLightbulb | jq '.errors')
fi
}
function lightsOn(){
echo Turn all Lightbulbs On...
for bulbid in $bulbIDs
do
updateLightbulb $bulbid true 1
done
}
function lightsOff(){
echo Turn all Lightbulbs Off...
for bulbid in $bulbIDs
do
updateLightbulb $bulbid false 0
done
}
function fadeLightsOn(){
echo Fade all Lightbulbs On...
for brightnesslevel in 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
do
for bulbid in $bulbIDs
do
updateLightbulb $bulbid true $brightnesslevel
sleep 1
done
done
}
function fadeLightsOff(){
echo Fade all Lightbulbs Off...
for brightnesslevel in 1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0
do
for bulbid in $bulbIDs
do
updateLightbulb $bulbid true $brightnesslevel
sleep 1
done
done
for bulbid in $bulbIDs
do
updateLightbulb $bulbid false 0
done
}
function retrieveACIDs(){
retrieveACIDs=$(curl -s "$APIURL"/users/me/air_conditioners \
-H "Authorization: Bearer $access_token")
ACIDs=$(echo $retrieveACIDs | jq . | grep air_conditioner_id | cut -d \" -f4)
}
function updateAC(){
updateAC=$(curl -sX PUT "$APIURL"/air_conditioners/"$1" \
-H "Authorization: Bearer $access_token" \
-H "Content-Type: application/json" \
--data-binary '{
"desired_state": {
"powered": "'"$2"'",
"fan_speed": "'"$3"'",
"mode": "'"$4"'",
"max_set_point": "'"$5"'"
}
}')
result=$(echo $updateAC | jq '.errors != null')
if [ $result = "true" ]; then
name=$(echo $updateAC | jq '.data | .name')
tput setaf 2; echo $name Successful && tput sgr0
else
failwithoutexit $(echo $updateAC | jq '.errors')
fi
}
function updateACmode(){
updateACmode=$(curl -sX PUT "$APIURL"/air_conditioners/"$1" \
-H "Authorization: Bearer $access_token" \
-H "Content-Type: application/json" \
--data-binary '{
"desired_state": {
"powered": "'"$2"'",
"mode": "'"$3"'"
}
}')
result=$(echo $updateACmode | jq '.errors != null')
if [ $result = "true" ]; then
name=$(echo $updateACmode | jq '.data | .name')
tput setaf 2; echo $name Successful && tput sgr0
else
failwithoutexit $(echo $updateACmode | jq '.errors')
fi
}
function updateACtemp(){
updateACtemp=$(curl -sX PUT "$APIURL"/air_conditioners/"$1" \
-H "Authorization: Bearer $access_token" \
-H "Content-Type: application/json" \
--data-binary '{
"desired_state": {
"powered": "'"$2"'",
"max_set_point": "'"$3"'"
}
}')
result=$(echo $updateACtemp | jq '.errors != null')
if [ $result = "true" ]; then
name=$(echo $updateACtemp | jq '.data | .name')
tput setaf 2; echo $name Successful && tput sgr0
else
failwithoutexit $(echo $updateACtemp | jq '.errors')
fi
}
function updateACfan(){
updateACfan=$(curl -sX PUT "$APIURL"/air_conditioners/"$1" \
-H "Authorization: Bearer $access_token" \
-H "Content-Type: application/json" \
--data-binary '{
"desired_state": {
"powered": "'"$2"'",
"fan_speed": "'"$3"'"
}
}')
result=$(echo $updateACfan | jq '.errors != null')
if [ $result = "true" ]; then
name=$(echo $updateACfan | jq '.data | .name')
tput setaf 2; echo $name Successful && tput sgr0
else
failwithoutexit $(echo $updateACfan | jq '.errors')
fi
}
function updateACs(){
echo Set Air Conditioners...
ACTempC=$(echo "scale=2;(5/9)*($3-32)"|bc)
for ACid in $ACIDs
do
updateAC $ACid true $1 $2 $ACTempC
done
}
function ACOn(){
echo Turn all Air Conditioners On...
for ACid in $ACIDs
do
updateAC $ACid true
done
}
function ACOff(){
echo Turn all Air Conditioners Off...
for ACid in $ACIDs
do
updateAC $ACid false
done
}
function ACMode(){
echo Set Air Conditioners Mode...
for ACid in $ACIDs
do
updateACmode $ACid true $1
done
}
function ACTemp(){
echo Set Air Conditioners Temp...
ACTempC=$(echo "scale=2;(5/9)*($1-32)"|bc)
for ACid in $ACIDs
do
updateACtemp $ACid true $ACTempC
done
}
function ACFan(){
echo Set Air Conditioners Fan...
for ACid in $ACIDs
do
updateACfan $ACid true $1
done
}
function GetCurrentAverageTemp(){
totaltemp=0
temps=$(echo $retrieveAirConditioners | jq '.data | .[] | .last_reading | .temperature')
numtemps=$(echo "$temps" | wc -l)
starttemps=1
for (( COUNT=$starttemps; COUNT<=$numtemps; COUNT++ ))
do
currenttemp=$(echo "$temps" | nl | grep -w $COUNT | cut -f2)
totaltemp=$(echo "scale=10; $currenttemp + $totaltemp" | bc)
done
averageTemp=$(echo "scale=10; $totaltemp / $numtemps" | bc)
tempF=$(echo "scale=1; 9*$averageTemp/5+32" | bc)
}
# Check required commands
check_command "jq"
check_command "curl"
# Get Token
getToken
# List all your devices
# retrieveAllDevices
# retrieveAirConditioners
# Do Something - Turn on a lightbulb
# updateLightbulb light_bulbs {your_lightbulb_id_000000} true 1.00
# retrieveLightBulbIDs
# echo $bulbIDs
# fadeLightsOff
# Loop the Menu
while :
do
# Call the menu
deviceMenu
done