Changing remote branch to PLRG Git server.
[smartapps.git] / official / notify-me-with-hue.groovy
1 /**
2  *  Copyright 2015 SmartThings
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  *  in compliance with the License. You may obtain a copy of the License at:
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
10  *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
11  *  for the specific language governing permissions and limitations under the License.
12  *
13  *  Notify Me With Hue
14  *
15  *  Author: SmartThings
16  *  Date: 2014-01-20
17  */
18 definition(
19     name: "Notify Me With Hue",
20     namespace: "smartthings",
21     author: "SmartThings",
22     description: "Changes the color and brightness of Philips Hue bulbs when any of a variety of SmartThings is activated.  Supports motion, contact, acceleration, moisture and presence sensors as well as switches.",
23     category: "SmartThings Labs",
24     iconUrl: "https://s3.amazonaws.com/smartapp-icons/Partner/hue.png",
25     iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Partner/hue@2x.png"
26 )
27
28 preferences {
29
30         section("Control these bulbs...") {
31                 input "hues", "capability.colorControl", title: "Which Hue Bulbs?", required:true, multiple:true
32         }
33
34         section("Choose one or more, when..."){
35                 input "motion", "capability.motionSensor", title: "Motion Here", required: false, multiple: true
36                 input "contact", "capability.contactSensor", title: "Contact Opens", required: false, multiple: true
37                 input "contactClosed", "capability.contactSensor", title: "Contact Closes", required: false, multiple: true
38                 input "acceleration", "capability.accelerationSensor", title: "Acceleration Detected", required: false, multiple: true
39                 input "mySwitch", "capability.switch", title: "Switch Turned On", required: false, multiple: true
40                 input "mySwitchOff", "capability.switch", title: "Switch Turned Off", required: false, multiple: true
41                 input "arrivalPresence", "capability.presenceSensor", title: "Arrival Of", required: false, multiple: true
42                 input "departurePresence", "capability.presenceSensor", title: "Departure Of", required: false, multiple: true
43                 input "smoke", "capability.smokeDetector", title: "Smoke Detected", required: false, multiple: true
44                 input "water", "capability.waterSensor", title: "Water Sensor Wet", required: false, multiple: true
45                 input "button1", "capability.button", title: "Button Press", required:false, multiple:true //remove from production
46                 input "triggerModes", "mode", title: "System Changes Mode", description: "Select mode(s)", required: false, multiple: true
47                 input "timeOfDay", "time", title: "At a Scheduled Time", required: false
48         }
49
50         section("Choose light effects...")
51                 {
52                         input "color", "enum", title: "Hue Color?", required: false, multiple:false, options: ["Red","Green","Blue","Yellow","Orange","Purple","Pink"]
53                         input "lightLevel", "enum", title: "Light Level?", required: false, options: [10,20,30,40,50,60,70,80,90,100]
54                         input "duration", "number", title: "Duration Seconds?", required: false
55                         //input "turnOn", "enum", title: "Turn On when Off?", required: false, options: ["Yes","No"]
56                 }
57
58         section("Minimum time between messages (optional, defaults to every message)") {
59                 input "frequency", "decimal", title: "Minutes", required: false
60         }
61 }
62
63 def installed() {
64         log.debug "Installed with settings: ${settings}"
65         subscribeToEvents()
66         // Initialize input value
67         color = "Pink"
68 }
69
70 def updated() {
71         log.debug "Updated with settings: ${settings}"
72         unsubscribe()
73         unschedule()
74         subscribeToEvents()
75 }
76
77 def subscribeToEvents() {
78         subscribe(app, appTouchHandler)
79         subscribe(contact, "contact.open", eventHandler1)
80         subscribe(contactClosed, "contact.closed", eventHandler1)
81         subscribe(acceleration, "acceleration.active", eventHandler1)
82         subscribe(motion, "motion.active", eventHandler1)
83         subscribe(mySwitch, "switch.on", eventHandler1)
84         subscribe(mySwitchOff, "switch.off", eventHandler1)
85         subscribe(arrivalPresence, "presence.present", eventHandler1)
86         subscribe(departurePresence, "presence.not present", eventHandler1)
87         subscribe(smoke, "smoke.detected", eventHandler1)
88         subscribe(smoke, "smoke.tested", eventHandler1)
89         subscribe(smoke, "carbonMonoxide.detected", eventHandler1)
90         subscribe(water, "water.wet", eventHandler1)
91         subscribe(button1, "button.pushed", eventHandler1)
92
93         if (triggerModes) {
94                 subscribe(location, modeChangeHandler)
95         }
96
97         if (timeOfDay) {
98                 schedule(timeOfDay, scheduledTimeHandler)
99         }
100 }
101
102 def eventHandler1(evt) {
103         if (frequency) {
104                 def lastTime = state[evt.deviceId]
105                 if (lastTime == null || now() - lastTime >= frequency * 60000) {
106                         takeAction(evt)
107                 }
108         }
109         else {
110                 takeAction(evt)
111         }
112 }
113
114 def modeChangeHandler(evt) {
115         log.trace "modeChangeHandler $evt.name: $evt.value ($triggerModes)"
116         if (evt.value in triggerModes) {
117                 eventHandler1(evt)
118         }
119 }
120
121 def scheduledTimeHandler() {
122         //eventHandler1(null)
123 }
124
125 def appTouchHandler(evt) {
126         takeAction(evt)
127 }
128
129 private takeAction(evt) {
130
131         if (frequency) {
132                 state[evt.deviceId] = now()
133         }
134
135         def hueColor = 0
136         if(color == "Blue")
137                 hueColor = 70//60
138         else if(color == "Green")
139                 hueColor = 39//30
140         else if(color == "Yellow")
141                 hueColor = 25//16
142         else if(color == "Orange")
143                 hueColor = 10
144         else if(color == "Purple")
145                 hueColor = 75
146         else if(color == "Pink")
147                 hueColor = 83
148
149
150         state.previous = [:]
151
152         hues.each {
153                 state.previous[it.id] = [
154                         "switch": it.currentValue("switch"),
155                         "level" : it.currentValue("level"),
156                         "hue": it.currentValue("hue"),
157                         "saturation": it.currentValue("saturation"),
158                         "color": it.currentValue("color")                       
159                 ]
160         }
161
162         log.debug "current values = $state.previous"
163
164         def newValue = [hue: hueColor, saturation: 100, level: (lightLevel as Integer) ?: 100]
165         log.debug "new value = $newValue"
166
167         hues*.setColor(newValue)
168         setTimer()
169 }
170
171 def setTimer()
172 {
173         if(!duration) //default to 10 seconds
174         {
175                 log.debug "pause 10"
176                 pause(10 * 1000)
177                 log.debug "reset hue"
178                 resetHue()
179         }
180         else if(duration < 10)
181         {
182                 log.debug "pause $duration"
183                 pause(duration * 1000)
184                 log.debug "resetHue"
185                 resetHue()
186         }
187         else
188         {
189                 log.debug "runIn $duration, resetHue"
190                 runIn(duration,"resetHue", [overwrite: false])
191         }
192 }
193
194
195 def resetHue()
196 {
197         hues.each {
198                 it.setColor(state.previous[it.id])        
199         }
200 }