e2b60549e16d23efce8c723f9d63ca6b8a79f856
[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:"10%"],[20:"20%"],[30:"30%"],[40:"40%"],[50:"50%"],[60:"60%"],[70:"70%"],[80:"80%"],[90:"90%"],[100:"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 }
67
68 def updated() {
69         log.debug "Updated with settings: ${settings}"
70         unsubscribe()
71         unschedule()
72         subscribeToEvents()
73 }
74
75 def subscribeToEvents() {
76         subscribe(app, appTouchHandler)
77         subscribe(contact, "contact.open", eventHandler1)
78         subscribe(contactClosed, "contact.closed", eventHandler1)
79         subscribe(acceleration, "acceleration.active", eventHandler1)
80         subscribe(motion, "motion.active", eventHandler1)
81         subscribe(mySwitch, "switch.on", eventHandler1)
82         subscribe(mySwitchOff, "switch.off", eventHandler1)
83         subscribe(arrivalPresence, "presence.present", eventHandler1)
84         subscribe(departurePresence, "presence.not present", eventHandler1)
85         subscribe(smoke, "smoke.detected", eventHandler1)
86         subscribe(smoke, "smoke.tested", eventHandler1)
87         subscribe(smoke, "carbonMonoxide.detected", eventHandler1)
88         subscribe(water, "water.wet", eventHandler1)
89         subscribe(button1, "button.pushed", eventHandler1)
90
91         if (triggerModes) {
92                 subscribe(location, modeChangeHandler)
93         }
94
95         if (timeOfDay) {
96                 schedule(timeOfDay, scheduledTimeHandler)
97         }
98 }
99
100 def eventHandler1(evt) {
101         if (frequency) {
102                 def lastTime = state[evt.deviceId]
103                 if (lastTime == null || now() - lastTime >= frequency * 60000) {
104                         takeAction(evt)
105                 }
106         }
107         else {
108                 takeAction(evt)
109         }
110 }
111
112 def modeChangeHandler(evt) {
113         log.trace "modeChangeHandler $evt.name: $evt.value ($triggerModes)"
114         if (evt.value in triggerModes) {
115                 eventHandler1(evt)
116         }
117 }
118
119 def scheduledTimeHandler() {
120         eventHandler1(null)
121 }
122
123 def appTouchHandler(evt) {
124         takeAction(evt)
125 }
126
127 private takeAction(evt) {
128
129         if (frequency) {
130                 state[evt.deviceId] = now()
131         }
132
133         def hueColor = 0
134         if(color == "Blue")
135                 hueColor = 70//60
136         else if(color == "Green")
137                 hueColor = 39//30
138         else if(color == "Yellow")
139                 hueColor = 25//16
140         else if(color == "Orange")
141                 hueColor = 10
142         else if(color == "Purple")
143                 hueColor = 75
144         else if(color == "Pink")
145                 hueColor = 83
146
147
148         state.previous = [:]
149
150         hues.each {
151                 state.previous[it.id] = [
152                         "switch": it.currentValue("switch"),
153                         "level" : it.currentValue("level"),
154                         "hue": it.currentValue("hue"),
155                         "saturation": it.currentValue("saturation"),
156                         "color": it.currentValue("color")                       
157                 ]
158         }
159
160         log.debug "current values = $state.previous"
161
162         def newValue = [hue: hueColor, saturation: 100, level: (lightLevel as Integer) ?: 100]
163         log.debug "new value = $newValue"
164
165         hues*.setColor(newValue)
166         setTimer()
167 }
168
169 def setTimer()
170 {
171         if(!duration) //default to 10 seconds
172         {
173                 log.debug "pause 10"
174                 pause(10 * 1000)
175                 log.debug "reset hue"
176                 resetHue()
177         }
178         else if(duration < 10)
179         {
180                 log.debug "pause $duration"
181                 pause(duration * 1000)
182                 log.debug "resetHue"
183                 resetHue()
184         }
185         else
186         {
187                 log.debug "runIn $duration, resetHue"
188                 runIn(duration,"resetHue", [overwrite: false])
189         }
190 }
191
192
193 def resetHue()
194 {
195         hues.each {
196                 it.setColor(state.previous[it.id])        
197         }
198 }