forked from SmartThingsCommunity/SmartThingsPublic
/
lightify-dimming-switch.groovy
347 lines (306 loc) · 11.8 KB
/
lightify-dimming-switch.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
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
/**
* OSRAM Lightify Dimming Switch
*
* Copyright 2016 Michael Hudson
*
* 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.
*
* Thanks to @Sticks18 for the Hue Dimmer remote code used as a base for this!
*
*/
metadata {
definition (name: "Lightify Dimming Switch", namespace: "aoutler", author: "Adam Outler") {
capability "Actuator"
capability "Battery"
capability "Button"
capability "Configuration"
capability "Refresh"
capability "Sensor"
capability "polling"
attribute "zMessage", "String"
//fingerprint profileId: "0104", deviceId: "0001", inClusters: "0000, 0001, 0003, 0020, 0402, 0B05", outClusters: "0003, 0006, 0008, 0019", /*manufacturer: "OSRAM", model: "Lightify 2.4GHZZB/SWITCH/LFY", */deviceJoinName: "OSRAM Lightify Dimming Switch"
}
// simulator metadata
simulator {
// status messages
}
preferences {
section("Device1") {
input("device1", "string", title:"Device Network ID 1", description: "The Device Network Id", defaultValue: "" ,required: false, displayDuringSetup: false)
input("end1", "string", title:"Device Endpoint ID 1", description: "endpointId from Data Section of device", defaultValue: "" ,required: false, displayDuringSetup: false)
}
section("Device2") {
input("device2", "string", title:"Device Network ID 2", description: "The Device Network Id", defaultValue: "" ,required: false, displayDuringSetup: false)
input("end2", "string", title:"Device Endpoint ID 2", description: "endpointId from Data Section of device", defaultValue: "" ,required: false, displayDuringSetup: false)
}
section("Device3") {
input("device3", "string", title:"Device Network ID 3", description: "The Device Network Id", defaultValue: "" ,required: false, displayDuringSetup: false)
input("end3", "string", title:"Device Endpoint ID 3", description: "endpointId from Data Section of device", defaultValue: "" ,required: false, displayDuringSetup: false)
}
section("Device4") {
input("device4", "string", title:"Device Network ID 4", description: "The Device Network Id", defaultValue: "" ,required: false, displayDuringSetup: false)
input("end4", "string", title:"Device Endpoint ID 4", description: "endpointId from Data Section of device", defaultValue: "" ,required: false, displayDuringSetup: false)
}
section("Device5") {
input("device5", "string", title:"Device Network ID 5", description: "The Device Network Id", defaultValue: "" ,required: false, displayDuringSetup: false)
input("end5", "string", title:"Device Endpoint ID 5", description:"endpointId from Data Section of device", defaultValue: "" ,required: false, displayDuringSetup: false)
}
}
// UI tile definitions
tiles(scale: 2) {
standardTile("button", "device.button", width: 6, height: 4) {
state "default", label: "", icon: "st.unknown.zwave.remote-controller", backgroundColor: "#ffffff"
}
valueTile("battery", "device.battery", decoration: "flat", inactiveLabel: false, width: 2, height: 2) {
state "battery", label:'${currentValue} battery'
}
standardTile("refresh", "device.button", decoration: "flat", width: 2, height: 2) {
state "default", label:"", action:"refresh.refresh", icon:"st.secondary.refresh"
}
main "button"
details(["button", "battery", "refresh"])
}
}
def initialize() {
state.battery=0
state.brightness=0
state.value="pushed"
state.dimming=false
}
def updated() {
initialize()
}
def poll(){
log.debug "Executing 'poll'"
sendEvent(name: 'battery', value: state.battery)
refresh()
}
// Parse incoming device messages to generate events
def parse(String description) {
Map map = [:]
def result = []
log.debug "parse description: $description"
if (description?.startsWith('catchall:')) {
// call parseCatchAllMessage to parse the catchall message received
map = parseCatchAllMessage(description)
if (device1 == null) {} else {
if (map.value == "pushed") {
List cmds
if (map.data.buttonNumber == 1) {
cmds = onResponse()
} else {
cmds = offResponse()
}
log.trace "Sending current state to device ${cmds}"
result = cmds?.collect { new physicalgraph.device.HubAction(it) }
return result
}
}
if (device1 == null) {} else {
if (map.value == "held") {
List cmds
if (map.data.buttonNumber == 1) {
cmds = dim1Response()
} else {
cmds = dim2Response()
}
log.trace "Sending current state to device ${cmds}"
result = cmds?.collect { new physicalgraph.device.HubAction(it) }
cmds?.collect { new physicalgraph.device.HubAction(it)}
return result
}
}
} else if (description?.startsWith('read')) {
// call parseReadMessage to parse the read message received
map = parseReadMessage(description)
} else {
log.debug "Unknown message received: $description"
}
return map ? createEvent(map) : null
}
def configure() {
log.debug "Confuguring Reporting and Bindings."
initialize()
def configCmds = [
// Bind the outgoing on/off cluster from remote to hub, so the hub receives messages when On/Off buttons pushed
"zdo bind 0x${device.deviceNetworkId} 0x01 0x01 0x0006 {${device.zigbeeId}} {}",
// Bind the outgoing level cluster from remote to hub, so the hub receives messages when Dim Up/Down buttons pushed
"zdo bind 0x${device.deviceNetworkId} 0x01 0x01 0x0008 {${device.zigbeeId}} {}",
// Bind the incoming battery info cluster from remote to hub, so the hub receives battery updates
"zdo bind 0x${device.deviceNetworkId} 0x01 0x01 0x0001 {${device.zigbeeId}} {}",
]
return configCmds
}
def refresh() {
def refreshCmds = [
zigbee.readAttribute(0x0001, 0x0020)
]
sendEvent name: "battery", value: state.battery
log.debug(state)
//when refresh button is pushed, read updated status
return refreshCmds
}
private Map parseReadMessage(String description) {
// Create a map from the message description to make parsing more intuitive
def msg = zigbee.parseDescriptionAsMap(description)
//def msg = zigbee.parse(description)
if (msg.clusterInt==1 && msg.attrInt==32) {
// call getBatteryResult method to parse battery message into event map
def result = getBatteryResult(Integer.parseInt(msg.value, 16))
} else {
log.debug "Unknown read message received, parsed message: $msg"
}
// return map used to create event
return result
}
private Map parseCatchAllMessage(String description) {
// Create a map from the raw zigbee message to make parsing more intuitive
def msg = zigbee.parse(description)
def value="pushed"
def button=0
String data= "[buttonnumber:"+msg.command +"]"
Map result = [:]
log.debug("battery state: " + state.battery)
switch(msg.clusterId) {
case 1: // battery message
// call getBatteryResult method to parse battery message into event map
log.debug 'BATTERY MESSAGE'
result = getBatteryResult(Integer.parseInt(msg.value, 16))
state.battery=result.value
return result
break
case 6: //button pressed
button = (msg.command == 1 ? 1 : 2)
state.value="pushed"
break
case 8: //button held
button=(msg.command == 1 ? 1 : 2)
switch(msg.command) {
case 1: // brightness decrease command
state.value="held"
button=2
state.dimming=true
break
case 3: // brightness change stop command
state.value="released"
state.dimming=false
sendEvent name: "value", value: "released"
break
case 5: // brightness increase command
state.value="held"
button=1
state.dimming=true
break
}
}
log.debug 'message '+value+ " button " + button + ". brightness " + state.brightness
result = [
name: 'button',
value: state.value,
data: [buttonNumber: button],
descriptionText: "$device.displayName button $button was $value",
brightness: state.brightness,
isStateChange: true
]
log.info "${result?.descriptionText}"
refresh()
return result
}
//obtained from other examples, converts battery message into event map
private Map getBatteryResult(rawValue) {
def linkText = getLinkText(device)
def result = [
name: 'battery',
value: state.battery
]
def volts = rawValue / 10
def descriptionText
if (rawValue == 0) {
state.battery="unknown"
} else {
if (volts > 3.5) {
result.descriptionText = "${linkText} battery has too much power (${volts} volts)."
state.battery="overvoltage"
} else if (volts > 0){
def minVolts = 2.1
def maxVolts = 3.0
def pct = (volts - minVolts) / (maxVolts - minVolts)
result.value = Math.min(100, (int) pct * 100)
state.battery="${result.value}"
result.descriptionText = "${linkText} battery was ${result.value}%"
}
}
log.debug "Parse returned ${result?.descriptionText}"
return result
}
private String createOnResponse(device, end){
if ( device == null) return null
return "st cmd 0x${device} 0x${end} 8 4 {FE 0000}"
}
private String createOffResponse(device, end){
if ( device == null) return null
return "st cmd 0x${device} 0x${end} 6 0 {}"
}
private ArrayList createListOfNotNullDevices(allDevices){
ArrayList list = []
for (item in allDevices ){
if (item !=null ) {
list.add(item)
}
}
return list
}
def onResponse() {
state.brightness=100
def on1 =createOnResponse(device1,end1)
def on2 =createOnResponse(device2,end2)
def on3 =createOnResponse(device3,end3)
def on4 =createOnResponse(device4,end4)
def on5 =createOnResponse(device5,end5)
return createListOfNotNullDevices([on1,on2,on3,on4,on5]).toArray()
}
def offResponse() {
state.brightness=0
def off1 = createOffResponse(device1,end1)
def off2 = createOffResponse(device2,end2)
def off3 = createOffResponse(device3,end3)
def off4 = createOffResponse(device4,end4)
def off5 = createOffResponse(device5,end5)
return createListOfNotNullDevices([off1,off2,off3,off4,off5]).toArray()
}
private String createDimResponse(device, end, level){
if ( device == null) return null
return "st cmd 0x${device} 0x${end} 8 4 {${level} 0000}"
}
def dimResponse(level) {
log.debug "Creating dim response to level "+level
def d1 = createDimResponse(device1, end1, level);
def d2 = createDimResponse(device2, end2, level);
def d3 = createDimResponse(device3, end3, level);
def d4 = createDimResponse(device4, end4, level);
def d5 = createDimResponse(device5, end5, level);
return createListOfNotNullDevices([d1,d2,d3,d4,d5]).toArray()
}
private controlMinMaxDim(){
if (state.brightness>95) state.brightness=95
if (state.brightness<10) state.brightness=10
}
def dim1Response() {
if (state.brightness==0) state.brightness=5 //convience to turn it on immediately
state.brightness=state.brightness+10
controlMinMaxDim()
return dimResponse(state.brightness)
}
def dim2Response() {
log.debug("performing dim2 response")
state.brightness=state.brightness-10
controlMinMaxDim()
return dimResponse(state.brightness)
}