-
Notifications
You must be signed in to change notification settings - Fork 89.6k
/
zwave-metering-dimmer.groovy
385 lines (333 loc) · 13.4 KB
/
zwave-metering-dimmer.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
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
384
385
/**
* 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.
*
* Z-Wave Metering Dimmer
*
* Copyright 2014 SmartThings
*
*/
metadata {
definition (name: "Z-Wave Metering Dimmer", namespace: "smartthings", author: "SmartThings", ocfDeviceType: "oic.d.switch", runLocally: true, minHubCoreVersion: '000.017.0012', executeCommandsLocally: true, genericHandler: "Z-Wave") {
capability "Switch"
capability "Polling"
capability "Power Meter"
capability "Energy Meter"
capability "Refresh"
capability "Switch Level"
capability "Sensor"
capability "Actuator"
capability "Health Check"
capability "Light"
capability "Configuration"
command "reset"
fingerprint inClusters: "0x26,0x32", deviceJoinName: "Dimmer Switch"
fingerprint mfr:"0086", prod:"0003", model:"001B", deviceJoinName: "Aeotec Dimmer Switch" //Aeotec Micro Smart Dimmer 2E
fingerprint mfr:"0086", prod:"0103", model:"0063", deviceJoinName: "Aeotec Dimmer Switch" //US //Aeotec Smart Dimmer 6
fingerprint mfr:"0086", prod:"0003", model:"0063", deviceJoinName: "Aeotec Dimmer Switch" //EU //Aeotec Smart Dimmer 6
fingerprint mfr:"0086", prod:"0103", model:"006F", deviceJoinName: "Aeotec Dimmer Switch", mnmn: "SmartThings", vid: "SmartThings-smartthings-Aeotec_Nano_Dimmer" //Aeotec Nano Dimmer
fingerprint mfr:"0086", prod:"0003", model:"006F", deviceJoinName: "Aeotec Dimmer Switch", mnmn: "SmartThings", vid: "SmartThings-smartthings-Aeotec_Nano_Dimmer" //Aeotec Nano Dimmer
fingerprint mfr:"0086", prod:"0203", model:"006F", deviceJoinName: "Aeotec Dimmer Switch", mnmn: "SmartThings", vid: "SmartThings-smartthings-Aeotec_Nano_Dimmer" //Aeotec Nano Dimmer, AU
fingerprint mfr:"014F", prod:"5044", model:"3533", deviceJoinName: "GoControl Dimmer Switch" //GoControl Plug-in Dimmer
}
simulator {
status "on": "command: 2603, payload: FF"
status "off": "command: 2603, payload: 00"
status "09%": "command: 2603, payload: 09"
status "10%": "command: 2603, payload: 0A"
status "33%": "command: 2603, payload: 21"
status "66%": "command: 2603, payload: 42"
status "99%": "command: 2603, payload: 63"
for (int i = 0; i <= 10000; i += 1000) {
status "power ${i} W": new physicalgraph.zwave.Zwave().meterV1.meterReport(
scaledMeterValue: i, precision: 3, meterType: 4, scale: 2, size: 4).incomingMessage()
}
for (int i = 0; i <= 100; i += 10) {
status "energy ${i} kWh": new physicalgraph.zwave.Zwave().meterV1.meterReport(
scaledMeterValue: i, precision: 3, meterType: 0, scale: 0, size: 4).incomingMessage()
}
["FF", "00", "09", "0A", "21", "42", "63"].each { val ->
reply "2001$val,delay 100,2602": "command: 2603, payload: $val"
}
}
tiles(scale: 2) {
multiAttributeTile(name:"switch", type: "lighting", width: 6, height: 4, canChangeIcon: true){
tileAttribute ("device.switch", key: "PRIMARY_CONTROL") {
attributeState "on", label:'${name}', action:"switch.off", icon:"st.switches.switch.on", backgroundColor:"#00a0dc", nextState:"turningOff"
attributeState "off", label:'${name}', action:"switch.on", icon:"st.switches.switch.off", backgroundColor:"#ffffff", nextState:"turningOn"
attributeState "turningOn", label:'${name}', action:"switch.off", icon:"st.switches.switch.on", backgroundColor:"#00a0dc", nextState:"turningOff"
attributeState "turningOff", label:'${name}', action:"switch.on", icon:"st.switches.switch.off", backgroundColor:"#ffffff", nextState:"turningOn"
}
tileAttribute ("device.level", key: "SLIDER_CONTROL") {
attributeState "level", action:"switch level.setLevel"
}
}
valueTile("power", "device.power", width: 2, height: 2) {
state "default", label:'${currentValue} W'
}
valueTile("energy", "device.energy", width: 2, height: 2) {
state "default", label:'${currentValue} kWh'
}
standardTile("reset", "device.energy", inactiveLabel: false, decoration: "flat", width: 2, height: 2) {
state "default", label:'reset kWh', action:"reset"
}
standardTile("refresh", "device.switch", inactiveLabel: false, decoration: "flat", width: 2, height: 2) {
state "default", label:"", action:"refresh.refresh", icon:"st.secondary.refresh"
}
}
main(["switch","power","energy"])
details(["switch", "power", "energy", "refresh", "reset"])
preferences {
section {
input(
title: "Settings Available For Aeotec Nano Dimmer Only",
type: "paragraph",
element: "paragraph"
)
input(
title: "Set the MIN brightness level (Aeotec Nano Dimmer Only):",
description: "This may need to be adjusted for bulbs that are not dimming properly.",
name: "minDimmingLevel",
type: "number",
range: "1..99",
defaultValue: 1
)
}
}
}
def getCommandClassVersions() {
[
0x20: 1, // Basic
0x26: 3, // SwitchMultilevel
0x56: 1, // Crc16Encap
0x70: 1, // Configuration
0x32: 3, // Meter
]
}
def installed() {
// Device-Watch simply pings if no device events received for 32min(checkInterval)
sendEvent(name: "checkInterval", value: 2 * 15 * 60 + 2 * 60, displayed: false, data: [protocol: "zwave", hubHardwareId: device.hub.hardwareID])
}
def updated() {
// Device-Watch simply pings if no device events received for 32min(checkInterval)
sendEvent(name: "checkInterval", value: 2 * 15 * 60 + 2 * 60, displayed: false, data: [protocol: "zwave", hubHardwareId: device.hub.hardwareID])
def results = []
results << refresh()
if (isAeotecNanoDimmer()) {
results << getAeotecNanoDimmerConfigurationCommands()
}
response(results)
}
// parse events into attributes
def parse(String description) {
def result = null
if (description != "updated") {
def cmd = zwave.parse(description, commandClassVersions)
if (cmd) {
result = zwaveEvent(cmd)
log.debug("'$description' parsed to $result")
} else {
log.debug("Couldn't zwave.parse '$description'")
}
}
result
}
def zwaveEvent(physicalgraph.zwave.commands.basicv1.BasicReport cmd) {
dimmerEvents(cmd)
}
def zwaveEvent(physicalgraph.zwave.commands.basicv1.BasicSet cmd) {
dimmerEvents(cmd)
}
def zwaveEvent(physicalgraph.zwave.commands.switchmultilevelv3.SwitchMultilevelReport cmd) {
dimmerEvents(cmd)
}
def zwaveEvent(physicalgraph.zwave.Command cmd) {
// Handles all Z-Wave commands we aren't interested in
[:]
}
def dimmerEvents(physicalgraph.zwave.Command cmd) {
def result = []
def value = (cmd.value ? "on" : "off")
def switchEvent = createEvent(name: "switch", value: value, descriptionText: "$device.displayName was turned $value")
result << switchEvent
if (cmd.value) {
result << createEvent(name: "level", value: cmd.value == 99 ? 100 : cmd.value , unit: "%")
}
if (switchEvent.isStateChange) {
result << response(["delay 3000", meterGet(scale: 2).format()])
}
return result
}
def handleMeterReport(cmd) {
if (cmd.meterType == 1) {
if (cmd.scale == 0) {
createEvent(name: "energy", value: cmd.scaledMeterValue, unit: "kWh")
} else if (cmd.scale == 1) {
createEvent(name: "energy", value: cmd.scaledMeterValue, unit: "kVAh")
} else if (cmd.scale == 2) {
createEvent(name: "power", value: Math.round(cmd.scaledMeterValue), unit: "W")
}
}
}
def zwaveEvent(physicalgraph.zwave.commands.meterv3.MeterReport cmd) {
log.debug "v3 Meter report: "+cmd
handleMeterReport(cmd)
}
def on() {
encapSequence([
zwave.basicV1.basicSet(value: 0xFF),
zwave.switchMultilevelV1.switchMultilevelGet(),
], 5000)
}
def off() {
encapSequence([
zwave.basicV1.basicSet(value: 0x00),
zwave.switchMultilevelV1.switchMultilevelGet(),
], 5000)
}
def poll() {
encapSequence([
meterGet(scale: 0),
meterGet(scale: 2),
], 1000)
}
/**
* PING is used by Device-Watch in attempt to reach the Device
* */
def ping() {
log.debug "ping() called"
refresh()
}
def refresh() {
log.debug "refresh()"
encapSequence([
zwave.switchMultilevelV1.switchMultilevelGet(),
meterGet(scale: 0),
meterGet(scale: 2),
], 1000)
}
def setLevel(level, rate = null) {
if(level > 99) level = 99
encapSequence([
zwave.basicV1.basicSet(value: level),
zwave.switchMultilevelV1.switchMultilevelGet()
], 5000)
}
def configure() {
log.debug "configure()"
def result = []
if (isAeotecNanoDimmer()) {
state.configured = false
result << response(getAeotecNanoDimmerConfigurationCommands())
}
log.debug "Configure zwaveInfo: "+zwaveInfo
if (zwaveInfo.mfr == "0086") { // Aeon Labs meter
result << response(encap(zwave.configurationV1.configurationSet(parameterNumber: 80, size: 1, scaledConfigurationValue: 2))) // basic report cc
result << response(encap(zwave.configurationV1.configurationSet(parameterNumber: 101, size: 4, scaledConfigurationValue: 12))) // report power in watts
result << response(encap(zwave.configurationV1.configurationSet(parameterNumber: 111, size: 4, scaledConfigurationValue: 300))) // every 5 min
if (zwaveInfo.model == "006F") { // Aeotec Nano Dimmer
result << response(encap(zwave.configurationV1.configurationSet(parameterNumber: 90, size: 1, scaledConfigurationValue: 1))) // enables parameter 91
result << response(encap(zwave.configurationV1.configurationSet(parameterNumber: 91, size: 2, scaledConfigurationValue: 1))) // wattage report after 1 watt change
result << response(encap(zwave.configurationV1.configurationSet(parameterNumber: 102, size: 1, scaledConfigurationValue: 4))) // meter report of wattage for group 2
result << response(encap(zwave.configurationV1.configurationSet(parameterNumber: 103, size: 1, scaledConfigurationValue: 8))) // meter report of energy for group 3
result << response(encap(zwave.configurationV1.configurationSet(parameterNumber: 112, size: 4, scaledConfigurationValue: 300))) // automatic report for group 2 every 5 min
result << response(encap(zwave.configurationV1.configurationSet(parameterNumber: 113, size: 4, scaledConfigurationValue: 300))) // automatic report for group 3 every 5 min
}
}
result << response(encap(meterGet(scale: 0)))
result << response(encap(meterGet(scale: 2)))
}
def reset() {
encapSequence([
meterReset(),
meterGet(scale: 0)
])
}
def meterGet(scale) {
zwave.meterV2.meterGet(scale)
}
def meterReset() {
zwave.meterV2.meterReset()
}
def normalizeLevel(level) {
// Normalize level between 1 and 100.
level == 99 ? 100 : level
}
def getAeotecNanoDimmerConfigurationCommands() {
def result = []
Integer minDimmingLevel = (settings.minDimmingLevel as Integer) ?: 1 // default value (parameter 131) for Aeotec Nano Dimmer
if (!state.minDimmingLevel) {
state.minDimmingLevel = 1 // default value (parameter 131) for Aeotec Nano Dimmer
}
if (!state.configured || (minDimmingLevel != state.minDimmingLevel)) {
state.configured = false // this flag needs to be set to false when settings are changed (and the device was initially configured before)
result << encap(zwave.configurationV1.configurationSet(parameterNumber: 131, size: 1, scaledConfigurationValue: minDimmingLevel))
result << encap(zwave.configurationV1.configurationGet(parameterNumber: 131))
}
return result
}
def zwaveEvent(physicalgraph.zwave.commands.configurationv1.ConfigurationReport cmd) {
if (isAeotecNanoDimmer()) {
if (cmd.parameterNumber == 131) {
state.minDimmingLevel = cmd.scaledConfigurationValue
state.configured = true
}
log.debug "${device.displayName} parameter '${cmd.parameterNumber}' with a byte size of '${cmd.size}' is set to '${cmd.configurationValue}'"
}
return [:]
}
/*
* Security encapsulation support:
*/
def zwaveEvent(physicalgraph.zwave.commands.securityv1.SecurityMessageEncapsulation cmd) {
def encapsulatedCommand = cmd.encapsulatedCommand(commandClassVersions)
if (encapsulatedCommand) {
log.debug "Parsed SecurityMessageEncapsulation into: ${encapsulatedCommand}"
zwaveEvent(encapsulatedCommand)
} else {
log.warn "Unable to extract Secure command from $cmd"
}
}
def zwaveEvent(physicalgraph.zwave.commands.crc16encapv1.Crc16Encap cmd) {
def version = commandClassVersions[cmd.commandClass as Integer]
def ccObj = version ? zwave.commandClass(cmd.commandClass, version) : zwave.commandClass(cmd.commandClass)
def encapsulatedCommand = ccObj?.command(cmd.command)?.parse(cmd.data)
if (encapsulatedCommand) {
log.debug "Parsed Crc16Encap into: ${encapsulatedCommand}"
zwaveEvent(encapsulatedCommand)
} else {
log.warn "Unable to extract CRC16 command from $cmd"
}
}
private secEncap(physicalgraph.zwave.Command cmd) {
log.debug "encapsulating command using Secure Encapsulation, command: $cmd"
zwave.securityV1.securityMessageEncapsulation().encapsulate(cmd).format()
}
private crcEncap(physicalgraph.zwave.Command cmd) {
log.debug "encapsulating command using CRC16 Encapsulation, command: $cmd"
zwave.crc16EncapV1.crc16Encap().encapsulate(cmd).format()
}
private encap(physicalgraph.zwave.Command cmd) {
if (zwaveInfo?.zw?.contains("s")) {
secEncap(cmd)
} else if (zwaveInfo?.cc?.contains("56")){
crcEncap(cmd)
} else {
log.debug "no encapsulation supported for command: $cmd"
cmd.format()
}
}
private encapSequence(cmds, Integer delay=250) {
delayBetween(cmds.collect{ encap(it) }, delay)
}
private isAeotecNanoDimmer() {
zwaveInfo?.mfr?.equals("0086") && zwaveInfo?.model?.equals("006F")
}