Update step-notifier.groovy
[smartapps.git] / third-party / auto-lock-door.smartapp.groovy
1 /**
2  *  Auto Lock Door
3  *
4  *  Author: Chris Sader (@csader)
5  *  Collaborators: @chrisb
6  *  Date: 2013-08-21
7  *  URL: http://www.github.com/smartthings-users/smartapp.auto-lock-door
8  *
9  * Copyright (C) 2013 Chris Sader.
10  * Permission is hereby granted, free of charge, to any person obtaining a copy of this
11  * software and associated documentation files (the "Software"), to deal in the Software
12  * without restriction, including without limitation the rights to use, copy, modify,
13  * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to the following
15  * conditions: The above copyright notice and this permission notice shall be included
16  * in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
19  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
20  * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
22  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
23  * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25
26 preferences
27 {
28     section("When a door unlocks...") {
29         input "lock1", "capability.lock"
30     }
31     section("Lock it how many minutes later?") {
32         input "minutesLater", "number", title: "When?"
33     }
34     section("Lock it only when this door is closed") {
35         input "openSensor", "capability.contactSensor", title: "Where?"
36     }
37 }
38
39 def installed()
40 {
41     log.debug "Auto Lock Door installed. (URL: http://www.github.com/smartthings-users/smartapp.auto-lock-door)"
42     initialize()
43 }
44
45 def updated()
46 {
47     unsubscribe()
48     unschedule()
49     log.debug "Auto Lock Door updated."
50     initialize()
51 }
52
53 def initialize()
54 {
55     log.debug "Settings: ${settings}"
56     subscribe(lock1, "lock", doorHandler)
57     subscribe(openSensor, "contact.closed", doorClosed)
58     subscribe(openSensor, "contact.open", doorOpen)
59 }
60
61 def lockDoor()
62 {
63     log.debug "Locking Door if Closed"
64     if((openSensor.latestValue("contact") == "closed")){
65         log.debug "Door Closed"
66         lock1.lock()
67     } else {
68         if ((openSensor.latestValue("contact") == "open")) {
69         def delay = minutesLater * 60
70         log.debug "Door open will try again in $minutesLater minutes"
71         runIn( delay, lockDoor )
72         }
73     }
74 }
75
76 def doorOpen(evt) {
77     log.debug "Door open reset previous lock task..."
78     unschedule( lockDoor )
79     def delay = minutesLater * 60
80     runIn( delay, lockDoor )
81 }
82
83 def doorClosed(evt) {
84     log.debug "Door Closed"
85 }
86
87 def doorHandler(evt)
88 {
89     log.debug "Door ${openSensor.latestValue}"
90     log.debug "Lock ${evt.name} is ${evt.value}."
91
92     if (evt.value == "locked") {                  // If the human locks the door then...
93         log.debug "Cancelling previous lock task..."
94         unschedule( lockDoor )                  // ...we don't need to lock it later.
95     }
96     else {                                      // If the door is unlocked then...
97         def delay = minutesLater * 60          // runIn uses seconds
98         log.debug "Re-arming lock in ${minutesLater} minutes (${delay}s)."
99         runIn( delay, lockDoor )                // ...schedule to lock in x minutes.
100     }
101 }