Merge branch 'master' of ssh://plrg.eecs.uci.edu/home/git/smartthings-infrastructure
[smartthings-infrastructure.git] / Extractor / App1 / App1.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
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     if ((contact.latestValue("contact") == "open") && (evt.value == "locked")) { // If the door is open and a person locks the door then...  
82         //def delay = (secondsLater) // runIn uses seconds
83         runIn( secondsLater, unlockDoor )   // ...schedule (in minutes) to unlock...  We don't want the door to be closed while the lock is engaged. 
84     }
85     else if ((contact.latestValue("contact") == "open") && (evt.value == "unlocked")) { // If the door is open and a person unlocks it then...
86         unschedule( unlockDoor ) // ...we don't need to unlock it later.
87     }
88     else if ((contact.latestValue("contact") == "closed") && (evt.value == "locked")) { // If the door is closed and a person manually locks it then...
89         unschedule( lockDoor ) // ...we don't need to lock it later.
90     }   
91     else if ((contact.latestValue("contact") == "closed") && (evt.value == "unlocked")) { // If the door is closed and a person unlocks it then...
92        //def delay = (minutesLater * 60) // runIn uses seconds
93         runIn( (minutesLater * 60), lockDoor ) // ...schedule (in minutes) to lock.
94     }
95     else if ((lock1.latestValue("lock") == "unlocked") && (evt.value == "open")) { // If a person opens an unlocked door...
96         unschedule( lockDoor ) // ...we don't need to lock it later.
97     }
98     else if ((lock1.latestValue("lock") == "unlocked") && (evt.value == "closed")) { // If a person closes an unlocked door...
99         //def delay = (minutesLater * 60) // runIn uses seconds
100         runIn( (minutesLater * 60), lockDoor ) // ...schedule (in minutes) to lock.
101     }
102     else { //Opening or Closing door when locked (in case you have a handle lock)
103         log.debug "Unlocking the door."
104         lock1.unlock()
105         if(location.contactBookEnabled) {
106             if ( recipients ) {
107                 log.debug ( "Sending Push Notification..." ) 
108                 sendNotificationToContacts( "${lock1} unlocked after ${contact} was opened or closed when ${lock1} was locked!", recipients)
109             }
110         }
111         if ( phoneNumber ) {
112             log.debug("Sending text message...")
113             sendSms( phoneNumber, "${lock1} unlocked after ${contact} was opened or closed when ${lock1} was locked!")
114         }
115     }
116 }