Update step-notifier.groovy
[smartapps.git] / official / mini-hue-controller.groovy
1 /**
2  *  Mini Hue Controller
3  *
4  *  Copyright 2014 SmartThings
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7  *  in compliance with the License. You may obtain a copy of the License at:
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
12  *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
13  *  for the specific language governing permissions and limitations under the License.
14  *
15  */
16 definition(
17     name: "Mini Hue Controller",
18     namespace: "smartthings",
19     author: "SmartThings",
20     description: "Control one or more Hue bulbs using an Aeon MiniMote.",
21     category: "SmartThings Labs",
22     iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
23     iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png",
24     iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png")
25
26
27 preferences {
28         section("Control these lights") {
29                 input "bulbs", "capability.colorControl", title: "Hue light bulbs", multiple: true
30         }
31         section("Using this controller") {
32                 input "controller", "capability.button", title: "Aeon minimote"
33         }
34 }
35
36 def installed() {
37         log.debug "Installed with settings: ${settings}"
38         state.colorIndex = -1    
39         initialize()
40 }
41
42 def updated() {
43         log.debug "Updated with settings: ${settings}"
44         unsubscribe()
45         initialize()
46 }
47
48 def initialize() {
49         subscribe controller, "button", buttonHandler
50 }
51
52 def buttonHandler(evt) {
53         switch(evt.jsonData?.buttonNumber) {
54         case 2:
55                 if (evt.value == "held") {
56                 bulbs.setLevel(100)
57             }
58             else {
59                 levelUp()
60             }
61                 break
62         
63         case 3:
64                 if (evt.value == "held") {
65                 def color = [name:"Soft White", hue: 23, saturation: 56]
66                 bulbs.setColor(hue: color.hue, saturation: color.saturation)                    
67             }
68             else {
69                 changeColor()
70             }
71                 break
72         
73         case 4:
74                 if (evt.value == "held") {
75                 bulbs.setLevel(10)
76             }
77             else {
78                 levelDown()
79             }
80                 break
81         
82         default:
83                 toggleState()
84                 break
85     }
86 }
87
88 private toggleState() {
89         if (currentSwitchState == "on") {
90         log.debug "off"
91         bulbs.off()
92     }
93     else {
94         log.debug "on"
95         bulbs.on()
96     }
97 }
98
99 private levelUp() {
100         def level = Math.min(currentSwitchLevel + 10, 100)
101     log.debug "level = $level"
102     bulbs.setLevel(level)
103 }
104
105 private levelDown() {
106         def level = Math.max(currentSwitchLevel - 10, 10)
107         log.debug "level = $level"
108     bulbs.setLevel(level)
109 }
110
111 private changeColor() {
112
113         final colors = [
114         [name:"Soft White", hue: 23, saturation: 56],
115         [name:"Daylight", hue: 53, saturation: 91],
116         [name:"White", hue: 52, saturation: 19],
117         [name:"Warm White", hue: 20, saturation: 80],
118         [name:"Blue", hue: 70, saturation: 100],
119         [name:"Green", hue: 39, saturation: 100],
120         [name:"Yellow", hue: 25, saturation: 100],
121         [name:"Orange", hue: 10, saturation: 100],
122         [name:"Purple", hue: 75, saturation: 100],
123         [name:"Pink", hue: 83, saturation: 100],
124         [name:"Red", hue: 100, saturation: 100]
125         ]
126     
127     final maxIndex = colors.size() - 1
128     
129     if (state.colorIndex < maxIndex) {
130         state.colorIndex = state.colorIndex + 1
131     }
132     else {
133         state.colorIndex = 0
134     }
135     
136     def color = colors[state.colorIndex]
137     bulbs.setColor(hue: color.hue, saturation: color.saturation)
138 }
139
140 private getCurrentSwitchState() {
141         def on = 0
142     def off = 0
143     bulbs.each {
144         if (it.currentValue("switch") == "on") {
145                 on++
146         }
147         else {
148                 off++
149         }
150     }
151     on > off ? "on" : "off"
152 }
153
154 private getCurrentSwitchLevel() {
155         def level = 0
156     bulbs.each {
157         level = Math.max(it.currentValue("level")?.toInteger() ?: 0, level)
158     }
159     level.toInteger()
160 }