forked from Ledridge/SmartThings
-
Notifications
You must be signed in to change notification settings - Fork 21
/
SmartApp_DLI_Web_Power_Switch_with_Virtual_Tiles
136 lines (114 loc) · 4.89 KB
/
SmartApp_DLI_Web_Power_Switch_with_Virtual_Tiles
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
/**
* DLI Web Power Switch with Virtual Tiles
*
*/
definition(
name: "DLI Web Power Switch with Virtual Tiles.",
namespace: "Ledridge",
author: "Ledridge",
description: "Use this app with a Digital Logger Inc. Web Power Switch and it will create virtual switches for each of the embedded switches.",
category: "Convenience",
iconUrl: "http://cdn.flaticon.com/png/256/25823.png",
iconX2Url: "http://cdn.flaticon.com/png/256/25823.png",
iconX3Url: "http://cdn.flaticon.com/png/256/25823.png")
preferences {
page(name: "mainDevice", uninstall: true, install:false)
page(name: "virtualDetails", uninstall: true, install:true)
page(name: "virtualSwitches", uninstall: true, install:true)
}
def mainDevice() {
dynamicPage(name: "mainDevice", title: "Setup virtual app and multi-switch device", nextPage: "virtualDetails", uninstall: true, install:false) {
section {
input "master", "capability.switch", multiple: false, required: true, title: "Choose the device with multiple switches", image: "http://cdn.flaticon.com/png/256/61163.png"
label title: "Assign a name for this virtual tile handler", required: false
paragraph: "Assign switches to virtual tiles or real switches on next page"
}
}
}
def virtualDetails() {
unsubscribe()
syncChildDevices()
dynamicPage(name: "virtualDetails", title: "Which virtual switches to create for $master.label?", uninstall: true, install:true) {
section {
input "switches", "enum", multiple: true, required: false, refreshAfterSelection:true, options:templates(), title:"Select Switches", description: "Choose which switches of $master.label you would like to have as separate switches", image: "http://cdn.flaticon.com/png/256/25823.png"
}
section("Current Virtual Switches of $master.label:") {
def kids = getChildDevices()
kids.each { paragraph "$it.label" }
}
}
}
def templates() {
return [ "switch":"Web Power Switch 7", "Outlet1":"Outlet 1", "Outlet2":"Outlet 2", "Outlet3":"Outlet 3", "Outlet4":"Outlet 4", "Outlet5":"Outlet 5", "Outlet6":"Outlet6", "Outlet7":"Outlet 7", "Outlet8":"Outlet 8", "OutletA":"All Outlets"]
}
def installed() {
syncChildDevices()
def kids = getChildDevices()
}
def updated() {
syncChildDevices()
def kids = getChildDevices()
unsubscribe()
kids.each { subscribe(master, "$it.name", vswitch) }
}
def uninstalled() {
removeChildDevices()
}
def vswitch(evt) {
log.debug evt.descriptionText
def vswitch = evt.descriptionText.find(/outlet./).replaceAll(" ","")
log.debug "child switch to change: $vswitch event: $evt.value DDNI:${ddni(vswitch)}"
def vkid = getChildDevice(ddni(vswitch))
log.debug "$evt.value $evt.deviceId $evt.description $evt.descriptionText"
log.debug "vkid: $vkid"
vkid."${evt.value}"()
}
def mswitch(evt) { log.debug "$evt.value $evt.deviceId $evt.description $evt.descriptionText" }
def OutletStatus(childDevice) {
log.debug "Parent OUTLET_STATUS: ${childDevice}"
def num = childDevice.device.name.replaceAll("Outlet","")
log.debug "Parent OUTLET_STATUS: ${num}"
if(num) {return master.OutletStatus(num)} else { return "Unknown" ;log.debug "No switch number provided for off" }
}
def OutletName(childDevice) {
log.debug "Parent OUTLET_NAME: ${childDevice}"
def num = childDevice.device.name.replaceAll("Outlet","")
log.debug "Parent OUTLET_NAME: ${num}"
if(num) {return master.OutletName(num)} else { return "Unknown" ;log.debug "No switch number provided for off" }
}
def OutletAction(childDevice,action) {
log.debug "Parent OUTLET_ACTION: ${childDevice} ${action}"
def num = childDevice.device.name.replaceAll("Outlet","")
log.debug "Parent OUTLET_ACTION: ${num} ${action}"
if(num) { master.OutletAction(num,action)} else { master.off();log.debug "No switch number provided for off" }
}
private syncChildDevices() {
switches.each { def e = getChildDevice(ddni(it)); if(!e) { createChild(it) } }
def switchKids = getChildDevices()
def removeKids = switchKids.name - switches
log.debug "SWs: $switches del: $removeKids kids: $removeKids"
removeKids.each { rem ->
def delKid = getChildDevice(ddni(rem))
log.debug delKid.deviceNetworkId
removeChildDevices(delKid)
}
}
private createChild(vt) {
def label = templates().getAt(vt)
log.debug "Label $label"
def addedvt = addChildDevice("Ledridge", "vTile_DLI", ddni(vt), null, [name:vt, label:label, completedSetup: true])
log.info "created Virtual Switch ${addedvt.displayName} with DNI ${addedvt.deviceNetworkId}"
}
private removeChildDevices(delete) {
unsubscribe()
if(!delete) { delete = getChildDevices() }
delete.each {
deleteChildDevice(it.deviceNetworkId)
log.debug "deleted ${delete.displayName}"
}
}
private ddni(id){
if(!state.appId) { state.appId = app.id }
def ddni = state.appId + "/" + id
return ddni
}