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