-
Notifications
You must be signed in to change notification settings - Fork 190
/
Brighten When It Opens
157 lines (133 loc) · 4.64 KB
/
Brighten When It Opens
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
/**
* App Name: [Scene Machine] Brighten When Open
*
* Author: Todd Wackford; modified by Bruce Ravenel
* Date: 2013-06-14
* Version: 1.1
*
* Updated: 2013-07-25
* Modified: 2015-02-13
*
* Change #1 Fixed bug where string null was being returned for non-dimmers and
* was trying to assign to variable.
*
* Change #2 Updated setLevel setion to work with bulbs that were not defined as type "Dimmer Switch"
*
* Modified Now triggered by a contact opening, and temporarily sets dimmers to 100%
*
*
* This app lets the user select from a list of switches or dimmers and record
* their currents states when a door opens, and then restores that state when
* the door closes
*
* Usage Note: GE-Jasco dimmers with ST is real buggy right now. Sometimes the levels
* get correctly, sometimes not.
* On/Off is OK.
* Other dimmers should be OK.
*
* Use License: Non-Profit Open Software License version 3.0 (NPOSL-3.0)
* http://opensource.org/licenses/NPOSL-3.0
*/
// Automatically generated. Make future change here.
definition(
name: "Brighten When Open",
namespace: "",
author: "[email protected]",
description: "This app lets the user select from a list of switches or dimmers and record their currents states when a door opens, and then restores that state when the door closes",
category: "My Apps",
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/window_contact.png",
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/[email protected]"
)
preferences {
section("When this door opens..."){
input "contact1", "capability.contactSensor", title: "Where?"
}
section("Select dimmers to brighten...") {
input "switches", "capability.switch", multiple: true, title "Which?"
}
//Uncomment this section below to test/change on the IDE. Smartphone does
//not need it.
//section("Record New Scene?") {
// input "record", "enum", title: "New Scene...", multiple: false,
// required: true, metadata:[values:['No','Yes']]
//}
}
def installed() {
// log.debug "Installed with settings: ${settings}"
subscribe(contact1, "contact.open", contactOpenHandler)
subscribe(contact1, "contact.closed", contactClosedHandler)
}
def updated() {
// log.debug "Updated with settings: ${settings}"
unsubscribe()
subscribe(contact1, "contact.open", contactOpenHandler)
subscribe(contact1, "contact.closed", contactClosedHandler)
}
def contactOpenHandler(evt) {
getDeviceSettings()
def i = 0
for(mySwitch in switches) {
if(mySwitch.latestValue("level")) mySwitch.setLevel(100)
else mySwitch.on() // a switch, not a dimmer
i++
}
}
def contactClosedHandler(evt) {
resetScene()
}
private resetScene() {
def i = 0
def switchName = ""
def switchType = ""
def switchState = ""
def dimmerValue = ""
for(myData in state.lastSwitchData) {
switchName = myData.switchName
switchType = myData.switchType
switchState = myData.switchState
if(myData.dimmerValue != "null") //
dimmerValue = myData.dimmerValue.toInteger() //BF #1
else //
dimmerValue = 0 //
log.info "switchName: $switchName"
log.info "switchType: $switchType"
log.info "switchState: $switchState"
log.info "dimmerValue: $dimmerValue"
if(switchState == "on")
switches[i].on()
if(dimmerValue > 0)
switches[i].setLevel(dimmerValue)
if(switchState == "off")
switches[i].off()
i++
log.info "Device setting is Done-------------------"
}
}
private getDeviceSettings() {
def cnt = 0
for(myCounter in switches) {
switches[cnt].refresh() //this was a try to get dimmer values (bug)
// switches[cnt].poll()
cnt++
}
state.lastSwitchData = [cnt]
def i = 0
def switchName = ""
def switchType = ""
def switchState = ""
def dimmerValue = ""
for(mySwitch in switches) {
switchName = mySwitch.device.toString()
switchType = mySwitch.name.toString()
switchState = mySwitch.latestValue("switch").toString()
//dimmerValue below returns null if it is not a dimmer
dimmerValue = mySwitch.latestValue("level").toString()
state.lastSwitchData[i] = [switchName: switchName,
switchType: switchType,
switchState: switchState,
dimmerValue: dimmerValue]
log.debug "SwitchData: ${state.lastSwitchData[i]}"
i++
}
}