forked from twack/smartthings-devices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbetter-virtual-dimmer.device.groovy
193 lines (155 loc) · 5.58 KB
/
better-virtual-dimmer.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
/*****************************************************************************
Device: VirtualBetterDimmer.device.groovy
Author: [email protected]
Version: 0.1
Date: 2013-11-16
Purpose: To implement horizontal slider and up/down toggle in virtual dimmer
device. This is great device to use with "Dim With Me" app. This
functionality can also be found in "BetterDimmer.app.groovy".
Use License: Non-Profit Open Software License version 3.0 (NPOSL-3.0)
http://opensource.org/licenses/NPOSL-3.0
******************************************************************************
* Changes
******************************************************************************
*
* Change 1: 2013-11-16 (wackford)
* Initial Build
*
* Change 2: 2014-10-10 (twackford)
* Rebuilt to add metadata
*
* Change 3: 2014-10-12 (twackford)
* Moved preferences section into metadata section
*
* Change 4: 2014-10-22 (twackford)
* Fixed on/off override so it will work with other apps/devices
*
******************************************************************************
Other Info: Special thanks to Danny Kleinman at ST for helping me get the
state stuff figured out. The Android state filtering had me
stumped.
*****************************************************************************/
metadata {
// Automatically generated. Make future change here.
definition (name: "Better Virtual Dimmer", author: "[email protected]") {
capability "Switch"
capability "Switch Level"
capability "Refresh"
capability "Polling"
attribute "stepsize", "string"
command "levelUp"
command "levelDown"
command "getLevel"
command "dimmerOn"
command "dimmerOff"
}
preferences {
input "stepsize", "number", title: "Step Size", description: "Dimmer Step Size", defaultValue: 10, required: false, displayDuringSetup: true
}
tiles {
standardTile("switch", "device.switch", width: 2, height: 2, canChangeIcon: true) {
state "on", label:'${name}', action:"dimmerOff", icon:"st.switches.light.on", backgroundColor:"#79b821"
state "off", label:'${name}', action:"dimmerOn", icon:"st.switches.light.off", backgroundColor:"#ffffff"
}
controlTile("levelSliderControl", "device.level", "slider", height: 1, width: 2, inactiveLabel: false) {
state "level", action:"switch level.setLevel", unit:"", backgroundColor:"#ffe71e"
}
valueTile("level", "device.level", inactiveLabel: false, decoration: "flat") {
state "level", label: 'Level ${currentValue}%'
}
standardTile("lUp", "device.switch", inactiveLabel: false,decoration: "flat", canChangeIcon: false) {
state "up", label:'', action:"levelUp",icon:"st.illuminance.illuminance.bright"
}
standardTile("lDown", "device.switch", inactiveLabel: false,decoration: "flat", canChangeIcon: false) {
state "down", label:'', action:"levelDown",icon:"st.illuminance.illuminance.light"
}
main(["switch"])
details(["switch","lUp","lDown","levelSliderControl","level" ,"Preferences"])
}
}
def initialize() {
if ( !settings.stepsize )
state.stepsize = 10
else
state.stepsize = settings.stepsize
if (!device.currentValue("level"))
setLevel(100)
}
def parse(String description) {}
def dimmerOn() { //made our own, since event was filtered by default on Android
log.info "on"
sendEvent(name:"switch",value:"on")
}
def dimmerOff() { //made our own, since event was filtered by default on Android
log.info "off"
sendEvent(name:"switch",value:"off")
}
def on() {
log.info "on"
sendEvent(name:"switch",value:"on")
}
def off() {
log.info "off"
sendEvent(name:"switch",value:"off")
}
def setLevel(val){
log.info "setLevel $val"
log.info "Step Size: ${state.stepsize}"
// make sure we don't drive switches past allowed values (command will hang device waiting for it to
// execute. Never commes back)
if (val < 0){
val = 0
}
if( val > 100){
val = 100
}
if (val == 0){ // I liked that 0 = off
sendEvent(name:"level",value:val)
dimmerOff()
}
else
{
dimmerOn()
sendEvent(name:"level",value:val)
sendEvent(name:"switch.setLevel",value:val) // had to add this to work if apps subscribed to
// setLevel event. "Dim With Me" was one.
}
}
def levelUp(){
if ( !state.stepsize ) {
initialize()
log.info "initialized on first up"
} else {
state.stepsize = settings.stepsize
}
def thisStep = state.stepsize as float
int nextLevel = device.currentValue("level") + thisStep
setLevel(nextLevel)
log.info "level up $nextLevel"
}
def levelDown(){
if ( !state.stepsize ) {
initialize()
log.info "initialized on first down"
} else {
state.stepsize = settings.stepsize
}
def thisStep = state.stepsize as float
int nextLevel = device.currentValue("level") - thisStep
setLevel(nextLevel)
log.info "level down $nextLevel"
}
def setLevel(val, dur){ //not called, but leave here for hue and other controllables
log.info "setLevel $val, $dur"
sendEvent(name:"setLevel",value:val)
}
def getLevel(){ //not called, dunno why but I'll leave it for later playing
log.info device.currentValue("level")
log.info device.currentValue("switch")
}
def poll() {
log.info "poll"
}
def refresh() {
log.info "refresh"
}