forked from Lcstyle/HBBWASpaManager
-
Notifications
You must be signed in to change notification settings - Fork 1
/
HBBWASpaManager_Parent_Device.groovy
611 lines (557 loc) · 21.3 KB
/
HBBWASpaManager_Parent_Device.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
/*
* Hubitat BWA Spa Manager
* -> Parent Device Driver
*
* Copyright 2023/2024 Kurt Sannders
* based on work Copyright 2020 Richard Powell that he did for Hubitat
*
* Copyright 2020 Richard Powell
* based on work Copyright 2020 Nathan Spencer that he did for 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.
*
*/
#include kurtsanders.SanderSoft-Library
#include kurtsanders.BWA-Library
@Field static String DEVICE_NAME_PREFIX = "HB BWA SPA"
@Field static String PARENT_DEVICE_NAME = "HB BPA SPA Parent"
@Field static final String VERSION = "1.3.1"
@Field static final List VALID_SPA_BYTE_ARRAY = [-1, -81]
@Field static String THERMOSTAT_CHILD_DEVICE_NAME = "HB BWA SPA Thermostat"
@Field static String SWITCH_CHILD_DEVICE_NAME = "HB BWA SPA Switch"
@Field static String UNKNOWN = "unknown"
@Field static final List READYMODES = ["Ready", "Rest"]
@Field static final List TEMPRANGES = ["low", "high"]
metadata {
definition (name: PARENT_DEVICE_NAME, namespace: NAMESPACE, author: AUTHOR_NAME) {
capability "Actuator"
capability "Configuration"
capability "Refresh"
capability "Sensor"
capability "Switch"
capability "TemperatureMeasurement"
attribute "deviceId", "string"
attribute "blowerState", "enum", ["off","low","medium","high"]
attribute "blowerPump", "enum", ["true","false"]
attribute "heatingSetpoint", "number"
attribute "online", "enum", ["Online","Offline"]
attribute "pollingInterval", "string"
attribute "ReadyMode", "enum", READYMODES
attribute "spaStatus", "string"
attribute "spaTime", "string"
attribute "spaTimeLocalDeltaMins", "number"
attribute "is24HourTime", "enum", ["true","false"]
attribute "temperature", "number"
attribute "TempRange", "enum", TEMPRANGES
attribute "thermostatOperatingState", "string"
attribute "thermostatSetpoint", "number"
attribute "updated_at", "string"
attribute "wifiState", "enum", ["WiFi OK","WiFi Spa Not Communicating","WiFi Startup","WiFi Prime","WiFi Hold","WiFi Panel","WiFi Unnknown"]
command "setReadyMode"
command "setSpaToLocalTime"
command "setTempRange"
}
}
preferences {
//Logging Options
input name: "logLevel", type: "enum", title: fmtTitle("Logging Level"),
description: fmtDesc("Logs selected level and above"), defaultValue: 0, options: LOG_LEVELS
input name: "logLevelTime", type: "enum", title: fmtTitle("Logging Level Time"),
description: fmtDesc("Time to enable Debug/Trace logging"),defaultValue: 0, options: LOG_TIMES
//Help Link
input name: "helpInfo", type: "hidden", title: fmtHelpInfo("Community Link")
input name: "autoSpaTimeSync", type: "bool", title: "Automatically sync spa time to local hub time"
}
@Field static Map PUMP_BUTTON_MAP = [
1: 4, // Pump 1 maps to Balboa API Button #4
2: 5, // Pump 2 maps to Balboa API Button #5 etc.
3: 6,
4: 7,
5: 8,
6: 9]
@Field static Map LIGHT_BUTTON_MAP = [
1: 17, // Light 1 maps to Balboa API Button #17 etc.
2: 18]
@Field static Map AUX_BUTTON_MAP = [
1: 22, // Aux 1 maps to Balboa API Button #22 etc.
2: 23
]
@Field static Map BUTTON_MAP = [
Blower: 12,
Mister: 14,
Aux1: 22,
Aux2: 23,
TempRange: 80,
HeatMode: 81]
def installed() {
}
def updated() {
logInfo "Preferences Updated..."
checkLogLevel()
}
def on() {
if (device.currentValue("ReadyMode") == "Rest") {
setReadyMode()
}
if (device.currentValue("TempRange") == "low") {
setTempRange()
}
}
def off() {
if (device.currentValue("ReadyMode") == "Ready") {
setReadyMode()
}
if (device.currentValue("TempRange") == "high") {
setTempRange()
}
}
void setSpaToLocalTime() {
def timeData = nowFormatted("HH:mm")
logDebug "setSpaToLocalTime(): SystemLocalTime = ${timeData}"
sendCommand("SystemTime", timeData)
}
void setReadyMode() {
logDebug "In parent setReadyMode..."
ReadyMode = device.currentValue("ReadyMode")
logDebug "Device current mode is ${ReadyMode}"
text_mode = ReadyMode == "Ready" ? "Rest" : "Ready"
sendEvent(name: "ReadyMode", value: text_mode)
logDebug "Setting mode to ${text_mode}"
sendCommand("Button", BUTTON_MAP.HeatMode)
}
void setTempRange() {
logDebug "In parent setTempRange..."
TempRange = device.currentValue("TempRange")
logDebug "Device current heating mode is ${TempRange}"
temp_range = TempRange == "low" ? "high" : "high"
sendEvent(name: "TempRange", value: temp_range)
logDebug "Setting hTemp Range to ${temp_range}"
sendCommand("Button", BUTTON_MAP.TempRange)
}
def sendCommand(action, data) {
/* sendCommand parameters and example values
SetTemp / 69
TempUnits / C
TempUnits / F
TimeFormat / 12
TimeFormat / 24
SystemTime / 01:49
Filters / <base64 string of bytes dictating the filter cycle>
Request / <panel request, such as list filter cycles??>
*/
parent.sendCommand(device.currentValue("deviceId"), action, data)
runIn(2, refresh)
}
def parseDeviceData(Map results) {
results.each {name, value ->
logDebug ("name: ${name}, value ${value}")
sendEvent(name: name, value: value, displayed: true)
}
}
def createChildDevices(spaConfiguration) {
// Thermostat
fetchChild(true, "Thermostat", "Thermostat")
/* The incoming spaConfiguration has a list of all the possible add-on devices like
pumps, lights, etc. mapped to a boolean indicating whether or not this particular
hot tub actually has that specific device installed on it.
Iterate through all the possible add-on devices and if the hot tub we're working
with actually has that device installed on it then we will go ahead and create a
child device for it (passing "true" as the first parameter to fetchChild(...) will
have it go and create a device if it doesn't exist already.
*/
// Pumps
spaConfiguration.each { k, v ->
if (k.startsWith("Pump") && v == true) {
def pumpNumber = k[4].toInteger()
fetchChild(true, "Switch", "Pump ${pumpNumber}", PUMP_BUTTON_MAP[pumpNumber])
}
}
// Lights
spaConfiguration.each { k, v ->
if (k.startsWith("Light") && v == true) {
def lightNumber = k[5].toInteger()
fetchChild(true, "Switch", "Light ${lightNumber}", LIGHT_BUTTON_MAP[lightNumber])
}
}
// Blower Pump
// TODO: Support Blower properly. It's not a "Switch" device type.
// fetchChild(true, ???, "Blower", BUTTON_MAP.Blower)
sendEvent(name:"blowerPump",value: spaConfiguration["Blower"]?"true":"false" )
// Aux
spaConfiguration.each { k, v ->
if (k.startsWith("Aux") && v == true) {
def lightNumber = k[3].toInteger()
fetchChild(true, "Switch", "Aux ${lightNumber}", AUX_BUTTON_MAP[lightNumber])
}
}
// Mister
if (spaConfiguration["Mister"] == true) {
fetchChild(true, "Switch", "Mister", BUTTON_MAP.Mister)
}
}
def parsePanelData(encodedData) {
logTrace "encodedData= ${encodedData}"
if (encodedData==null) return
def now = new Date().format('EEE MMM d, h:mm:ss a',location.timeZone)
byte[] decoded = encodedData.decodeBase64()
// Check for a valid SPA array prefix
if (decoded[1..2] != VALID_SPA_BYTE_ARRAY) {
if (encodedData != null) {
logWarn "BWA Cloud Spa Error: encodedData '${encodedData}' is NOT a valid SPA panel data. Is the Spa Online?"
} else {
logWarn "BWA Cloud Spa Error: SPA panel data was (null). Is the Spa Online?"
encodedData = "Spa BWA cloud spa data was null"
}
sendEvent(name: "online", value: "Offline")
sendEvent(name: "spaStatus", value: UNKNOWN)
sendEvent(name: "updated_at", value: "Error: ${encodedData} at: ${now}")
// Send events to Thermostat child device
def thermostatChildDevice = fetchChild(false, "Thermostat", "Thermostat")
if (thermostatChildDevice != null) {
thermostatChildDevice.sendEvents([
[name: "temperature", value: -1 ],
[name: "thermostatMode", value: "heat"],
[name: "thermostatOperatingState", value: "idle"]
])
}
// Reset child switch devices switch and speed currentValues when spa is offline
logDebug "Resetting switch child devices capabilities and attribute state(s) due to spa 'Offline'"
def switchDevices = getChildDevices()
switchDevices.each {
if (it.typeName == "HB BWA SPA Switch") {
if (it.currentValue('speed')=="${UNKNOWN}") {
logDebug "Reset device: '${it.label}' speed to ${UNKNOWN}"
it.sendEvent(name: "speed", value: UNKNOWN, descriptionText: "Auto-reset speed to 'unkown' by spa 'offline'")
}
if (it.currentValue('switch')=='on') {
logDebug "Reset device: '${it.label}' switch to 'off'"
it.sendEvent(name: "switch", value: "off" , descriptionText: "Auto-reset switch to 'off' by spa 'offline'")
}
}
}
return false
}
// Get Spa Time
String currentSpaTimeHour = decoded[7].toString().padLeft(2,"0")
String currentSpaTimeMinute = decoded[8].toString().padLeft(2,"0")
String is24HourTime = (decoded[13] & 2) != 0 ? "true" : "false"
String spaTime = Date.parse("HHmm", "${currentSpaTimeHour}${currentSpaTimeMinute}").format((is24HourTime.toBoolean())?"HH:mm":"h:mm a")
// Get delta mins from spa internal clock to hub's localtime
use(TimeCategory){
def locaTime = Date.parse("HH:mm",nowFormatted("HH:mm"))
logTrace "locaTime = ${locaTime}"
def spaHHmm = Date.parse("HH:mm","${currentSpaTimeHour}:${currentSpaTimeMinute}")
logTrace "spaHHmm = ${spaHHmm}"
def duration = TimeCategory.minus(spaHHmm, locaTime)
logTrace "duration= ${duration}"
Integer durationMins = duration.hours*60 + duration.minutes
logTrace "durationMins= ${durationMins}"
sendEvent(name: "spaTimeLocalDeltaMins", value: "${durationMins}")
}
def temperatureScale = (decoded[13] & 1) == 0 ? "F" : "C"
def actualTemperature = decoded[6]
def targetTemperature = decoded[24]
def isHeating = (decoded[14] & 48) != 0
def heatingMode = (decoded[14] & 4) == 4 ? "high" : "low"
def heatMode
switch (decoded[9]) {
case 0:
heatMode = "Ready"
break;
case 1:
heatMode = "Rest"
break;
case 2:
heatMode = "Ready in Rest"
break;
default:
heatMode = "None"
}
// Send events to Thermostat child device
def thermostatChildDevice = fetchChild(false, "Thermostat", "Thermostat")
if (thermostatChildDevice != null) {
thermostatChildDevice.sendEventsWithUnits([
[name: "temperature", value: actualTemperature, unit: temperatureScale],
[name: "heatingSetpoint", value: targetTemperature, unit: temperatureScale],
[name: "thermostatSetpoint", value: actualTemperature, unit: temperatureScale]
])
thermostatChildDevice.sendEvents([
[name: "thermostatMode", value: "heat"],
[name: "thermostatOperatingState", value: isHeating ? "heating" : "idle"],
])
}
def filtermode
switch (decoded[13] & 12) {
case 4:
filterMode = "Filter 1"
break;
case 8:
filterMode = "Filter 2"
break;
case 12:
filterMode = "Filter 1 & 2"
break;
case 0:
default:
filterMode = "Off"
}
def accessibilityType
switch (decoded[13] & 48) {
case 16:
accessibilityType = "Pump Light"
break;
case 32:
case 42:
accessibilityType = "None"
break;
default:
accessibilityType = "All"
}
// Spa Pumps
def pumpState = []
pumpState[0] = null
def pump1ChildDevice = fetchChild(false, "Switch", "Pump 1")
if (pump1ChildDevice != null) {
switch (decoded[15] & 3) { // Pump 1
case 1:
pumpState[1] = "low"
break
case 2:
pumpState[1] = "high"
break
default:
pumpState[1] = "off"
}
pump1ChildDevice.parse(pumpState[1])
}
def pump2ChildDevice = fetchChild(false, "Switch", "Pump 2")
if (pump2ChildDevice != null) {
switch (decoded[15] & 12) { // Pump 2
case 4:
pumpState[2] = "low"
break
case 8:
pumpState[2] = "high"
break
default:
pumpState[2] = "off"
}
pump2ChildDevice.parse(pumpState[2])
}
def pump3ChildDevice = fetchChild(false, "Switch", "Pump 3")
if (pump3ChildDevice != null) {
switch (decoded[15] & 48) { // Pump 3
case 16:
pumpState[3] = "low"
break
case 32:
pumpState[3] = "high"
break
default:
pumpState[3] = "off"
}
pump3ChildDevice.parse(pumpState[3])
}
def pump4ChildDevice = fetchChild(false, "Switch", "Pump 4")
if (pump4ChildDevice != null) {
switch (decoded[15] & 192) {
case 64:
pumpState[4] = "low"
break
case 128:
pumpState[4] = "high"
break
default:
pumpState[4] = "off"
}
pump4ChildDevice.parse(pumpState[4])
}
def pump5ChildDevice = fetchChild(false, "Switch", "Pump 5")
if (pump5ChildDevice != null) {
switch (decoded[16] & 3) {
case 1:
pumpState[5] = "low"
break
case 2:
pumpState[5] = "high"
break
default:
pumpState[5] = "off"
}
pump5ChildDevice.parse(pumpState[5])
}
def pump6ChildDevice = fetchChild(false, "Switch", "Pump 6")
if (pump6ChildDevice != null) {
switch (decoded[16] & 12) {
case 4:
pumpState[6] = "low"
break
case 8:
pumpState[6] = "high"
break
default:
pumpState[6] = "off"
}
pump6ChildDevice.parse(pumpState[6])
}
// TODO: Support Blower properly. It's not a switch device type
switch (decoded[17] & 12) {
case 4:
blowerState = "low"
break
case 8:
blowerState = "medium"
break
case 12:
blowerState = "high"
break
default:
blowerState = "off"
}
if (device.currentValue("blowerPump")) {
sendEvent(name: "blowerState", value: blowerState)
} else {
logDebug "blowerPump currentValue is ${device.currentValue("blowerPump")}, blowerState will not be set/updated"
}
// Lights
def lightState = []
lightState[0] = null
def light1ChildDevice = fetchChild(false, "Switch", "Light 1")
if (light1ChildDevice != null) {
lightState[1] = (decoded[18] & 3) != 0
light1ChildDevice.parse(lightState[1])
}
def light2ChildDevice = fetchChild(false, "Switch", "Light 2")
if (light2ChildDevice != null) {
lightState[2] = (decoded[18] & 12) != 0
light2ChildDevice.parse(lightState[2])
}
// Mister
def misterChildDevice = fetchChild(false, "Switch", "Mister")
def misterState = null
if (misterChildDevice != null) {
misterState = (decoded[19] & 1) != 0
misterChildDevice.parse(misterState)
}
// Aux
def auxState = []
auxState[0] = null
def aux1ChildDevice = fetchChild(false, "Switch", "Aux 1")
if (aux1ChildDevice != null) {
auxState[1] = (decoded[19] & 8) != 0
aux1ChildDevice.parse(auxState[1])
}
def aux2ChildDevice = fetchChild(false, "Switch", "Aux 2")
if (aux2ChildDevice != null) {
auxState[2] = (decoded[19] & 16) != 0
aux2ChildDevice.parse(auxState[2])
}
def wifiState
switch (decoded[16] & 240) {
case 0:
wifiState = "WiFi OK"
break
case 16:
wifiState = "WiFi Spa Not Communicating"
break
case 32:
wifiState = "WiFi Startup"
break
case 48:
wifiState = "WiFi Prime"
break
case 64:
wifiState = "WiFi Hold"
break
case 80:
wifiState = "WiFi Panel"
break
default:
wifiState = "Unknown"
}
def pumpStateStatus
if (decoded[15] < 1 && decoded[16] < 1 && (decoded[17] & 3) < 1) {
pumpStateStatus = "Off"
} else {
pumpStateStatus = isHeating ? "Low Heat" : "Low"
}
if (actualTemperature == 255) {
actualTemperature = device.currentValue("temperature") * (temperatureScale == "C" ? 2.0F : 1)
}
if (temperatureScale == "C") {
actualTemperature /= 2.0F
targetTemperature /= 2.0F
}
// Create events for the parent spa device
sendEvent(name: "spaStatus", value: "${heatMode} ${isHeating ? "heating to ${targetTemperature}°${temperatureScale}" : "not heating"}")
sendEvent(name: "ReadyMode", value: "${heatMode}")
sendEvent(name: "TempRange", value: "${heatingMode}")
sendEvent(name: "updated_at", value: "Updated at: ${now}")
sendEvent(name: "online", value: "Online")
sendEvent(name: "wifiState", value: "${wifiState}")
sendEvent(name: "temperature", value: actualTemperature, unit: temperatureScale)
sendEvent(name: "heatingSetpoint", value: targetTemperature, unit: temperatureScale)
sendEvent(name: "thermostatSetpoint", value: actualTemperature, unit: temperatureScale)
sendEvent(name: "thermostatOperatingState", value: isHeating ? "heating" : "idle")
sendEvent(name: "spaTime", value: "${spaTime}" )
sendEvent(name: "is24HourTime", value: "${is24HourTime}" )
if (device.currentValue("ReadyMode") == "Ready" && device.currentValue("TempRange") == "high") {
sendEvent(name: "switch", value: "on")
} else {
sendEvent(name: "switch", value: "off")
}
logTrace ("<br><ol><li>Actual Temperature : ${actualTemperature}"
+ "<li>Current Time Hour : ${currentSpaTimeHour}"
+ "<li>Current Time Minute : ${currentSpaTimeMinute}"
+ "<li>Is 24-Hour Time : ${is24HourTime}"
+ "<li>Temperature Scale : ${temperatureScale}"
+ "<li>Target Temperature : ${targetTemperature}"
+ "<li>Filter Mode : ${filterMode}"
+ "<li>Accessibility Type : ${accessibilityType}"
+ "<li>Heating Mode : ${heatingMode}"
+ "<li>lightState[1] : ${lightState[1]}"
+ "<li>lightState[2] : ${lightState[2]}"
+ "<li>Heat Mode : ${heatMode}"
+ "<li>Is Heating : ${isHeating}"
+ "<li>pumpState[1] : ${pumpState[1]}"
+ "<li>pumpState[2] : ${pumpState[2]}"
+ "<li>pumpState[3] : ${pumpState[3]}"
+ "<li>pumpState[4] : ${pumpState[4]}"
+ "<li>pumpState[5] : ${pumpState[5]}"
+ "<li>pumpState[6] : ${pumpState[6]}"
+ "<li>blowerState : ${blowerState}"
+ "<li>misterState : ${misterState}"
+ "<li>auxState[1] : ${auxState[1]}"
+ "<li>auxState[2] : ${auxState[2]}"
+ "<li>pumpStateStatus : ${pumpStateStatus}"
+ "<li>wifiState : ${wifiState}</ol>"
)
return true
}
def fetchChild(createIfDoesntExist, String type, String name, Integer balboaApiButtonNumber = 0) {
String thisId = device.id
def childDeviceName = "${thisId}-${name}"
logTrace "childDeviceName: '${childDeviceName}"
def cd = getChildDevice(childDeviceName)
if (!cd && createIfDoesntExist) {
def driverName = "${DEVICE_NAME_PREFIX} ${type}"
logInfo "Adding Child Device. Driver: '${driverName}', Name: '${childDeviceName}'"
cd = addChildDevice(NAMESPACE, driverName, childDeviceName, [name: "${device.displayName} {$name}", isComponent: true])
// Switches will need to know their respective Balboa API Button IDs
if (type == "Switch" && balboaApiButtonNumber > 0) {
cd.setBalboaAPIButtonNumber(balboaApiButtonNumber)
}
}
return cd
}
void refresh() {
logInfo "BWA Cloud Refresh Requested"
parent.pollChildren(override=true)
}