forked from SteveTheGeekHA/NanoleafAuroraHandler
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathNanoleafAuroraSmarterAPI.groovy
283 lines (225 loc) · 9.28 KB
/
NanoleafAuroraSmarterAPI.groovy
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
/**
* Copyright 2015 SmartThings
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
* for the specific language governing permissions and limitations under the License.
*
*/
import groovy.json.JsonSlurper
metadata {
definition (name: "Nanoleaf Aurora Smarter API", namespace: "SteveTheGeekAH", author: "Steve The Geek") {
capability "Light"
capability "Switch Level"
capability "Switch"
capability "Color Control"
capability "Polling"
capability "Refresh"
command "previousScene"
command "nextScene"
command "changeScene" //for CoRE access to scenes
command "setScene1"
command "setScene2"
command "setScene3"
attribute "scene", "String"
attribute "scenesList", "String"
}
simulator {
}
tiles {
multiAttributeTile(name:"switch", type: "lighting", width: 6, height: 4, decoration: "flat", canChangeIcon: true){
tileAttribute ("device.switch", key: "PRIMARY_CONTROL") {
attributeState "on", label: 'on', action: "off", icon: "http://stevethegeek.net/smartthings/aurora/aurora-on.png", backgroundColor: "#00a0dc"
attributeState "off", label: 'off', action: "on", icon: "http://stevethegeek.net/smartthings/aurora/aurora-off.png", backgroundColor: "#ffffff"
}
tileAttribute ("level", key: "SLIDER_CONTROL", range:"(1..100)") {
attributeState "level", action:"setLevel"
}
tileAttribute ("color", key: "COLOR_CONTROL") {
attributeState "color", action:"setColor"
}
}
standardTile("scene1", "scene1", width: 2, height: 1, decoration: "flat") {
state "val", label: '${currentValue}', backgroundColor: "#ffffff", action: "setScene1"
}
standardTile("scene2", "scene2", width: 2, height: 1, decoration: "flat") {
state "val", label: '${currentValue}', backgroundColor: "#ffffff", action: "setScene2"
}
standardTile("scene3", "scene", width: 2, height: 1, decoration: "flat") {
state "val", label: '${currentValue}', backgroundColor: "#ffffff", action: "setScene3"
}
standardTile("previousScene", "scene", width: 1, height: 1, decoration: "flat") {
state "default", label: "", backgroundColor: "#ffffff", action: "previousScene", icon: "http://stevethegeek.net/smartthings/aurora/aurora-left.png"
}
valueTile("currentScene", "scene", width: 4, height: 1, decoration: "flat") {
state "val", label: '${currentValue}', backgroundColor: "#ffffff"
}
standardTile("nextScene", "scene", width: 1, height: 1, decoration: "flat") {
state "default", label: "", backgroundColor: "#ffffff", action: "nextScene", icon: "http://stevethegeek.net/smartthings/aurora/aurora-right.png"
}
standardTile("refresh", "device.switch", decoration: "flat", width: 1, height: 1) {
state "default", action:"refresh", icon:"st.secondary.refresh"
}
main "switch"
details(["switch","scene1","scene2","scene3","previousScene","currentScene","nextScene","refresh"])
}
preferences {
input name: "apiKey", type: "text", title: "Aurora API Key", description: "Enter The Key Returned By The Api Authentication Method", required: true
input name: "scene1", type: "text", title: "Favorite Scene 1", description: "Enter a Scene name", required: false
input name: "scene2", type: "text", title: "Favorite Scene 2", description: "Enter a Scene name", required: false
input name: "scene3", type: "text", title: "Favorite Scene 3", description: "Enter a Scene name", required: false
}
}
def parse(String description) {
def message = parseLanMessage(description)
if(message.json) {
def auroraOn = message.json.state.on.value
if(auroraOn && device.currentValue("switch") == "off") {
log.debug("Aurora has been switched on outside of Smartthings")
sendEvent(name: "switch", value: "on", isStateChange: true)
}
if(!auroraOn && device.currentValue("switch") == "on") {
log.debug("Aurora has been switched off outside of Smartthings")
sendEvent(name: "switch", value: "off", isStateChange: true)
}
def currentScene = message.json.effects.select
if(currentScene != device.currentValue("scene")) {
log.debug("Scene was changed outside of Smartthings")
sendEvent(name: "scene", value: currentScene, isStateChange: true)
}
def currentBrightness = message.json.state.brightness.value
def deviceBrightness = "${device.currentValue("level")}"
if(currentBrightness != device.currentValue("level")) {
log.debug("Brightness was changed outside of Smartthings")
sendEvent(name: "level", value: currentBrightness, isStateChange: true)
}
def effectsList = message.json.effects.effectsList
if(effectsList.toString() != device.currentValue("scenesList").toString()) {
log.debug("List of effects was changed in the Aurora App")
sendEvent(name: "scenesList", value: effectsList, isStateChange: true)
}
} else {
log.debug("Response from PUT, do nothing")
}
}
def poll() {
log.debug("polled")
refresh()
}
def refresh() {
return createGetRequest("");
}
def off() {
sendEvent(name: "switch", value: "off", isStateChange: true)
return createPutRequest("state", "{\"on\" : false}")
}
def on() {
sendEvent(name: "switch", value: "on", isStateChange: true)
return createPutRequest("state", "{\"on\" : true}")
}
def previousScene() {
def sceneListString = device.currentValue("scenesList").replaceAll(", ", ",")
def sceneList = sceneListString.substring(1, sceneListString.length()-1).tokenize(',')
def currentSelectedScene = device.currentValue("scene");
def index = sceneList.indexOf(currentSelectedScene)
log.debug(index)
if(index == -1) {
index = 1;
}
index--
if(index == -1) {
index = sceneList.size -1
}
changeScene(sceneList[index])
}
def nextScene() {
def sceneListString = device.currentValue("scenesList").replaceAll(", ", ",")
def sceneList = sceneListString.substring(1, sceneListString.length()-1).tokenize(',')
def currentSelectedScene = device.currentValue("scene");
def index = sceneList.indexOf(currentSelectedScene)
index++
if(index == sceneList.size) {
index = 0
}
changeScene(sceneList[index])
}
def changeScene(String scene) {
sendEvent(name: "scene", value: scene, isStateChange: true)
return createPutRequest("effects", "{\"select\" : \"${scene}\"}")
}
def setScene1() {
sendEvent(name: "scene1", value: "${scene1}")
changeScene("${scene1}")
}
def setScene2() {
sendEvent(name: "scene2", value: "${scene2}")
changeScene("${scene2}")
}
def setScene3() {
sendEvent(name: "scene3", value: "${scene3}")
changeScene("${scene3}")
}
def setLevel(Integer value) {
sendEvent(name: "level", value: value, isStateChange: true)
return createPutRequest("state", "{\"brightness\" : ${value}}")
}
def setColor(value) {
sendEvent(name: "scene", value: "--", isStateChange: true)
sendEvent(name: "color", value: value.hex, isStateChange: true)
return createPutRequest("state", "{\"hue\" : ${(value.hue*360/100).toInteger()}, \"sat\" : ${value.saturation.toInteger()}}")
}
// gets the address of the hub
private getCallBackAddress() {
return device.hub.getDataValue("localIP") + ":" + device.hub.getDataValue("localSrvPortTCP")
}
private createPutRequest(String url, String body) {
log.debug("/api/v1/${apiKey}/${url}")
log.debug("body : ${body}")
def result = new physicalgraph.device.HubAction(
method: "PUT",
path: "/api/v1/${apiKey}/${url}",
body: body,
headers: [
HOST: getHostAddress()
]
)
return result;
}
private createGetRequest(String url) {
//log.debug("/api/v1/${apiKey}/${url}")
def result = new physicalgraph.device.HubAction(
method: "GET",
path: "/api/v1/${apiKey}/${url}",
headers: [
HOST: getHostAddress()
]
)
return result;
}
// gets the address of the device
private getHostAddress() {
def ip = getDataValue("ip")
def port = getDataValue("port")
if (!ip || !port) {
def parts = device.deviceNetworkId.split(":")
if (parts.length == 2) {
ip = parts[0]
port = parts[1]
} else {
log.warn "Can't figure out ip and port for device: ${device.id}"
}
}
//log.debug "Using IP: $ip and port: $port for device: ${device.id}"
return convertHexToIP(ip) + ":" + convertHexToInt(port)
}
private Integer convertHexToInt(hex) {
return Integer.parseInt(hex,16)
}
private String convertHexToIP(hex) {
return [convertHexToInt(hex[0..1]),convertHexToInt(hex[2..3]),convertHexToInt(hex[4..5]),convertHexToInt(hex[6..7])].join(".")
}