-
Notifications
You must be signed in to change notification settings - Fork 3
/
Virtual RGBW Light KH
261 lines (227 loc) · 8.11 KB
/
Virtual RGBW Light KH
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
/*
Virtual RGBW Light
Copyright 2018 -> 2022 Hubitat Inc. All Rights Reserved
modified by Kevin Hawkins v1.0 intercepting forced ON behaviour if level is passed as 0
ver pre28a 6/March/2022
*/
metadata {
definition (name: "Virtual RGBW Light KH", namespace: "hubitat", author: "Mike Maxwell, Kevin Hawkins") {
capability "Actuator"
capability "Color Control"
capability "Color Temperature"
capability "Switch"
capability "Switch Level"
capability "Light"
capability "ColorMode"
capability "LevelPreset"
}
preferences {
input name: "txtEnable", type: "bool", title: "Enable descriptionText logging", defaultValue: true
}
}
def updated(){}
def installed() {
setColorTemperature(2500)
setColor([hue:50, saturation:100, level:100])
}
void noCommands(cmd) {
log.trace "Command ${cmd} is not implemented on this device."
}
def parse(String description) { noCommands("parse") }
private eventSend(name,verb,value,unit = ""){
String descriptionText = "${device.displayName} ${name} ${verb} ${value}${unit}"
if (txtEnable) log.info "${descriptionText}"
if (unit != "") sendEvent(name: name, value: value ,descriptionText: descriptionText, unit:unit)
else sendEvent(name: name, value: value ,descriptionText: descriptionText)
}
def on() {
String verb = (device.currentValue("switch") == "on") ? "is" : "was turned"
eventSend("switch",verb,"on")
}
def off() {
String verb = (device.currentValue("switch") == "off") ? "is" : "was turned"
eventSend("switch",verb,"off")
}
def setLevel(value, rate = null) {
if (value == null) return
Integer level = limitIntegerRange(value,0,100)
if (level == 0) {
off()
return
}
if (device.currentValue("switch") != "on") on()
String verb = (device.currentValue("level") == level) ? "is" : "was set to"
eventSend("level",verb,level,"%")
}
def setColor(Map value){
if (value == null) return
if (value.hue == null || value.saturation == null) return
if (device.currentValue("colorMode") != "RGB") {
eventSend("colorMode","is","RGB")
}
Integer hue = limitIntegerRange(value.hue,0,100)
String verb = (device.currentValue("hue") == hue) ? "is" : "was set to"
eventSend("hue",verb,hue,"%")
setGenericName(hue)
Integer sat = limitIntegerRange(value.saturation,0,100)
verb = (device.currentValue("saturation") == sat) ? "is" : "was set to"
eventSend("saturation",verb,sat,"%")
if (value.level) {
Integer level = limitIntegerRange(value.level,0,100)
if ((level != 0) && (device.currentValue("switch") != "on")) {
on()
verb = (device.currentValue("level") == level) ? "is" : "was set to"
eventSend("level",verb,level,"%")
}
}
}
def setHue(value) {
if (value == null) return
Integer hue = limitIntegerRange(value,0,100)
if (device.currentValue("switch") != "on") on()
if (device.currentValue("colorMode") != "RGB") {
eventSend("colorMode","is","RGB")
}
String verb = (device.currentValue("hue") == hue) ? "is" : "was set to"
eventSend("hue",verb,hue,"%")
setGenericName(hue)
}
def setSaturation(value) {
if (value == null) return
Integer sat = limitIntegerRange(value,0,100)
if (device.currentValue("switch") != "on") on()
if (device.currentValue("colorMode") != "RGB") {
eventSend("colorMode","is","RGB")
}
String verb = (device.currentValue("saturation") == sat) ? "is" : "was set to"
eventSend("saturation",verb,sat,"%")
}
def setColorTemperature(value, level = null, tt = null) {
if (value == null) return
Integer ct = limitIntegerRange(value,1500,9000)
//if (device.currentValue("switch") != "on") on()
if (device.currentValue("colorMode") != "CT") {
eventSend("colorMode","is","CT")
}
String verb = (device.currentValue("colorTemperature") == ct) ? "is" : "was set to"
eventSend("colorTemperature",verb,ct,"°K")
setGenericTempName(ct)
setLevel(level, tt)
}
def setGenericTempName(value){
String genericName
Integer sat = value.toInteger()
if (sat <= 2000) genericName = "Sodium"
else if (sat <= 2100) genericName = "Starlight"
else if (sat < 2400) genericName = "Sunrise"
else if (sat < 2800) genericName = "Incandescent"
else if (sat < 3300) genericName = "Soft White"
else if (sat < 3500) genericName = "Warm White"
else if (sat < 4150) genericName = "Moonlight"
else if (sat <= 5000) genericName = "Horizon"
else if (sat < 5500) genericName = "Daylight"
else if (sat < 6000) genericName = "Electronic"
else if (sat <= 6500) genericName = "Skylight"
else if (sat < 20000) genericName = "Polar"
eventSend("colorName","is",genericName)
}
def setGenericName(value){
String colorName
Integer hue = value.toInteger()
if (!hiRezHue) hue = (hue * 3.6).toInteger()
switch (hue){
case 0..15: colorName = "Red"
break
case 16..45: colorName = "Orange"
break
case 46..75: colorName = "Yellow"
break
case 76..105: colorName = "Chartreuse"
break
case 106..135: colorName = "Green"
break
case 136..165: colorName = "Spring"
break
case 166..195: colorName = "Cyan"
break
case 196..225: colorName = "Azure"
break
case 226..255: colorName = "Blue"
break
case 256..285: colorName = "Violet"
break
case 286..315: colorName = "Magenta"
break
case 316..345: colorName = "Rose"
break
case 346..360: colorName = "Red"
break
}
eventSend("colorName","is",colorName)
}
def presetLevel(value, rate = null) {
if (value == null) return
Integer level = limitIntegerRange(value,0,100)
if (level == 0) {
off()
return
}
//if (device.currentValue("switch") != "on") on()
String verb = (device.currentValue("level") == level) ? "is" : "was set to"
eventSend("level",verb,level,"%")
}
def presetColor(Map value){
if (value == null) return
if (value.hue == null || value.saturation == null) return
if (device.currentValue("colorMode") != "RGB") {
eventSend("colorMode","is","RGB")
}
Integer hue = limitIntegerRange(value.hue,0,100)
String verb = (device.currentValue("hue") == hue) ? "is" : "was set to"
eventSend("hue",verb,hue,"%")
setGenericName(hue)
Integer sat = limitIntegerRange(value.saturation,0,100)
verb = (device.currentValue("saturation") == sat) ? "is" : "was set to"
eventSend("saturation",verb,sat,"%")
if (value.level) {
Integer level = limitIntegerRange(value.level,0,100)
presetLevel(level)
}
}
def presetHue(value) {
if (value == null) return
Integer hue = limitIntegerRange(value,0,100)
//if (device.currentValue("switch") != "on") on()
if (device.currentValue("colorMode") != "RGB") {
eventSend("colorMode","is","RGB")
}
String verb = (device.currentValue("hue") == hue) ? "is" : "was set to"
eventSend("hue",verb,hue,"%")
setGenericName(hue)
}
def presetSaturation(value) {
if (value == null) return
Integer sat = limitIntegerRange(value,0,100)
//if (device.currentValue("switch") != "on") on()
if (device.currentValue("colorMode") != "RGB") {
eventSend("colorMode","is","RGB")
}
String verb = (device.currentValue("saturation") == sat) ? "is" : "was set to"
eventSend("saturation",verb,sat,"%")
}
def presetColorTemperature(value, level = null, tt = null) {
if (value == null) return
Integer ct = limitIntegerRange(value,1500,9000)
//if (device.currentValue("switch") != "on") on()
if (device.currentValue("colorMode") != "CT") {
eventSend("colorMode","is","CT")
}
String verb = (device.currentValue("colorTemperature") == ct) ? "is" : "was set to"
eventSend("colorTemperature",verb,ct,"°K")
setGenericTempName(ct)
if (level) setLevel(level, tt)
}
Integer limitIntegerRange(value,min,max) {
Integer limit = value.toInteger()
return (limit < min) ? min : (limit > max) ? max : limit
}