-
Notifications
You must be signed in to change notification settings - Fork 7
/
TCP Bulb.groovy
167 lines (133 loc) · 3.97 KB
/
TCP Bulb.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
/**
* TCP Bulb
*
*
*/
metadata {
definition (name: "TCP Bulb", namespace: "mujica", author: "Todd Wackford - Ule") {
capability "Switch"
capability "Polling"
capability "Power Meter"
capability "Refresh"
capability "Switch Level"
attribute "stepsize", "string"
command "levelUp"
command "levelDown"
command "on"
command "off"
command "setBulbPower"
}
simulator {
// TODO: define status and reply messages here
}
preferences {
input "stepsize", "number", title: "Step Size", description: "Dimmer Step Size", defaultValue: 5
}
tiles {
standardTile("switch", "device.switch", width: 2, height: 2, canChangeIcon: true) {
state "on", label:'${name}', action:"switch.off", icon:"st.switches.light.on", backgroundColor:"#79b821", nextState:"turningOff"
state "off", label:'${name}', action:"switch.on", icon:"st.switches.light.off", backgroundColor:"#ffffff", nextState:"turningOn"
state "turningOn", label:'${name}', action:"switch.off", icon:"st.switches.light.on", backgroundColor:"#79b821", nextState:"turningOff"
state "turningOff", label:'${name}', action:"switch.on", icon:"st.switches.light.off", backgroundColor:"#ffffff", nextState:"turningOn"
}
controlTile("levelSliderControl", "device.level", "slider", height: 1, width: 2, inactiveLabel: false) {
state "level", action:"switch level.setLevel"
}
standardTile("refresh", "device.switch", inactiveLabel: false, decoration: "flat") {
state "default", label:"", action:"refresh.refresh", icon:"st.secondary.refresh"
}
valueTile("level", "device.level", inactiveLabel: false, decoration: "flat") {
state "level", label: 'Level ${currentValue}%'
}
standardTile("lUp", "device.switchLevel", inactiveLabel: false,decoration: "flat", canChangeIcon: false) {
state "default", action:"levelUp", icon:"st.illuminance.illuminance.bright"
}
standardTile("lDown", "device.switchLevel", inactiveLabel: false,decoration: "flat", canChangeIcon: false) {
state "default", action:"levelDown", icon:"st.illuminance.illuminance.light"
}
valueTile( "power", "device.power", inactiveLabel: false, decoration: "flat") {
state "power", label: '${currentValue} Watts'
}
main(["switch"])
details(["switch", "lUp", "lDown", "levelSliderControl", "level" , "power", "refresh" ])
}
}
def parse(description) {
def results = []
if ( description == "updated" )
return
if (description?.name && description?.value)
{
results << createEvent(name: "${description?.name}", value: "${description?.value}")
}
}
def setBulbPower(value) {
state.bulbPower = value
log.debug "In child with bulbPower of ${state.bulbPower}"
}
def on() {
log.debug "Executing 'on'"
sendEvent(name:"switch",value:on)
parent.on(this)
parent.poll(this)
}
def off() {
log.debug "Executing 'off'"
sendEvent(name:"switch",value:off)
parent.off(this)
sendEvent(name: "power", value: 0.0)
parent.poll(this)
}
def levelUp() {
def level = device.latestValue("level") as Integer ?: 0
def step = state.stepsize as float
level+= step
if ( level > 100 )
level = 100
setLevel(level)
}
def levelDown() {
def level = device.latestValue("level") as Integer ?: 0
def step = state.stepsize as float
level-= step
if ( level < 1 )
level = 1
setLevel(level)
}
def setLevel(value) {
log.debug "in setLevel with value: ${value}"
def level = value as Integer
sendEvent( name: "level", value: level )
sendEvent( name: "switch.setLevel", value:level )
parent.setLevel( this, level )
if (( level > 0 ) && ( level <= 100 ))
on()
else
off()
parent.poll(this)
}
def poll() {
log.debug "Executing poll()"
parent.poll(this)
}
def refresh() {
log.debug "Executing refresh()"
parent.poll(this)
}
def installed() {
initialize()
}
def updated() {
initialize()
refresh()
}
def initialize() {
if ( !settings.stepsize )
state.stepsize = 10 //set the default stepsize
else
state.stepsize = settings.stepsize
}
def uninstalled() {
log.debug "Executing 'uninstall' in device type"
parent.uninstallFromChildDevice(this)
}