-
Notifications
You must be signed in to change notification settings - Fork 1
/
driver_HaasTIThing
376 lines (346 loc) · 13.6 KB
/
driver_HaasTIThing
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
metadata {
definition (name: "HaasTI ZigBee Switch", namespace: "drandyhaas", author: "Andy Haas", ocfDeviceType: "oic.d.switch", runLocally: true, minHubCoreVersion: '000.019.00012', executeCommandsLocally: true, genericHandler: "Zigbee") {
capability "Actuator"
capability "Configuration"
capability "Refresh"
capability "Switch"
capability "Sensor"
capability "Health Check"
capability "Battery"
capability "Temperature Measurement"
command "sendCommand", ["string"] //allows arbitrary command to be sent
command "gettext"
command "butt1"
command "butt2"
command "butt3"
command "allon"
command "on1"
command "on2"
command "on3"
command "on4"
command "alloff"
command "off1"
command "off2"
command "off3"
command "off4"
command "getadc0"
command "getadc1"
command "getadc4"
command "getadc5"
command "polladc0"
command "polladc1"
command "polladc4"
command "polladc5"
command "nopolladc0"
command "nopolladc1"
command "nopolladc4"
command "nopolladc5"
//for battery powered MonaLisa
command "pwrwait", ["string"]
command "getpwrwait"
command "push"
command "sendtext1"
attribute "text","string"
attribute "ntctemp","number"
attribute "adc0","number"
attribute "adc1","number"
attribute "adc4","number"
attribute "adc5","number"
attribute "pwrwait","number"
attribute "vcc","number"
attribute "digin1","text"
attribute "digin2","text"
attribute "digin3","text"
attribute "digout1","text"
attribute "digout2","text"
attribute "digout3","text"
attribute "digout4","text"
fingerprint inClusters: "0000,0003,0004,0005,0006", outClusters: "0000", profileId: "0104", manufacturer: "TexasInstruments", model: "TI0001", deviceJoinName: "HaasTI Thing"
}
preferences {
input name: "tempOffset", type: "decimal", title: "Amount to add to temperature, in F, for calibration", defaultValue: 0.0, required: true
}
}
// Parse incoming device messages to generate events
def parse(String description) {
//log.debug "description is $description"
Map map = [:]
def event = zigbee.getEvent(description)
if (event) {
sendEvent(event)
}
else if (description?.startsWith("catchall:")) {
log.debug "catchall is $description"
}
else if (description?.startsWith("read attr -")) {
def descMap = zigbee.parseDescriptionAsMap(description)
log.debug "Desc Map: $descMap"
if (descMap.clusterInt == 0) {
//def readstring = descMap.value
//byte[] asciireadstring = readstring.decodeHex()
//String text = new String(asciireadstring)
//log.debug "readstring is $readstring, ascii $asciireadstring, text $text"
def text = descMap.value
log.debug "text is $text"
if (text.startsWith("ping.")) return
if (text.startsWith("0_")){
log.debug "adc 0 parse"
val = text.substring(2,text.indexOf("."))
sendEvent(name: "adc0", value: val)
// if you have a ntc3950 thermistor between adc0 and GND (or digout), and a 10k resistor between adc0 and vcc
sendEvent(name: "ntctemp", value: convertNTC(val.toFloat()))
}
if (text.startsWith("1_")){
log.debug "adc 1 parse"
val = text.substring(2,text.indexOf("."))
sendEvent(name: "adc1", value: val)
}
if (text.startsWith("4_")){
log.debug "adc 4 parse"
val = text.substring(2,text.indexOf("."))
sendEvent(name: "adc4", value: val)
}
if (text.startsWith("5_")){
log.debug "adc 5 parse"
val = text.substring(2,text.indexOf("."))
sendEvent(name: "adc5", value: val)
}
if (text.startsWith("pwrwait_")){
log.debug "pwrwait parse"
val = text.substring(8,text.indexOf("."))
sendEvent(name: "pwrwait", value: val)
}
if (text.startsWith("3_")){ // VCC
log.debug "vcc3 parse"
val = text.substring(2,text.indexOf("."))
val = val.toInteger()
float valf = val*2.6/6140.0;
valf=valf.round(2);
sendEvent(name: "vcc", value: valf)
float bat=100.0
if (valf<3.0) bat=(valf-2.0)*100.0
if (valf<2.0) bat=0.0
sendEvent(name: "battery", value: bat.round(2))
}
if (text.startsWith("t_")){ // temp
log.debug "temp parse"
val = text.substring(2,text.indexOf("."))
valf = val.toInteger()
valf = 25.0 + (valf-5920)/18.0;
valf = (valf*1.8)+32.0+tempOffset; // to F, and apply calibration
valf=valf.toFloat().round(1);
sendEvent(name: "temperature", value: valf)
log.debug "temp parse done $valf"
}
if (text.startsWith("Button1 ")){
log.debug "butt1 parse"
val = text.substring(8,text.indexOf("."))
sendEvent(name: "digin1", value: val)
}
if (text.startsWith("Button2 ")){
log.debug "butt2 parse"
val = text.substring(8,text.indexOf("."))
sendEvent(name: "digin2", value: val)
}
if (text.startsWith("Button3 ")){
log.debug "butt3 parse"
val = text.substring(8,text.indexOf("."))
sendEvent(name: "digin3", value: val)
}
if (text.startsWith("on...")){
log.debug "on parse"
sendEvent(name: "digout1", value: "on")
sendEvent(name: "digout2", value: "on")
sendEvent(name: "digout3", value: "on")
sendEvent(name: "digout4", value: "on")
}
if (text.startsWith("off...")){
log.debug "off parse"
sendEvent(name: "digout1", value: "off")
sendEvent(name: "digout2", value: "off")
sendEvent(name: "digout3", value: "off")
sendEvent(name: "digout4", value: "off")
}
if (text.startsWith("on1...")){
log.debug "on1 parse"
sendEvent(name: "digout1", value: "on")
}
if (text.startsWith("on2...")){
log.debug "on2 parse"
sendEvent(name: "digout2", value: "on")
}
if (text.startsWith("on3...")){
log.debug "on3 parse"
sendEvent(name: "digout3", value: "on")
}
if (text.startsWith("on4...")){
log.debug "on4 parse"
sendEvent(name: "digout4", value: "on")
}
if (text.startsWith("off1...")){
log.debug "off1 parse"
sendEvent(name: "digout1", value: "off")
}
if (text.startsWith("off2...")){
log.debug "off2 parse"
sendEvent(name: "digout2", value: "off")
}
if (text.startsWith("off3...")){
log.debug "off3 parse"
sendEvent(name: "digout3", value: "off")
}
if (text.startsWith("off4...")){
log.debug "off4 parse"
sendEvent(name: "digout4", value: "off")
}
return createEvent(name: "text", value: "$text")
}
else {
log.warn "Not an attribute we can decode"
}
}
else {
log.warn "DID NOT PARSE MESSAGE for description : $description"
log.debug zigbee.parseDescriptionAsMap(description)
}
}
def off() {
log.info "off"
sendEvent(name: "digout1", value: "off")
sendEvent(name: "digout2", value: "off")
sendEvent(name: "digout3", value: "off")
sendEvent(name: "digout4", value: "off")
zigbee.off()
}
def on() {
log.info "on"
sendEvent(name: "digout1", value: "on")
sendEvent(name: "digout2", value: "on")
sendEvent(name: "digout3", value: "on")
sendEvent(name: "digout4", value: "on")
zigbee.on()
}
/**
* PING is used by Device-Watch in attempt to reach the Device
* */
def ping() {
return refresh()
}
def refresh() {
log.info "refresh"
zigbee.onOffRefresh() + zigbee.onOffConfig()
}
def gettext(){ // read some attribute string from the device
log.info "gettext"
//zigbee.readAttribute(0x000, 0x0006) // gets the last thing the device tried to send to us
zigbee.readAttribute(0x000, 0x0010) // gets the last command the device heard us send
}
def sendtext1(){ // set the LocationDescription string on the device
log.debug "sendtext1"
//sendtodevice("ping") // to say hi
sendtodevice("arduino1") // to tell the arduino, connected on serial, to do something, like send back a message on serial (it should just make sure it's <16 bytes, and ends with a ".")
}
def sendCommand(String msg) {
log.info "sendCommand - ${msg}"
sendtodevice(msg)
}
def pwrwait(String msg) {
log.info "pwrwait_${msg}"
sendtodevice("pwrwait_"+msg)
}
def getpwrwait() {
log.info "getpwrwait"
sendtodevice("getpwrwait")
}
def allon(){sendtodevice("on")}
def on1(){sendtodevice("on1")}
def on2(){sendtodevice("on2")}
def on3(){sendtodevice("on3")}
def on4(){sendtodevice("on4")}
def alloff(){sendtodevice("off")}
def off1(){sendtodevice("off1")}
def off2(){sendtodevice("off2")}
def off3(){sendtodevice("off3")}
def off4(){sendtodevice("off4")}
def butt1(){sendtodevice("getbutt1")}
def butt2(){sendtodevice("getbutt2")}
def butt3(){sendtodevice("getbutt3")}
def getadc0(){sendtodevice("getadc0")}
def getadc1(){sendtodevice("getadc1")}
def getadc4(){sendtodevice("getadc4")}
def getadc5(){sendtodevice("getadc5")}
def polladc0(){sendtodevice("polladc0")}
def polladc1(){sendtodevice("polladc1")}
def polladc4(){sendtodevice("polladc4")}
def polladc5(){sendtodevice("polladc5")}
def nopolladc0(){sendtodevice("nopolladc0")}
def nopolladc1(){sendtodevice("nopolladc1")}
def nopolladc4(){sendtodevice("nopolladc4")}
def nopolladc5(){sendtodevice("nopolladc5")}
def push(b){
log.debug "got button $b "
if (b=="1") {
if (device.currentValue("digout1")=="on") return off1();
if (device.currentValue("digout1")=="off") return on1();
}
if (b=="2") {
if (device.currentValue("digout2")=="on") return off2();
if (device.currentValue("digout2")=="off") return on2();
}
if (b=="3") {
if (device.currentValue("digout3")=="on") return off3();
if (device.currentValue("digout3")=="off") return on3();
}
if (b=="4") {
if (device.currentValue("digout4")=="on") return off4();
if (device.currentValue("digout4")=="off") return on4();
}
if (b=="10") return getadc0();
if (b=="11") return getadc1();
if (b=="14") return getadc4();
if (b=="15") return getadc5();
if (b=="30") return polladc0();
if (b=="31") return polladc1();
if (b=="34") return polladc4();
if (b=="35") return polladc5();
if (b=="40") return polladc0();
if (b=="41") return polladc1();
if (b=="44") return polladc4();
if (b=="45") return polladc5();
}
def sendtodevice(String mystr){
//log.debug "mystr is $mystr"
mystr=mystr.padRight(16,".") // mystr should be 16 bytes!
//log.debug "mystr is $mystr"
//mystr = mystr.reverse() // must reverse since little-endian(?)
//log.debug "mystr is $mystr"
def packed = hubitat.helper.HexUtils.byteArrayToHexString(mystr.getBytes())
log.info "sending $mystr, packed is $packed "
def commandtosend = "he wattr 0x${device.deviceNetworkId} 8 0x000 0x010 0x42 {10"+packed+"}" // SAMPLELIGHT_ENDPOINT is defined as 8 in device code // the 10 on the end means 16 bytes length
//log.debug "$commandtosend"
return commandtosend
}
def configure() {
// Device-Watch allows 2 check-in misses from device + ping (plus 2 min lag time)
sendEvent(name: "checkInterval", value: 2 * 10 * 60 + 2 * 60, displayed: false, data: [protocol: "zigbee", hubHardwareId: device.hub.hardwareID])
log.debug "Configuring Reporting and Bindings."
zigbee.onOffRefresh() + zigbee.onOffConfig()
}
def convertNTC(float average){ // https://learn.adafruit.com/thermistor/using-a-thermistor
float THERMISTORNOMINAL = 10000 // resistance at 25 degrees C
float TEMPERATURENOMINAL = 25 // temp. for nominal resistance (almost always 25 C)
float BCOEFFICIENT = 3950 // The beta coefficient of the thermistor (usually 3000-4000)
float SERIESRESISTOR = 10000 // the value of the 'other' resistor
float maxreading = 8191 // the max adc reading
// convert the value to resistance
average = maxreading / average - 1;
average = SERIESRESISTOR / average;
float steinhart = average / THERMISTORNOMINAL; // (R/Ro)
steinhart = Math.log(steinhart); // ln(R/Ro)
steinhart /= BCOEFFICIENT; // 1/B * ln(R/Ro)
steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
steinhart = 1.0 / steinhart; // Invert
steinhart -= 273.15; // convert absolute temp to C
steinhart = (steinhart*1.8) + 32;
return steinhart
}