Update thermostat.groovy
[smartapps.git] / official / cameras-on-when-im-away.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  *  Cameras On When I'm Away
14  *
15  *  Author: danny@smartthings.com
16  *  Date: 2013-10-07
17  */
18
19 definition(
20     name: "Cameras On When I'm Away",
21     namespace: "smartthings",
22     author: "SmartThings",
23     description: "Turn cameras on when I'm away",
24     category: "Available Beta Apps",
25     iconUrl: "https://s3.amazonaws.com/smartapp-icons/Partner/dropcam-on-off-presence.png",
26     iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Partner/dropcam-on-off-presence@2x.png"
27 )
28
29 preferences {
30         section("When all of these people are home...") {
31                 input "people", "capability.presenceSensor", multiple: true
32         }
33         section("Turn off camera power..."){
34                 input "switches1", "capability.switch", multiple: true
35         }
36 }
37
38 def installed() {
39         log.debug "Installed with settings: ${settings}"
40         log.debug "Current people = ${people.collect{it.label + ': ' + it.currentPresence}}"
41         subscribe(people, "presence", presence)
42 }
43
44 def updated() {
45         log.debug "Updated with settings: ${settings}"
46         log.debug "Current people = ${people.collect{it.label + ': ' + it.currentPresence}}"
47         unsubscribe()
48         subscribe(people, "presence", presence)
49 }
50
51 def presence(evt)
52 {
53         log.debug "evt.name: $evt.value"
54         if (evt.value == "not present") {
55                 
56         log.debug "checking if everyone is away"
57         if (everyoneIsAway()) {
58             log.debug "starting on Sequence"
59
60             runIn(60*2, "turnOn") //two minute delay after everyone has left
61         }
62         }
63         else {
64         if (!everyoneIsAway()) {
65           turnOff()
66         }
67         }
68 }
69
70 def turnOff()
71 {
72     log.debug "canceling On requests"
73     unschedule("turnOn")
74     
75     log.info "turning off the camera"
76     switches1.off()
77 }
78
79 def turnOn()
80 {
81
82         log.info "turned on the camera"
83     switches1.on()
84
85         unschedule("turnOn") // Temporary work-around to scheduling bug
86 }
87
88 private everyoneIsAway()
89 {
90         def result = true
91         for (person in people) {
92                 if (person.currentPresence == "present") {
93                         result = false
94                         break
95                 }
96         }
97         log.debug "everyoneIsAway: $result"
98         return result
99 }
100
101