4380f24408c0c5534d994ae06440b5625101a79c
[smartthings-infrastructure.git] / ColorControl / ColorControl.groovy
1 //Create a class for color control
2 package ColorControl
3 import SmartThing.SmartThing
4
5 public class ColorControl extends SmartThing {
6         // id, label, and display name of the device
7         String id
8         String label
9         String displayName
10         // Maps from features to values
11         HashMap<String, String> deviceValuesMap = new HashMap<String, String>()
12         HashMap<String, Integer> deviceIntValuesMap = new HashMap<String, Integer>()
13
14         ColorControl(Closure sendEvent, String id, String label, String displayName, String currentColor, Integer currentHue, Integer currentSaturation) {
15                 deviceValueSmartThing = deviceValuesMap
16                 deviceIntValueSmartThing = deviceIntValuesMap
17                 idSmartThing = id
18                 labelSmartThing = label
19                 displayNameSmartThing = displayName
20                 sendEventSmartThings = sendEvent
21
22                 // Initialization
23                 this.id = id
24                 this.label = label
25                 this.displayName = displayName
26
27                 deviceValuesMap.put("color", currentColor)
28                 deviceIntValuesMap.put("hue", currentHue)
29                 deviceIntValuesMap.put("saturation", currentSaturation)
30         }
31
32         def setColor(LinkedHashMap metaData) {
33                 def hexColor = metaData.hex
34                 def newColor
35                 switch (hexColor) {
36                         case "#0000FF":
37                                 newColor = "Blue"
38                                 break;
39                         case "#00FF00":
40                                 newColor = "Green"
41                                 break;
42                         case "#FFFF00":
43                                 newColor = "Yellow"
44                                 break;
45                         case "#FF6000":
46                                 newColor = "Orange"
47                                 break;
48                         case "#BF7FBF":
49                                 newColor = "Purple"
50                                 break;
51                         case "#FF5F5F":
52                                 newColor = "Pink"
53                                 break;
54                         case "#FF0000":
55                                 newColor = "Red"
56                                 break;
57                         default:
58                                 newColor = "Blue"
59                                 break;
60                 }
61                 setColor(newColor)
62         }
63
64         // Methods to set values
65         def setColor(String newValue) {
66                 action(newValue, "color")
67         }
68
69         def setHue(int newValue) {
70                 action(newValue, "hue")
71         }
72         
73         def setHue(double newValue) {
74                 setHue((int) newValue)
75         }
76
77         def setSaturation(int newValue) {
78                 action(newValue, "saturation")
79         }
80         
81         def setSaturation(double newValue) {
82                 setSaturation((int) newValue)
83         }
84 }