Update single-button-controller.groovy
[smartapps.git] / official / enhanced-auto-lock-door.groovy
1 definition(
2     name: "Enhanced Auto Lock Door",
3     namespace: "Lock Auto Super Enhanced",
4     author: "Arnaud",
5     description: "Automatically locks a specific door after X minutes when closed  and unlocks it when open after X seconds.",
6     category: "Safety & Security",
7     iconUrl: "http://www.gharexpert.com/mid/4142010105208.jpg",
8     iconX2Url: "http://www.gharexpert.com/mid/4142010105208.jpg"
9 )
10
11 preferences{
12     section("Select the door lock:") {
13         input "lock1", "capability.lock", required: true
14     }
15     section("Select the door contact sensor:") {
16         input "contact", "capability.contactSensor", required: true
17     }   
18     section("Automatically lock the door when closed...") {
19         input "minutesLater", "number", title: "Delay (in minutes):", required: true
20     }
21     section("Automatically unlock the door when open...") {
22         input "secondsLater", "number", title: "Delay (in seconds):", required: true
23     }
24     section( "Notifications" ) {
25         input("recipients", "contact", title: "Send notifications to", required: false) {
26             input "phoneNumber", "phone", title: "Warn with text message (optional)", description: "Phone Number", required: false
27         }
28     }
29 }
30
31 def installed(){
32     initialize()
33 }
34
35 def updated(){
36     unsubscribe()
37     unschedule()
38     initialize()
39 }
40
41 def initialize(){
42     log.debug "Settings: ${settings}"
43     subscribe(lock1, "lock", doorHandler, [filterEvents: false])
44     subscribe(lock1, "unlock", doorHandler, [filterEvents: false])  
45     subscribe(contact, "contact.open", doorHandler)
46     subscribe(contact, "contact.closed", doorHandler)
47 }
48
49 def lockDoor(){
50     log.debug "Locking the door."
51     lock1.lock()
52     if(location.contactBookEnabled) {
53         if ( recipients ) {
54             log.debug ( "Sending Push Notification..." ) 
55             sendNotificationToContacts( "${lock1} locked after ${contact} was closed for ${minutesLater} minutes!", recipients)
56         }
57     }
58     if (phoneNumber) {
59         log.debug("Sending text message...")
60         sendSms( phoneNumber, "${lock1} locked after ${contact} was closed for ${minutesLater} minutes!")
61     }
62 }
63
64 def unlockDoor(){
65     log.debug "Unlocking the door."
66     lock1.unlock()
67     if(location.contactBookEnabled) {
68         if ( recipients ) {
69             log.debug ( "Sending Push Notification..." ) 
70             sendNotificationToContacts( "${lock1} unlocked after ${contact} was opened for ${secondsLater} seconds!", recipients)
71         }
72     }
73     if ( phoneNumber ) {
74         log.debug("Sending text message...")
75         sendSms( phoneNumber, "${lock1} unlocked after ${contact} was opened for ${secondsLater} seconds!")
76     }
77 }
78
79 def doorHandler(evt){
80     if ((contact.latestValue("contact") == "open") && (evt.value == "locked")) { // If the door is open and a person locks the door then...  
81         //def delay = (secondsLater) // runIn uses seconds
82         runIn( secondsLater, unlockDoor )   // ...schedule (in minutes) to unlock...  We don't want the door to be closed while the lock is engaged. 
83     }
84     else if ((contact.latestValue("contact") == "open") && (evt.value == "unlocked")) { // If the door is open and a person unlocks it then...
85         unschedule( unlockDoor ) // ...we don't need to unlock it later.
86     }
87     else if ((contact.latestValue("contact") == "closed") && (evt.value == "locked")) { // If the door is closed and a person manually locks it then...
88         unschedule( lockDoor ) // ...we don't need to lock it later.
89     }   
90     else if ((contact.latestValue("contact") == "closed") && (evt.value == "unlocked")) { // If the door is closed and a person unlocks it then...
91        //def delay = (minutesLater * 60) // runIn uses seconds
92         runIn( (minutesLater * 60), lockDoor ) // ...schedule (in minutes) to lock.
93     }
94     else if ((lock1.latestValue("lock") == "unlocked") && (evt.value == "open")) { // If a person opens an unlocked door...
95         unschedule( lockDoor ) // ...we don't need to lock it later.
96     }
97     else if ((lock1.latestValue("lock") == "unlocked") && (evt.value == "closed")) { // If a person closes an unlocked door...
98         //def delay = (minutesLater * 60) // runIn uses seconds
99         runIn( (minutesLater * 60), lockDoor ) // ...schedule (in minutes) to lock.
100     }
101     else { //Opening or Closing door when locked (in case you have a handle lock)
102         log.debug "Unlocking the door."
103         lock1.unlock()
104         if(location.contactBookEnabled) {
105             if ( recipients ) {
106                 log.debug ( "Sending Push Notification..." ) 
107                 sendNotificationToContacts( "${lock1} unlocked after ${contact} was opened or closed when ${lock1} was locked!", recipients)
108             }
109         }
110         if ( phoneNumber ) {
111             log.debug("Sending text message...")
112             sendSms( phoneNumber, "${lock1} unlocked after ${contact} was opened or closed when ${lock1} was locked!")
113         }
114     }
115 }