1 //Create a class for color control
3 import Timer.SimulatedTimer
6 public class ColorControl {
10 private String displayName
12 private String currentColor
13 private String currentSwitch
15 private int currentLevel
17 private int currentHue
18 private int saturation
19 private int currentSaturation
20 private int colorTemperature
22 ColorControl(Closure sendEvent, String id, String label, String displayName, String color, int hue, int saturation, int level, String currentSwitch, int colorTemperature) {
25 this.displayName = displayName
27 this.currentColor = color
30 this.saturation = saturation
31 this.currentSaturation = saturation
33 this.currentLevel = level
34 this.currentSwitch = currentSwitch
35 this.colorTemperature = colorTemperature
36 this.sendEvent = sendEvent
40 def setValue(String value, String name) {
41 if ((name == "color") && (value != this.color)) {
43 this.currentColor = value
44 println("the color of the light is changed to $value!")
45 } else if ((name == "hue") && (value != this.hue)) {
46 this.hue = value.toInteger()
47 this.currentHue = value.toInteger()
48 println("The hue level of the light is changed to $value!")
49 } else if ((name == "saturation") && (value != this.saturation)) {
50 this.saturation = value.toInteger()
51 this.currentSaturation = value.toInteger()
52 println("The saturation level of the light is changed to $value!")
53 } else if ((name == "level") && (value != this.level)) {
54 this.currentLevel = value.toInteger()
55 this.level = value.toInteger()
56 println("The level of the light is changed to $value!")
57 } else if ((name == "currentSwitch") && (value != this.currentSwitch)) {
58 this.currentSwitch = value
59 println("The light is changed to $value!")
60 } else if ((name == "colorTemperature") && (value != this.colorTemperature)) {
61 this.colorTemperature = value.toInteger()
62 println("The color temperature level of the light is changed to $value!")
67 def setColor(LinkedHashMap metaData) {
68 def hexColor = metaData.hex
96 if (newColor != this.color) {
97 this.currentColor = newColor
99 println("The color of the light is changed to $newColor!")
100 sendEvent([name: "color", value: "$newColor", deviceId: this.id, descriptionText: "",
101 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
105 def setColor(String color) {
106 if (color != this.color) {
107 this.currentColor = color
109 println("The color of the light is changed to $color!")
110 sendEvent([name: "color", value: "$color", deviceId: this.id, descriptionText: "",
111 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
115 def setHue(int hue) {
116 if (hue != this.hue) {
118 this.currentHue = hue
119 println("The hue level of the light is changed to $hue!")
120 sendEvent([name: "hue", value: "$hue", deviceId: this.id, descriptionText: "",
121 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
125 def setHue(double hue) {
126 if (hue != this.hue) {
128 this.currentHue = hue
129 println("The hue level of the light is changed to $hue!")
130 sendEvent([name: "hue", value: "$hue", deviceId: this.id, descriptionText: "",
131 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
135 def setSaturation(int saturation) {
136 if (saturation != this.saturation) {
137 this.currentSaturation = saturation
138 this.saturation = saturation
139 println("The saturation level of the light is changed to $saturation!")
140 sendEvent([name: "saturation", value: "$saturation", deviceId: this.id, descriptionText: "",
141 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
145 def setSaturation(double saturation) {
146 if (saturation != this.saturation) {
147 this.currentSaturation = saturation
148 this.saturation = saturation
149 println("The saturation level of the light is changed to $saturation!")
150 sendEvent([name: "saturation", value: "$saturation", deviceId: this.id, descriptionText: "",
151 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
156 def setLevel(int level) {
157 if (level != this.level) {
158 this.currentLevel = level
160 println("The level of the light is changed to $level!")
161 sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
162 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
166 def setLevel(long level) {
167 if (level != this.level) {
168 this.currentLevel = level
170 println("The level of the light is changed to $level!")
171 sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
172 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
176 def setColorTemperature(int colorTemperature) {
177 if (colorTemperature != this.colorTemperature) {
178 this.colorTemperature = colorTemperature
179 println("The color temperature level of the light is changed to $colorTemperature!")
180 sendEvent([name: "colorTemperature", value: "$colorTemperature", deviceId: this.id, descriptionText: "",
181 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
186 if (this.currentSwitch != "on") {
187 this.currentSwitch = currentSwitch
188 println("The light is changed to on!")
189 sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
190 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
195 if (this.currentSwitch != "off") {
196 this.currentSwitch = currentSwitch
197 println("The light is changed to off!")
198 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
199 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
203 def currentValue(String deviceFeature) {
204 if (deviceFeature == "color") {
206 } else if (deviceFeature == "saturation") {
208 } else if (deviceFeature == "hue") {
210 } else if (deviceFeature == "level") {
212 } else if (deviceFeature == "colorTemperature") {
213 return colorTemperature
214 } else if (deviceFeature == "switch") {
219 def latestValue(String deviceFeature) {
220 if (deviceFeature == "color") {
222 } else if (deviceFeature == "saturation") {
224 } else if (deviceFeature == "hue") {
226 } else if (deviceFeature == "level") {
228 } else if (deviceFeature == "colorTemperature") {
229 return colorTemperature
230 } else if (deviceFeature == "switch") {