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