Update groveStreams.groovy
[smartapps.git] / third-party / YouLeftTheDoorOpen.groovy
1 /**
2  *  You left the door open!
3  *
4  *  Copyright 2014 ObyCode
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7  *  in compliance with the License. You may obtain a copy of the License at:
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
12  *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
13  *  for the specific language governing permissions and limitations under the License.
14  *
15  */
16 definition(
17     name: "You left the door open!",
18     namespace: "com.obycode",
19     author: "ObyCode",
20     description: "Choose a contact sensor from your alarm system (AlarmThing) and get a notification when it is left open for too long.",
21     category: "Safety & Security",
22     iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
23     iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png")
24
25
26 preferences {
27     section("Notify me when the door named...") {
28         input "theSensor", "string", multiple: false, required: true
29     }
30     section("On this alarm...") {
31         input "theAlarm", "capability.alarm", multiple: false, required: true
32     }
33     section("Is left open for more than...") {
34         input "maxOpenTime", "number", title: "Minutes?"
35     }
36 }
37
38 def installed() {
39     log.debug "Installed with settings: ${settings}"
40
41     initialize()
42 }
43
44 def updated() {
45     log.debug "Updated with settings: ${settings}"
46
47     unsubscribe()
48     initialize()
49 }
50
51 def initialize() {
52     subscribe(theAlarm, theSensor, sensorTriggered)
53 }
54
55 def sensorTriggered(evt) {
56     if (evt.value == "closed") {
57         clearStatus()
58     }
59     else if (evt.value == "open" && state.status != "scheduled") {
60         runIn(maxOpenTime * 60, takeAction, [overwrite: false])
61         state.status = "scheduled"
62     }
63 }
64
65 def takeAction(){
66     if (state.status == "scheduled")
67     {
68         log.debug "$theSensor was open too long, sending message"
69         def msg = "Your $theSensor has been open for more than $maxOpenTime minutes!"
70         sendPush msg
71         clearStatus()
72     } else {
73         log.trace "Status is no longer scheduled. Not sending text."
74     }
75 }
76
77 def clearStatus() {
78     state.status = null
79 }