forked from adeparker/RuleMachine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RuleMachine.groovy
764 lines (720 loc) · 20.5 KB
/
RuleMachine.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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
/**
* Rule Machine
*
* Copyright 2015, 2016 Bruce Ravenel
*
* Version 1.9.0a 25 Mar 2016
*
* Version History
*
* 1.9.0 24 Mar 2016 Updates for Rule, small bug fixes
* 1.8.2 9 Mar 2016 Changed startup page for installation
* 1.8.1 3 Mar 2016 Changed method of getting Rule version
* 1.8.0 2 Mar 2016 Clean up, added Door control
* 1.7.6 24 Feb 2016 Added User Guide link, fixed Rule truth mechanism
* 1.7.5 21 Feb 2016 Improved custom command selection
* 1.7.4 20 Feb 2016 Added saved command display, UI improvements
* 1.7.3 14 Feb 2016 Improved Rule Machine initialization, fixed Delete custom commands bug
* 1.7.2 8 Feb 2016 Added set Boolean for Rules
* 1.7.1 5 Feb 2016 Added update Rule
* 1.7.0 31 Jan 2016 Added run Rule actions
* 1.6.6 10 Jan 2016 Improved method of getting custom device commands
* 1.6.5 1 Jan 2016 Added version numbers to main page
* 1.6.4 30 Dec 2015 Multi-commands
* 1.6.3 26 Dec 2015 UI improvements and icon per Michael Struck
* 1.6.2 25 Dec 2015 null parameter value patch in expert, maxwell
* 1.6.1 24 Dec 2015 UI improvement
* 1.6.0 23 Dec 2015 Added expert commands per Mike Maxwell
*
* This software if free for Private Use. You may use and modify the software without distributing it.
*
* This software and derivatives may not be used for commercial purposes.
* You may not modify, distribute or sublicense this software.
* You may not grant a sublicense to modify and distribute this software to third parties not included in the license.
*
* Software is provided without warranty and the software author/license owner cannot be held liable for damages.
*
*/
definition(
name: "Rule Machine",
singleInstance: true,
namespace: "bravenel",
author: "Bruce Ravenel and Mike Maxwell",
description: "Rule Machine",
category: "My Apps",
iconUrl: "https://raw.githubusercontent.com/bravenel/Rule-Trigger/master/smartapps/bravenel/RuleMachine.png",
iconX2Url: "https://raw.githubusercontent.com/bravenel/Rule-Trigger/master/smartapps/bravenel/RuleMachine%402x.png",
iconX3Url: "https://raw.githubusercontent.com/bravenel/Rule-Trigger/master/smartapps/bravenel/RuleMachine%402x.png"
)
preferences {
page(name: "mainPage")
page(name: "firstPage")
page(name: "removePage")
//expert pages
page(name: "customCommandsPAGE")
page(name: "generalApprovalPAGE")
page(name: "addCustomCommandPAGE")
page(name: "customParamsPAGE")
}
def mainPage() {
if(!state.setup) firstRun()
else {
if(state.ruleState) state.ruleState = null // obsolete
def nApps = childApps.size()
dynamicPage(name: "mainPage", title: "Installed Rules, Triggers and Actions " + (nApps > 0 ? "[$nApps]" : ""), install: true, uninstall: false) {
section {
app(name: "childRules", appName: "Rule", namespace: "bravenel", title: "Create New Rule...", multiple: true)
}
section ("Expert Features") {
href("customCommandsPAGE", title: null, description: anyCustom() ? "Custom Commands..." : "Tap to create Custom Commands", state: anyCustom())
}
section ("Rule Machine User Guide") {
href url:"https://community.smartthings.com/t/rule-machine-user-guide/40176", style:"embedded", required:false, description:"Tap to view User Guide", title: ""
}
section ("Remove Rule Machine"){
href "removePage", description: "Tap to remove Rule Machine and Rules", title: ""
}
section ("Version 1.9.0a/" + (nApps > 0 ? "${childApps[0].appVersion()}" : "---")) { }
}
}
}
def removePage() {
dynamicPage(name: "removePage", title: "Remove Rule Machine And All Rules", install: false, uninstall: true) {
section ("WARNING!\n\nRemoving Rule Machine also removes all Rules\n") {
}
}
}
def installed() {
}
def updated() {
}
def firstRun() {
state.setup = true
state.ruleSubscribers = [:]
dynamicPage(name: "firstPage", title: "Hit Done to install Rule Machine", install: true, uninstall: false) { }
}
def childVersion() {
def result = "---"
if(childApps.size() > 0) result = childApps[0].appVersion()
return result
}
def ruleList(appLabel) {
def result = []
childApps.each { child ->
if(child.name == "Rule" && child.label != appLabel) result << child.label
}
return result
}
def subscribeRule(appLabel, ruleName, ruleTruth, childMethod) {
// log.debug "subscribe: $appLabel, $ruleName, $ruleTruth, $childMethod"
ruleName.each {name ->
state.ruleSubscribers[name].each {if(it == appLabel) return}
if(state.ruleSubscribers[name] == null) state.ruleSubscribers[name] = ["$appLabel":ruleTruth]
else state.ruleSubscribers[name] << ["$appLabel":ruleTruth]
}
}
def setRuleTruth(appLabel, ruleTruth) {
// log.debug "setRuleTruth: $appLabel, $ruleTruth, ${state.ruleState[appLabel]}"
def thisList = state.ruleSubscribers[appLabel]
thisList.each {
if(it.value == null || "$it.value" == "$ruleTruth") {
childApps.each { child ->
if(child.label == it.key) child.ruleHandler(appLabel, ruleTruth)
}
}
}
}
def setRuleBoolean(rule, ruleBoolean, appLabel) {
// log.debug "setRuleBoolean: $appLabel, $ruleBoolean"
childApps.each { child ->
rule.each {
if(child.label == it) child.setBoolean(ruleBoolean, appLabel)
}
}
}
def currentRule(appLabel) {
def result
childApps.each { child ->
if(child.label == appLabel) result = child.revealSuccess()
}
return result
}
def childUninstalled() {
// log.debug "childUninstalled called"
}
def removeChild(appLabel) {
// log.debug "removeChild: $appLabel"
unSubscribeRule(appLabel)
if(state.ruleSubscribers[appLabel] != null) state.ruleSubscribers.remove(appLabel)
}
def unSubscribeRule(appLabel) {
// log.debug "unSubscribeRule: $appLabel"
state.ruleSubscribers.each { rule ->
def newList = [:]
rule.value.each {list ->
if(list.key != appLabel) newList << list
}
rule.value = newList
}
}
def runRule(rule, appLabel) {
// log.debug "runRule: $rule, $appLabel"
childApps.each { child ->
rule.each {
if(child.label == it) child.ruleEvaluator(appLabel)
}
}
}
def runRuleAct(rule, appLabel) {
// log.debug "runRuleAct: $rule, $appLabel"
childApps.each { child ->
rule.each {
if(child.label == it) child.ruleActions(appLabel)
}
}
}
def runUpdate(rule) {
// log.debug "runUpdate: $rule"
childApps.each { child ->
rule.each {
if(child.label == it) child.updated()
}
}
}
/*****custom command specific pages *****/
def generalApprovalPAGE(params){
def title = params.title
def method = params.method
def result
dynamicPage(name: "generalApprovalPAGE", title: title ){
section() {
if (method) {
result = app."${method}"()
paragraph "${result}"
}
}
}
}
def customCommandsPAGE() {
if (!state.lastCmdIDX) state.lastCmdIDX = 0
def savedCommands = getMyCommands()
dynamicPage(name: "customCommandsPAGE", title: "Custom Commands", uninstall: false, install: false) {
def hasCommands = savedCommands.size() != 0
def result = ""
if (hasCommands){
def cmdMaps = state.customCommands ?: []
cmdMaps.each{ cmd ->
def cont = cmd.value.text.size() > 30 ? "..." : ""
result = result + "\n\t" + cmd.value.text.take(30) + cont
}
} else state.lastCmdIDX = 0
section(hasCommands ? "Saved commands:\n" + result : "") {
if (hasCommands) {
input(
name : "deleteCmds"
,title : "Delete saved commands"
,multiple : true
,required : false
,description : ""
,type : "enum"
,options : savedCommands
,submitOnChange : true
)
if (isValidCommand(deleteCmds)){
href( "generalApprovalPAGE"
,title : "Delete commands now"
,description : ""
,state : null
,params : [method:"deleteCommands",title:"Delete commands"]
,submitOnChange : true
)
}
paragraph("")
}
getCapab()
if(myCapab) getDevs()
if(devices && hasCommands) {
input(
name : "testCmd"
,title : "Test saved command on\n$devices"
,multiple : false
,required : false
,description : ""
,type : "enum"
,options : savedCommands
,submitOnChange : true
)
def res = execCommand(settings.testCmd)
if (res) paragraph("${result}")
}
}
section(){
if (devices){
href( "addCustomCommandPAGE"
,title : "New custom command..."
,description: ""
,state : null
)
}
}
}
}
def getCapab() {
def myOptions = ["Acceleration", "Actuator", "Button", "Carbon monoxide detector", "Contact", "Dimmer", "Door", "Energy meter", "Garage door", "Humidity", "Illuminance",
"Lock", "Motion", "Power meter", "Presence", "Smoke detector", "Switch", "Temperature", "Thermostat", "Water sensor", "Music player"]
def result = input "myCapab", "enum", title: "Select capability for test device", required: false, options: myOptions.sort(), submitOnChange: true
}
def getDevs() {
def multi = false
def thisName = ""
def thisCapab = ""
switch(myCapab) {
case "Switch":
thisName = "switch"
thisCapab = "switch"
break
case "Actuator":
thisName = "actuator"
thisCapab = "actuator"
break
case "Motion":
thisName = "motion sensor"
thisCapab = "motionSensor"
break
case "Button":
thisName = "button device"
thisCapab = "button"
break
case "Acceleration":
thisName = "acceleration sensor"
thisCapab = "accelerationSensor"
break
case "Contact":
thisName = "contact sensor"
thisCapab = "contactSensor"
break
case "Presence":
thisName = "presence sensor"
thisCapab = "presenceSensor"
break
case "Garage door":
thisName = "garage door"
thisCapab = "garageDoorControl"
break
case "Door":
thisName = "door"
thisCapab = "doorControl"
break
case "Lock":
thisName = "lock"
thisCapab = "lock"
break
case "Dimmer":
thisName = "dimmer" + (multi ? "s" : "")
thisCapab = "switchLevel"
break
case "Temperature":
thisName = "temperature sensor" + (multi ? "s" : "")
thisCapab = "temperatureMeasurement"
break
case "Thermostat":
thisName = "thermostat" + (multi ? "s" : "")
thisCapab = "thermostat"
break
case "Humidity":
thisName = "humidity sensor" + (multi ? "s" : "")
thisCapab = "relativeHumidityMeasurement"
break
case "Illuminance":
thisName = "illuminance sensor" + (multi ? "s" : "")
thisCapab = "illuminanceMeasurement"
break
case "Energy meter":
thisName = "energy meter" + (multi ? "s" : "")
thisCapab = "energyMeter"
break
case "Power meter":
thisName = "power meter" + (multi ? "s" : "")
thisCapab = "powerMeter"
break
case "Carbon monoxide detector":
thisName = "CO detector" + (multi ? "s" : "")
thisCapab = "carbonMonoxideDetector"
break
case "Smoke detector":
thisName = "smoke detector" + (multi ? "s" : "")
thisCapab = "smokeDetector"
break
case "Water sensor":
thisName = "water sensor"
thisCapab = "waterSensor"
break
case "Music player":
thisName = "music player"
thisCapab = "musicPlayer"
break
}
def result = input "devices", "capability.$thisCapab", title: "Select $thisName to test for commands", required: false, multiple: multi, submitOnChange: true
}
def addCustomCommandPAGE(){
def cmdLabel = getCmdLabel()
def complete = ""
def test = false
def rest = getDeviceCommands()
rest = "$rest"[1..-2]
def pageTitle = "Create new custom command from\n\n${devices}\n\nAvailable commands:\n\n" + rest
if (cmdLabel){
complete = "complete"
test = true
}
dynamicPage(name: "addCustomCommandPAGE", title: pageTitle, uninstall: false, install: false) {
section(){
input(
name : "cCmd"
,title : "Select custom command"
,multiple : false
,required : false
,type : "enum"
,options : getDeviceCommands()
,submitOnChange : true
)
href( "customParamsPAGE"
,title: "Parameters"
,description: parameterLabel()
,state: null
)
}
if (cCmd){
def result = execTestCommand()
section("Configured command: ${cmdLabel}\n${result}"){
if (result == "succeeded"){
if (!commandExists(cmdLabel)){
href( "generalApprovalPAGE"
,title : "Save command now"
,description: ""
,state : null
,params : [method:"addCommand",title:"Add Command"]
)
}
}
}
}
}
}
def customParamsPAGE(p){
def ct = settings.findAll{it.key.startsWith("cpType_")}
state.howManyP = ct.size() + 1
def howMany = state.howManyP
dynamicPage(name: "customParamsPAGE", title: "Select parameters", uninstall: false) {
if(howMany) {
for (int i = 1; i <= howMany; i++) {
def thisParam = "cpType_" + i
def myParam = ct.find {it.key == thisParam}
section("Parameter #${i}") {
getParamType(thisParam, i != howMany)
if(myParam) {
def pType = myParam.value
getPvalue(pType, i)
}
}
}
}
}
}
/***** child specific methods *****/
def getCommandMap(cmdID){
return state.customCommands["${cmdID}"]
}
/***** local custom command specific methods *****/
def anyCustom(){
def result = null
if (getCommands()) result = "complete"
return result
}
def getParamType(myParam,isLast){
def myOptions = ["string", "number", "decimal"]
def result = input (
name : myParam
,type : "enum"
,title : "parameter type"
,required : isLast
,options : myOptions
,submitOnChange : true
)
return result
}
def getPvalue(myPtype, n){
def myVal = "cpVal_" + n
def result = null
if (myPtype == "string"){
result = input(
name : myVal
,title : "string value"
,type : "text"
,required : false
)
} else if (myPtype == "number"){
result = input(
name : myVal
,title : "integer value"
,type : "number"
,required : false
)
} else if (myPtype == "decimal"){
result = input(
name : myVal
,title : "decimal value"
,type : "decimal"
,required : false
)
}
return result
}
def getCmdLabel(){
def cmd
if (settings.cCmd) cmd = settings.cCmd.value
def cpTypes = settings.findAll{it.key.startsWith("cpType_")}.sort()
def result = null
if (cmd) {
result = "${cmd}("
if (cpTypes.size() == 0){
result = result + ")"
} else {
def r = getParams(cpTypes)
if (r == "") result = r
else result = "${result}${r})"
}
}
return result
}
def getParams(cpTypes){
def result = ""
def cpValue
def badParam = false
cpTypes.each{ cpType ->
def i = cpType.key.replaceAll("cpType_","")
def cpVal = settings.find{it.key == "cpVal_${i}"}
if (cpVal){
cpValue = cpVal.value
if (cpType.value == "string"){
result = result + "'${cpValue}',"
} else {
if (cpValue.isNumber()){
result = result + "${cpValue},"
} else {
result = result + "[${cpValue}]: is not a number,"
}
}
} else {
badParam = true
}
}
if (badParam) result = ""
else result = result[0..-2]
return result
}
def parameterLabel(){
def howMany = (state.howManyP ?: 1) - 1
def result = ""
if (howMany) {
for (int i = 1; i <= howMany; i++) {
result = result + parameterLabelN(i) + "\n"
}
result = result[0..-2]
}
return result
}
def parameterLabelN(i){
def result = ""
def cpType = settings.find{it.key == "cpType_${i}"}
def cpVal = settings.find{it.key == "cpVal_${i}"}
def cpValue
if (cpVal) cpValue = cpVal.value
else cpValue = "missing value"
if (cpType){
result = "p${i} - type:${cpType.value}, value:${cpValue}"
}
return result
}
def getParamsAsList(cpTypes){
def result = []
cpTypes.each{ cpType ->
def i = cpType.key.replaceAll("cpType_","")
def cpVal = settings.find{it.key == "cpVal_${i}"}
if (cpVal){
if (cpType.value == "string"){
result << "${cpVal.value}"
} else if (cpType.value == "decimal"){
result << cpVal.value.toBigDecimal()
} else { // if (cpType.value == "number" && cpVal.value.isInteger()){
result << cpVal.value.toInteger()
}
} else {
result << "missing value"
}
}
return result
}
def getCommands(){
def result = []
def cmdMaps = state.customCommands ?: []
cmdMaps.each{ cmd ->
def option = [(cmd.key):([cmd.value.text, cmd.value.capab == null ? "actuator" : cmd.value.capab])]
result.push(option)
}
return result
}
def getMyCommands(){
def result = []
def cmdMaps = state.customCommands ?: []
cmdMaps.each{ cmd ->
def option = [(cmd.key):(cmd.value.text)]
result.push(option)
}
return result
}
def isValidCommand(cmdIDS){
def result = false
cmdIDS.each{ cmdID ->
def cmd = state.customCommands["${cmdID}"]
if (cmd) result = true
}
return result
}
def deleteCommands(){
def result
def cmdMaps = state.customCommands
if (deleteCmds.size() == 1) result = "Command removed"
else result = "Commands removed"
deleteCmds.each{ it ->
cmdMaps.remove(it)
}
return result
}
def commandExists(cmd){
def result = false
if (state.customCommands){
result = state.customCommands.find{ it.value.text == "${cmd}" }
}
return result
}
def addCommand(){
def capabs = [ "Acceleration" : "accelerationSensor",
"Button" : "button",
"Carbon monoxide detector" : "carbonMonoxideDetector",
"Contact" : "contactSensor",
"Dimmer" : "switchLevel",
"Door" : "doorControl",
"Energy meter" : "energyMeter",
"Garage door" : "garageDoorControl",
"Humidity" : "humiditySensor",
"Illuminance" : "illuminanceSensor",
"Lock" : "lock",
"Motion" : "motionSensor",
"Power meter" : "powerMeter",
"Presence" : "presenceSensor",
"Smoke detector" : "smokeDetector",
"Switch" : "switch",
"Temperature" : "temperatureMeasurement",
"Thermostat" : "thermostat",
"Water sensor" : "waterSensor",
"Music player" : "musicPlayer",
"Actuator" : "actuator"]
def result
def newCmd = getCmdLabel()
def found = commandExists(newCmd)
def cmdMaps = state.customCommands
//only update if not found...
if (!found) {
state.lastCmdIDX = state.lastCmdIDX + 1
def nextIDX = state.lastCmdIDX
def cmd = [text:"${newCmd}",cmd:"${cCmd}"]
def params = [:]
def cpTypes = settings.findAll{it.key.startsWith("cpType_")}.sort()
cpTypes.each{ cpType ->
def i = cpType.key.replaceAll("cpType_","")
def cpVal = settings.find{it.key == "cpVal_${i}"}
def param = ["type":"${cpType.value}","value":"${cpVal.value}"]
params.put(i, param)
}
cmd.put("params",params)
if(myCapab) cmd.put("capab", capabs[myCapab]) else cmd.put("capab", "actuator")
if (cmdMaps) cmdMaps.put((nextIDX),cmd)
else state.customCommands = [(nextIDX):cmd]
result = "command: ${newCmd} was added"
} else {
result = "command: ${newCmd} was not added, it already exists."
}
return result
}
def execTestCommand(){
def result
def cTypes = settings.findAll{it.key.startsWith("cpType_")}
def p = getParamsAsList(cTypes.sort()) as Object[]
devices.each { device ->
try {
device."${cCmd}"(p)
result = "succeeded"
}
catch (IllegalArgumentException e){
def em = e as String
def ems = em.split(":")
ems = ems[2].replace(" [","").replace("]","")
ems = ems.replaceAll(", ","\n")
// result = "failed, valid commands:\n${ems}"
result = "failed"
}
catch (e){
result = "failed with:\n${e}"
}
}
return result
}
def execCommand(cmdID){
def result = ""
def pList = []
if (cmdID){
def cmdMap = state.customCommands["${cmdID}"]
if (testCmd && cmdMap) {
def params = cmdMap.params.sort()
params.each{ p ->
if (p.value.type == "string"){
pList << "${p.value.value}"
} else if (p.value.type == "decimal"){
pList << p.value.value.toBigDecimal()
} else {
pList << p.value.value.toInteger()
}
}
def p = pList as Object[]
devices.each { device ->
try {
device."${cmdMap.cmd}"(p)
result = "Command succeeded"
}
catch (IllegalArgumentException e){
def em = e as String
def ems = em.split(":")
ems = ems[2].replace(" [","").replace("]","")
ems = ems.replaceAll(", ","\n")
result = "Command failed, valid commands:\n${ems}"
}
catch (e){
result = "failed with:\n${e}"
}
}
return result
}
}
}
def getDeviceCommands(){
def result = ""
devices.each { device ->
result = device.supportedCommands.collect{ it as String }
//log.debug "supportedCommands:${result}"
}
return result
}
def isExpert(){
return getCommands().size() > 0
}