Update beacon-control.groovy
[smartapps.git] / official / smart-auto-lock-unlock.groovy
1 /**
2  *  Smart Lock / Unlock
3  *
4  *  Copyright 2014 Arnaud
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: "Smart Auto Lock / Unlock",
18     namespace: "smart-auto-lock-unlock",
19     author: "Arnaud",
20     description: "Automatically locks door X minutes after being closed and keeps door unlocked if door is open.",
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 preferences
26 {
27     section("Select the door lock:") {
28         input "lock1", "capability.lock", required: true
29     }
30     section("Select the door contact sensor:") {
31         input "contact1", "capability.contactSensor", required: true
32     }
33         section("Automatically lock the door when closed...") {
34         input "minutesLater", "number", title: "Delay (in minutes):", required: true
35     }
36     section("Automatically unlock the door when open...") {
37         input "secondsLater", "number", title: "Delay (in seconds):", required: true
38     }
39         section( "Push notification?" ) {
40                 input "sendPushMessage", "enum", title: "Send push notification?", metadata:[values:["Yes", "No"]], required: false
41         }
42     section( "Text message?" ) {
43         input "sendText", "enum", title: "Send text message notification?", metadata:[values:["Yes", "No"]], required: false
44         input "phoneNumber", "phone", title: "Enter phone number:", required: false
45         }
46 }
47
48 def installed()
49 {
50     initialize()
51 }
52
53 def updated()
54 {
55     unsubscribe()
56     unschedule()
57     initialize()
58 }
59
60 def initialize()
61 {
62     log.debug "Settings: ${settings}"
63     subscribe(lock1, "lock", doorHandler, [filterEvents: false])
64     subscribe(lock1, "unlock", doorHandler, [filterEvents: false])  
65     subscribe(contact1, "contact.open", doorHandler)
66         subscribe(contact1, "contact.closed", doorHandler)
67 }
68
69 def lockDoor()
70 {
71         if (lock1.latestValue("lock") == "unlocked")
72         {
73         log.debug "Locking $lock1..."
74         lock1.lock()
75         log.debug ("Sending Push Notification...") 
76         if (sendPushMessage != "No") sendPush("$lock1 locked after $contact1 was closed for $minutesLater minute(s)!")
77         log.debug("Sending text message...")
78                 if ((sendText == "Yes") && (phoneNumber != "0")) sendSms(phoneNumber, "$lock1 locked after $contact1 was closed for $minutesLater minute(s)!")
79         }
80         else if (lock1.latestValue("lock") == "locked")
81         {
82         log.debug "$lock1 was already locked..."
83         }
84 }
85
86 def unlockDoor()
87 {
88         if (lock1.latestValue("lock") == "locked")
89         {
90         log.debug "Unlocking $lock1..."
91         lock1.unlock()
92         log.debug ("Sending Push Notification...") 
93         if (sendPushMessage != "No") sendPush("$lock1 unlocked after $contact1 was open for $secondsLater seconds(s)!")
94         log.debug("Sending text message...")
95                 if ((sendText == "Yes") && (phoneNumber != "0")) sendSms(phoneNumber, "$lock1 unlocked after $contact1 was open for $secondsLater seconds(s)!")        
96         }
97         else if (lock1.latestValue("lock") == "unlocked")
98         {
99         log.debug "$lock1 was already unlocked..."
100         }
101 }
102
103 def doorHandler(evt)
104 {
105     if ((contact1.latestValue("contact") == "open") && (evt.value == "locked"))
106         {
107         def delay = secondsLater
108         runIn (delay, unlockDoor)
109         }
110     else if ((contact1.latestValue("contact") == "open") && (evt.value == "unlocked"))
111         {
112         unschedule (unlockDoor)
113                 }
114     else if ((contact1.latestValue("contact") == "closed") && (evt.value == "locked"))
115         {
116         unschedule (lockDoor)
117         }   
118     else if ((contact1.latestValue("contact") == "closed") && (evt.value == "unlocked"))
119         {
120         log.debug "Unlocking $lock1..."
121         lock1.unlock()
122         def delay = (minutesLater * 60)
123         runIn (delay, lockDoor)
124         }
125     else if ((lock1.latestValue("lock") == "unlocked") && (evt.value == "open"))
126         {
127         unschedule (lockDoor)
128         log.debug "Unlocking $lock1..."
129         lock1.unlock()
130         }
131     else if ((lock1.latestValue("lock") == "unlocked") && (evt.value == "closed"))
132         {
133         log.debug "Unlocking $lock1..."
134         lock1.unlock()
135         def delay = (minutesLater * 60)
136         runIn (delay, lockDoor)
137         }
138         else if ((lock1.latestValue("lock") == "locked") && (evt.value == "open"))
139         {
140         unschedule (lockDoor)
141         log.debug "Unlocking $lock1..."
142         lock1.unlock()
143         }
144     else if ((lock1.latestValue("lock") == "locked") && (evt.value == "closed"))
145         {
146         unschedule (lockDoor)
147         log.debug "Unlocking $lock1..."
148         lock1.unlock()
149         }
150     else
151         {
152         log.debug "Problem with $lock1, the lock might be jammed!"
153         unschedule (lockDoor)
154         unschedule (unlockDoor)
155         }
156 }