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