Infrastructure now works for: locks, alarms, thermostats, and non-hue lights.
[smartthings-infrastructure.git] / ColorControl / ColorControl.groovy
1 //Create a class for color control
2 package ColorControl
3 import Timer.SimulatedTimer
4
5
6 public class ColorControl {
7         def sendEvent
8         private String id
9         private String label
10         private String displayName
11         private String color
12         private String currentColor
13         private String currentSwitch
14         private int level
15         private int currentLevel
16         private int hue
17         private int currentHue
18         private int saturation
19         private int currentSaturation
20         private int colorTemperature
21         
22         ColorControl(Closure sendEvent, String id, String label, String displayName, String color, int hue, int saturation, int level, String currentSwitch, int colorTemperature) {
23                 this.id = id
24                 this.label = label
25                 this.displayName = displayName
26                 this.color = color
27                 this.currentColor = color
28                 this.hue = hue
29                 this.currentHue = hue
30                 this.saturation = saturation
31                 this.currentSaturation = saturation
32                 this.level = level
33                 this.currentLevel = level
34                 this.currentSwitch = currentSwitch
35                 this.colorTemperature = colorTemperature
36                 this.sendEvent = sendEvent
37         }
38         
39         //By model checker
40         def setValue(String value, String name) {
41                 if ((name == "color") && (value != this.color)) {
42                         this.color = value
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!")
63                 }
64         }
65
66         //methods
67         def setColor(String color) {
68                 if (color != this.color) {
69                         this.currentColor = color
70                         this.color = color
71                         println("The color of the light is changed to $color!")
72                         sendEvent([name: "color", value: "$color", deviceId: this.id, descriptionText: "",
73                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
74                 }
75         }
76
77         def setHue(int hue) {
78                 if (hue != this.hue) {
79                         this.hue = hue
80                         this.currentHue = hue
81                         println("The hue level of the light is changed to $hue!")
82                         sendEvent([name: "hue", value: "$hue", deviceId: this.id, descriptionText: "",
83                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
84                 }
85         }
86
87         def setSaturation(int saturation) {
88                 if (saturation != this.saturation) {
89                         this.currentSaturation = saturation
90                         this.saturation = saturation
91                         println("The saturation level of the light is changed to $saturation!")
92                         sendEvent([name: "saturation", value: "$saturation", deviceId: this.id, descriptionText: "",
93                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
94                 }
95         }
96
97         def setLevel(int level) {
98                 if (level != this.level) {
99                         this.currentLevel = level
100                         this.level = level
101                         println("The level of the light is changed to $level!")
102                         sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
103                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
104                 }
105         }
106
107         def setColorTemperature(int colorTemperature) {
108                 if (colorTemperature != this.colorTemperature) {
109                         this.colorTemperature = colorTemperature
110                         println("The color temperature level of the light is changed to $colorTemperature!")
111                         sendEvent([name: "colorTemperature", value: "$colorTemperature", deviceId: this.id, descriptionText: "",
112                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
113                 }
114         }
115
116         def on(String currentSwitch) {
117                 if (currentSwitch != this.currentSwitch) {
118                         this.currentSwitch = currentSwitch
119                         println("The light is changed to $currentSwitch!")
120                         sendEvent([name: "switch", value: "$currentSwitch", deviceId: this.id, descriptionText: "",
121                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
122                         sendEvent([name: "switch.on", value: "$currentSwitch", deviceId: this.id, descriptionText: "",
123                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
124                 }
125         }
126
127         def off(String currentSwitch) {
128                 if (currentSwitch != this.currentSwitch) {
129                         this.currentSwitch = currentSwitch
130                         println("The light is changed to $currentSwitch!")
131                         sendEvent([name: "switch", value: "$currentSwitch", deviceId: this.id, descriptionText: "",
132                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
133                         sendEvent([name: "switch.off", value: "$currentSwitch", deviceId: this.id, descriptionText: "",
134                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
135                 }
136         }
137
138         def currentValue(String deviceFeature) {
139                 if (deviceFeature == "color") {
140                         return color
141                 } else if (deviceFeature == "saturation") {
142                         return saturation
143                 } else if (deviceFeature == "hue") {
144                         return hue
145                 } else if (deviceFeature == "level") {
146                         return level
147                 } else if (deviceFeature == "colorTemperature") {
148                         return colorTemperature
149                 } else if (deviceFeature == "switch") {
150                         return currentSwitch
151                 }
152         }
153
154         def latestValue(String deviceFeature) {
155                 if (deviceFeature == "color") {
156                         return color
157                 } else if (deviceFeature == "saturation") {
158                         return saturation
159                 } else if (deviceFeature == "hue") {
160                         return hue
161                 } else if (deviceFeature == "level") {
162                         return level
163                 } else if (deviceFeature == "colorTemperature") {
164                         return colorTemperature
165                 } else if (deviceFeature == "switch") {
166                         return currentSwitch
167                 }
168         }
169 }