Checking in all the SmartThings apps; both official and third-party.
[smartapps.git] / official / left-it-open.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  *  Left It Open
14  *
15  *  Author: SmartThings
16  *  Date: 2013-05-09
17  */
18 definition(
19     name: "Left It Open",
20     namespace: "smartthings",
21     author: "SmartThings",
22     description: "Notifies you when you have left a door or window open longer that a specified amount of time.",
23     category: "Convenience",
24     iconUrl: "https://s3.amazonaws.com/smartapp-icons/ModeMagic/bon-voyage.png",
25     iconX2Url: "https://s3.amazonaws.com/smartapp-icons/ModeMagic/bon-voyage%402x.png"
26 )
27
28 preferences {
29
30         section("Monitor this door or window") {
31                 input "contact", "capability.contactSensor"
32         }
33         section("And notify me if it's open for more than this many minutes (default 10)") {
34                 input "openThreshold", "number", description: "Number of minutes", required: false
35         }
36     section("Delay between notifications (default 10 minutes") {
37         input "frequency", "number", title: "Number of minutes", description: "", required: false
38     }
39         section("Via text message at this number (or via push notification if not specified") {
40         input("recipients", "contact", title: "Send notifications to") {
41             input "phone", "phone", title: "Phone number (optional)", required: false
42         }
43         }
44 }
45
46 def installed() {
47         log.trace "installed()"
48         subscribe()
49 }
50
51 def updated() {
52         log.trace "updated()"
53         unsubscribe()
54         subscribe()
55 }
56
57 def subscribe() {
58         subscribe(contact, "contact.open", doorOpen)
59         subscribe(contact, "contact.closed", doorClosed)
60 }
61
62 def doorOpen(evt)
63 {
64         log.trace "doorOpen($evt.name: $evt.value)"
65         def t0 = now()
66         def delay = (openThreshold != null && openThreshold != "") ? openThreshold * 60 : 600
67         runIn(delay, doorOpenTooLong, [overwrite: false])
68         log.debug "scheduled doorOpenTooLong in ${now() - t0} msec"
69 }
70
71 def doorClosed(evt)
72 {
73         log.trace "doorClosed($evt.name: $evt.value)"
74 }
75
76 def doorOpenTooLong() {
77         def contactState = contact.currentState("contact")
78     def freq = (frequency != null && frequency != "") ? frequency * 60 : 600
79
80         if (contactState.value == "open") {
81                 def elapsed = now() - contactState.rawDateCreated.time
82                 def threshold = ((openThreshold != null && openThreshold != "") ? openThreshold * 60000 : 60000) - 1000
83                 if (elapsed >= threshold) {
84                         log.debug "Contact has stayed open long enough since last check ($elapsed ms):  calling sendMessage()"
85                         sendMessage()
86             runIn(freq, doorOpenTooLong, [overwrite: false])
87                 } else {
88                         log.debug "Contact has not stayed open long enough since last check ($elapsed ms):  doing nothing"
89                 }
90         } else {
91                 log.warn "doorOpenTooLong() called but contact is closed:  doing nothing"
92         }
93 }
94
95 void sendMessage()
96 {
97         def minutes = (openThreshold != null && openThreshold != "") ? openThreshold : 10
98         def msg = "${contact.displayName} has been left open for ${minutes} minutes."
99         log.info msg
100     if (location.contactBookEnabled) {
101         sendNotificationToContacts(msg, recipients)
102     }
103     else {
104         if (phone) {
105             sendSms phone, msg
106         } else {
107             sendPush msg
108         }
109     }
110 }