Update influxdb-logger.groovy
[smartapps.git] / official / hue-minimote.groovy
1 /**
2  *  My Living Room Lighting
3  *
4  *  Copyright 2015 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: "My Living Room Lighting",
18     namespace: "smartthings",
19     author: "smartthings",
20     description: "allow a button controller to control my hue lamps, tv lights, and floor lamp",
21     category: "My Apps",
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 which hue lamps?") {
29         input "hueLamps", "capability.colorControl", required: true, multiple: true
30     }
31     section("Control which TV lights?") {
32         input "tvLights", "capability.colorControl", required: false
33     }
34     section("Control other (non-hue) lamps?") {
35         input "otherLights", "capability.switch", required: false, multiple: true
36     }
37     section("Which button controller?") {
38         input "buttonDevice", "capability.button", required: true
39     }
40 }
41
42 def installed() {
43     log.debug "Installed with settings: ${settings}"
44     initialize()
45 }
46
47 def updated() {
48     log.debug "Updated with settings: ${settings}"
49     unsubscribe()
50     initialize()
51 }
52
53 def initialize() {
54     subscribe(buttonDevice, "button", buttonEvent)
55     hueLamps.each {
56         log.debug "lamp ${it.displayName} hue: ${it.currentHue}"
57         log.debug "lamp ${it.displayName} saturation: ${it.currentSaturation}"
58         log.debug "lamp ${it.displayName} color ${it.currentColor}"
59         log.debug "lamp ${it.displayName} level ${it.currentLevel}"
60     }
61
62     log.debug "tv hue: ${tvLights?.currentHue}"
63     log.debug "tv saturation: ${tvLights?.currentSaturation}"
64     log.debug "tv color ${tvLights?.currentColor}"
65     log.debug "tv level ${tvLights?.currentLevel}"
66 }
67
68 def buttonEvent(evt) {
69     def buttonState = evt.value // "pushed" or "held"
70     def buttonNumber = parseJson(evt.data)?.buttonNumber 
71     
72     log.debug "buttonState:  $buttonState"
73     log.debug "buttonNumber: $buttonNumber"
74     
75     if (!(1..4).contains(buttonNumber)) {
76         log.error "This app only supports four buttons. Invalid buttonNumber: $buttonNumber"
77         } else if (!(buttonState == "pushed" || buttonState == "held")) {
78             log.error "This app only supports button pushed and held values. Invalid button state: $buttonState"
79             } else { 
80                 def meth = "handleButton" + buttonNumber + buttonState.capitalize()
81                 log.debug "meth: $meth"
82                 "$meth"()
83             }
84         }
85
86 // normal "white" lighting
87 def handleButton1Pushed() {
88     log.debug "handle1Pushed"
89     
90     hueLamps.setColor(level: 100, hue: 20, saturation: 80)
91     
92     // notice the "?." operator - tvLights may not be set (required: false).
93     tvLights?.setColor(level: 100, hue: 100, saturation: 100)
94     otherLights?.on()
95 }
96
97 // turn everything off
98 def handleButton1Held() {
99     log.debug "handleButton1Held"
100     
101     hueLamps.off()
102     tvLights?.off()
103     otherLights?.off()
104 }
105
106 // soft, dim white light
107 def handleButton2Pushed() {
108     log.debug "handleButton2Pushed"
109
110     hueLamps.setColor(level: 50, hue: 20, saturation: 80)
111     tvLights.setColor(level: 30, hue: 70, saturation: 70)
112     otherLights?.on()
113 }
114
115 // set to what you want!
116 def handleButton2Held() {
117     log.debug "handleButton2Held"
118 }
119
120 // dim red light
121 def handleButton3Pushed() {
122     hueLamps.setColor(level: 40, hue: 100, saturation: 100)
123     tvLights?.setColor(level: 30, hue: 100, saturation: 100)
124     otherLights?.off()
125 }
126
127 // set to what you want!
128 def handleButton3Held() {
129     log.debug "handleButton3Held"
130 }
131
132 // dim blue light 
133 def handleButton4Pushed() {
134     log.debug "handleButton4Pushed"
135     
136     hueLamps.setColor(level: 10, hue: 70, saturation: 100)
137     tvLights?.setColor(level: 10, hue: 70, saturation: 100)
138     otherLights?.off()
139 }
140
141 // debug information
142 def handleButton4Held() {
143     hueLamps.each {
144         log.debug "lamp ${it.displayName} hue: ${it.currentHue}"
145         log.debug "lamp ${it.displayName} saturation: ${it.currentSaturation}"
146         log.debug "lamp ${it.displayName} level ${it.currentLevel}"
147     }
148
149     log.debug "tv hue: ${tvLights?.currentHue}"
150     log.debug "tv saturation: ${tvLights?.currentSaturation}"
151     log.debug "tv color ${tvLights?.currentColor}"
152     log.debug "tv level ${tvLights?.currentLevel}"
153 }