Update vacation-lighting-director.groovy
[smartapps.git] / official / ridiculously-automated-garage-door.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  *  Ridiculously Automated Garage Door
14  *
15  *  Author: SmartThings
16  *  Date: 2013-03-10
17  *
18  * Monitors arrival and departure of car(s) and
19  *
20  *    1) opens door when car arrives,
21  *    2) closes door after car has departed (for N minutes),
22  *    3) opens door when car door motion is detected,
23  *    4) closes door when door was opened due to arrival and interior door is closed.
24  */
25
26 definition(
27     name: "Ridiculously Automated Garage Door",
28     namespace: "smartthings",
29     author: "SmartThings",
30     description: "Monitors arrival and departure of car(s) and 1) opens door when car arrives, 2) closes door after car has departed (for N minutes), 3) opens door when car door motion is detected, 4) closes door when door was opened due to arrival and interior door is closed.",
31     category: "Convenience",
32     iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/garage_contact.png",
33     iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/garage_contact@2x.png"
34 )
35
36 preferences {
37
38         section("Garage door") {
39                 input "doorSensor", "capability.contactSensor", title: "Which sensor?"
40                 input "doorSwitch", "capability.momentary", title: "Which switch?"
41                 input "openThreshold", "number", title: "Warn when open longer than (optional)",description: "Number of minutes", required: false
42         input("recipients", "contact", title: "Send notifications to") {
43             input "phone", "phone", title: "Warn with text message (optional)", description: "Phone Number", required: false
44         }
45         }
46         section("Car(s) using this garage door") {
47                 input "cars", "capability.presenceSensor", title: "Presence sensor", description: "Which car(s)?", multiple: true, required: false
48                 input "carDoorSensors", "capability.accelerationSensor", title: "Car door sensor(s)", description: "Which car(s)?", multiple: true, required: false
49         }
50         section("Interior door (optional)") {
51                 input "interiorDoorSensor", "capability.contactSensor", title: "Contact sensor?", required: false
52         }
53         section("False alarm threshold (defaults to 10 min)") {
54                 input "falseAlarmThreshold", "number", title: "Number of minutes", required: false
55         }
56 }
57
58 def installed() {
59         log.trace "installed()"
60         subscribe()
61 }
62
63 def updated() {
64         log.trace "updated()"
65         unsubscribe()
66         subscribe()
67 }
68
69 def subscribe() {
70         // log.debug "present: ${cars.collect{it.displayName + ': ' + it.currentPresence}}"
71         subscribe(doorSensor, "contact", garageDoorContact)
72
73         subscribe(cars, "presence", carPresence)
74         subscribe(carDoorSensors, "acceleration", accelerationActive)
75
76         if (interiorDoorSensor) {
77                 subscribe(interiorDoorSensor, "contact.closed", interiorDoorClosed)
78         }
79 }
80
81 def doorOpenCheck()
82 {
83         final thresholdMinutes = openThreshold
84         if (thresholdMinutes) {
85                 def currentState = doorSensor.contactState
86                 log.debug "doorOpenCheck"
87                 if (currentState?.value == "open") {
88                         log.debug "open for ${now() - currentState.date.time}, openDoorNotificationSent: ${state.openDoorNotificationSent}"
89                         if (!state.openDoorNotificationSent && now() - currentState.date.time > thresholdMinutes * 60 *1000) {
90                                 def msg = "${doorSwitch.displayName} was been open for ${thresholdMinutes} minutes"
91                                 log.info msg
92
93                 if (location.contactBookEnabled) {
94                     sendNotificationToContacts(msg, recipients)
95                 }
96                 else {
97                     sendPush msg
98                     if (phone) {
99                         sendSms phone, msg
100                     }
101                 }
102                                 state.openDoorNotificationSent = true
103                         }
104                 }
105                 else {
106                         state.openDoorNotificationSent = false
107                 }
108         }
109 }
110
111 def carPresence(evt)
112 {
113         log.info "$evt.name: $evt.value"
114         // time in which there must be no "not present" events in order to open the door
115         final openDoorAwayInterval = falseAlarmThreshold ? falseAlarmThreshold * 60 : 600
116
117         if (evt.value == "present") {
118                 // A car comes home
119
120                 def car = getCar(evt)
121                 def t0 = new Date(now() - (openDoorAwayInterval * 1000))
122                 def states = car.statesSince("presence", t0)
123                 def recentNotPresentState = states.find{it.value == "not present"}
124
125                 if (recentNotPresentState) {
126                         log.debug "Not opening ${doorSwitch.displayName} since car was not present at ${recentNotPresentState.date}, less than ${openDoorAwayInterval} sec ago"
127                 }
128                 else {
129                         if (doorSensor.currentContact == "closed") {
130                                 openDoor()
131                 sendPush "Opening garage door due to arrival of ${car.displayName}"
132                 state.appOpenedDoor = now()
133                         }
134                         else {
135                                 log.debug "door already open"
136                         }
137                 }
138         }
139         else {
140                 // A car departs
141                 if (doorSensor.currentContact == "open") {
142                         closeDoor()
143                         log.debug "Closing ${doorSwitch.displayName} after departure"
144             sendPush("Closing ${doorSwitch.displayName} after departure")
145
146                 }
147                 else {
148                         log.debug "Not closing ${doorSwitch.displayName} because its already closed"
149                 }
150         }
151 }
152
153 def garageDoorContact(evt)
154 {
155         log.info "garageDoorContact, $evt.name: $evt.value"
156         if (evt.value == "open") {
157                 schedule("0 * * * * ?", "doorOpenCheck")
158         }
159         else {
160                 unschedule("doorOpenCheck")
161         }
162 }
163
164
165 def interiorDoorClosed(evt)
166 {
167         log.info "interiorContact, $evt.name: $evt.value"
168
169         // time during which closing the interior door will shut the garage door, if the app opened it
170         final threshold = 15 * 60 * 1000
171         if (state.appOpenedDoor && now() - state.appOpenedDoor < threshold) {
172                 state.appOpenedDoor = 0
173                 closeDoor()
174         }
175         else {
176                 log.debug "app didn't open door"
177         }
178 }
179
180 def accelerationActive(evt)
181 {
182         log.info "$evt.name: $evt.value"
183
184         if (doorSensor.currentContact == "closed") {
185                 log.debug "opening door when car door opened"
186                 openDoor()
187         }
188 }
189
190 private openDoor()
191 {
192         if (doorSensor.currentContact == "closed") {
193                 log.debug "opening door"
194                 doorSwitch.push()
195         }
196 }
197
198 private closeDoor()
199 {
200         if (doorSensor.currentContact == "open") {
201                 log.debug "closing door"
202                 doorSwitch.push()
203         }
204 }
205
206 private getCar(evt)
207 {
208         cars.find{it.id == evt.deviceId}
209 }