-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
homeseer-status-led-blinking-color-child.groovy
275 lines (204 loc) · 6.56 KB
/
homeseer-status-led-blinking-color-child.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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
* HomeSeer Status LED Blinking Color Child v1.0
* (Child Device for HomeSeer HS-WD200+, and HS-FC200+)
*
* Allows users to change the Status LED Color using any of the built-in SmartApps.
*
*
* Changelog:
*
* 1.0 (10/31/2020)
* - Initial Release
*
*
* Copyright 2020 HomeSeer
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import groovy.transform.Field
@Field static Map supportedColors = [red: "FF0000", green: "00FF00", blue: "0000FF", magenta: "FF00FF", yellow: "FFFF00", cyan: "00FFFF", white: "FFFFFF"]
@Field static int colorTempMinBlinkingValue = 4500
metadata {
definition (
name: "HomeSeer Status LED Blinking Color Child",
namespace: "HomeSeer",
author: "Kevin LaFramboise (krlaframboise)",
ocfDeviceType: "oic.d.light",
mnmn: "SmartThingsCommunity",
vid: "2e1a6c3f-54f4-33ed-b344-c61e2f0547b6"
) {
capability "Switch"
capability "Color Control"
capability "Color Temperature"
capability "platemusic11009.message"
capability "platemusic11009.messageTwo"
capability "platemusic11009.hsStatusLedBlinkingColor"
capability "platemusic11009.hsChildDeviceWorkaround"
}
preferences() {
input "debugOutput", "enum",
title: "Debug Logging",
required: false,
defaultValue: 1,
options: [0:"Disabled", 1:"Enabled [DEFAULT]"]
}
}
def parse(String description) {
logDebug "parse($description)..."
return []
}
def installed() {
logDebug "installed()..."
state.lastBlinking = false
state.lastColorName = "white"
initialize()
}
def updated() {
logDebug "updated()..."
initialize()
}
void initialize() {
state.debugLoggingEnabled = (parent.safeToInt(settings?.debugOutput, 1) != 0)
if (!device.currentValue("workaround")) {
state.ledNum = (parent.safeToInt(getDataValue("ledNum")) ?: null)
String name = getDataValue("ledName")
sendEvent(name: "workaround", value: (name ? "statusLed${name}" : "statusLedAll"))
}
if (!device.currentValue("message")) {
sendEvent(name:"message", value:"<small><b>Switch Off</b>: LED Off<br><b>Color Control:</b> LED Color</small>")
}
if (!device.currentValue("messageTwo")) {
sendEvent(name:"messageTwo", value:"<small><b>Color Temperature</b>:<br>Above ${colorTempMinBlinkingValue} = Blink Enabled</small>")
}
if (device.currentValue("colorTemperature") == null) {
sendEvent(name: "colorTemperature", value: 1, unit: "K")
}
if (device.currentValue("statusLedColor") == null) {
sendColorEvents("white")
}
}
def setStatusLedColor(color) {
logDebug "setStatusLedColor($color)..."
executeSetStatusLedColor(color, parent.isBlinkingColorName(color))
}
def off() {
logDebug "off()..."
executeSetStatusLedColor("off")
}
def on() {
logDebug "on()..."
executeSetStatusLedColor(state.lastColorName)
}
def setColorTemperature(temperature) {
logDebug "setColorTemperature($temperature)..."
sendEvent(name: "colorTemperature", value: temperature, unit: "K")
boolean blinking = (temperature >= colorTempMinBlinkingValue)
executeSetStatusLedColor(state.lastColorName, blinking)
}
def setSaturation(saturation) {
logDebug "setSaturation($saturation)..."
setColor([hue: device.currentValue("hue"), saturation: saturation])
}
def setHue(hue) {
logDebug "setHue($hue)..."
setColor([hue: hue, saturation: device.currentValue("saturation")])
}
def setColor(color) {
logDebug "setColor($color)..."
int hue = parent.safeToPercentInt(color.hue)
int saturation = parent.safeToPercentInt(color.saturation)
String hex = color.hex
if (hex) {
def hsv = colorUtil.hexToHsv(hex)
hue = parent.safeToPercentInt(hsv[0])
saturation = parent.safeToPercentInt(hsv[1])
}
else {
hex = colorUtil.hsvToHex(hue, saturation)
}
sendEvent(name: "hue", value: hue)
sendEvent(name: "saturation", value: saturation)
executeSetStatusLedColor(findClosestMatchingSupportedColor(hex))
}
String findClosestMatchingSupportedColor(originalHex) {
String closestColor = "white"
int closestDistance = 1000
def rgb = colorUtil.hexToRgb(originalHex)
supportedColors.each { name, hex ->
def supportedRGB = colorUtil.hexToRgb(hex)
int distance = (Math.abs(rgb[0] - supportedRGB[0]) + Math.abs(rgb[1] - supportedRGB[1]) + Math.abs(rgb[2] - supportedRGB[2]))
if (distance < closestDistance) {
closestDistance = distance
closestColor = name
}
}
return closestColor
}
void executeSetStatusLedColor(String color, blinking=null) {
blinking = (blinking == null) ? parent.isBlinkingColorName(color) : blinking
String colorName = parent.formatColorName(color, blinking)
parent.executeSetStatusLedColor(colorName, state.ledNum)
}
void sendColorEvents(statusLedColor) {
sendEventIfNew("statusLedColor", statusLedColor)
if (statusLedColor != "off") {
state.lastColorName = statusLedColor
sendBlinkingEvents(statusLedColor)
sendEventIfNew("switch", "on")
String color = parent.formatColorName(statusLedColor, false)
String hex = supportedColors.get(color)
sendEventIfNew("color", hex)
def hsv = colorUtil.hexToHsv(hex)
sendEventIfNew("hue", parent.safeToPercentInt(hsv[0]))
sendEventIfNew("saturation", parent.safeToPercentInt(hsv[1]))
}
else {
sendEventIfNew("switch", "off")
}
}
void sendBlinkingEvents(statusLedColor) {
boolean blinking = parent.isBlinkingColorName(statusLedColor)
state.lastBlinking = blinking
int colorTemp = parent.safeToInt(device.currentValue("colorTemperature"))
Integer newColorTemp = null
if (blinking && (colorTemp < colorTempMinBlinkingValue)) {
newColorTemp = (colorTempMinBlinkingValue + 1000)
}
else if (!blinking && (colorTemp > colorTempMinBlinkingValue)) {
newColorTemp = (colorTempMinBlinkingValue - 1000)
}
if (newColorTemp != null) {
sendEventIfNew("colorTemperature", newColorTemp, "K")
}
}
void sendEventIfNew(String name, value, String unit="") {
if (device.currentValue(name) != value) {
def desc = "${device.displayName}: ${name} is ${value}${unit}"
logDebug(desc)
Map evt = [
name: name,
value: value,
descriptionText: desc
]
if (unit) {
evt.unit = unit
}
sendEvent(evt)
}
}
void logDebug(String msg) {
if (state.debugLoggingEnabled) {
log.debug(msg)
}
}