Adding new events in the script.
[smartthings-infrastructure.git] / Extractor / App1 / App1.groovy
index 0d4c97e23b7b623f0b2f8fd0f4e82e5a8fd2d23f..94ec37c73a6ba5e5fbec131bfd6c9a90f29bedae 100644 (file)
+definition(
+    name: "Enhanced Auto Lock Door",
+    namespace: "Lock Auto Super Enhanced",
+    author: "Arnaud",
+    description: "Automatically locks a specific door after X minutes when closed  and unlocks it when open after X seconds.",
+    category: "Safety & Security",
+    iconUrl: "http://www.gharexpert.com/mid/4142010105208.jpg",
+    iconX2Url: "http://www.gharexpert.com/mid/4142010105208.jpg"
+)
 
-/**
- *  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"
+preferences{
+    section("Select the door lock:") {
+        input "lock1", "capability.lock", required: true
+    }
+    section("Select the door contact sensor:") {
+        input "contact", "capability.contactSensor", required: true
+    }   
+    section("Automatically lock the door when closed...") {
+        input "minutesLater", "number", title: "Delay (in minutes):", required: true
     }
-    section("Lock it how many minutes later?") {
-        input "minutesLater", "number", title: "When?"
+    section("Automatically unlock the door when open...") {
+        input "secondsLater", "number", title: "Delay (in seconds):", required: true
     }
-    section("Lock it only when this door is closed") {
-        input "openSensor", "capability.contactSensor", title: "Where?"
+    section( "Notifications" ) {
+        input("recipients", "contact", title: "Send notifications to", required: false) {
+            input "phoneNumber", "phone", title: "Warn with text message (optional)", description: "Phone Number", required: false
+        }
     }
 }
 
-def installed()
-{
-    log.debug "Auto Lock Door installed. (URL: http://www.github.com/smartthings-users/smartapp.auto-lock-door)"
+def installed(){
     initialize()
 }
 
-def updated()
-{
+def updated(){
     unsubscribe()
     unschedule()
-    log.debug "Auto Lock Door updated."
     initialize()
 }
 
-def initialize()
-{
+def initialize(){
     log.debug "Settings: ${settings}"
-    subscribe(lock1, "lock", doorHandler)
-    subscribe(openSensor, "contact.closed", doorClosed)
-    subscribe(openSensor, "contact.open", doorOpen)
+    subscribe(lock1, "lock", doorHandler, [filterEvents: false])
+    subscribe(lock1, "unlock", doorHandler, [filterEvents: false])  
+    subscribe(contact, "contact.open", doorHandler)
+    subscribe(contact, "contact.closed", doorHandler)
 }
 
-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 lockDoor(){
+    log.debug "Locking the door."
+    lock1.lock()
+    if(location.contactBookEnabled) {
+        if ( recipients ) {
+            log.debug ( "Sending Push Notification..." ) 
+            sendNotificationToContacts( "${lock1} locked after ${contact} was closed for ${minutesLater} minutes!", recipients)
         }
     }
+    if (phoneNumber) {
+        log.debug("Sending text message...")
+        sendSms( phoneNumber, "${lock1} locked after ${contact} was closed for ${minutesLater} minutes!")
+    }
 }
 
-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 unlockDoor(){
+    log.debug "Unlocking the door."
+    lock1.unlock()
+    if(location.contactBookEnabled) {
+        if ( recipients ) {
+            log.debug ( "Sending Push Notification..." ) 
+            sendNotificationToContacts( "${lock1} unlocked after ${contact} was opened for ${secondsLater} seconds!", recipients)
+        }
+    }
+    if ( phoneNumber ) {
+        log.debug("Sending text message...")
+        sendSms( phoneNumber, "${lock1} unlocked after ${contact} was opened for ${secondsLater} seconds!")
+    }
 }
 
-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.
+def doorHandler(evt){
+    if ((contact.latestValue("contact") == "open") && (evt.value == "locked")) { // If the door is open and a person locks the door then...  
+        //def delay = (secondsLater) // runIn uses seconds
+        runIn( secondsLater, unlockDoor )   // ...schedule (in minutes) to unlock...  We don't want the door to be closed while the lock is engaged. 
+    }
+    else if ((contact.latestValue("contact") == "open") && (evt.value == "unlocked")) { // If the door is open and a person unlocks it then...
+        unschedule( unlockDoor ) // ...we don't need to unlock it later.
+    }
+    else if ((contact.latestValue("contact") == "closed") && (evt.value == "locked")) { // If the door is closed and a person manually locks it then...
+        unschedule( lockDoor ) // ...we don't need to lock it later.
+    }   
+    else if ((contact.latestValue("contact") == "closed") && (evt.value == "unlocked")) { // If the door is closed and a person unlocks it then...
+       //def delay = (minutesLater * 60) // runIn uses seconds
+        runIn( (minutesLater * 60), lockDoor ) // ...schedule (in minutes) to lock.
     }
-    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.
+    else if ((lock1.latestValue("lock") == "unlocked") && (evt.value == "open")) { // If a person opens an unlocked door...
+        unschedule( lockDoor ) // ...we don't need to lock it later.
+    }
+    else if ((lock1.latestValue("lock") == "unlocked") && (evt.value == "closed")) { // If a person closes an unlocked door...
+        //def delay = (minutesLater * 60) // runIn uses seconds
+        runIn( (minutesLater * 60), lockDoor ) // ...schedule (in minutes) to lock.
+    }
+    else { //Opening or Closing door when locked (in case you have a handle lock)
+        log.debug "Unlocking the door."
+        lock1.unlock()
+        if(location.contactBookEnabled) {
+            if ( recipients ) {
+                log.debug ( "Sending Push Notification..." ) 
+                sendNotificationToContacts( "${lock1} unlocked after ${contact} was opened or closed when ${lock1} was locked!", recipients)
+            }
+        }
+        if ( phoneNumber ) {
+            log.debug("Sending text message...")
+            sendSms( phoneNumber, "${lock1} unlocked after ${contact} was opened or closed when ${lock1} was locked!")
+        }
     }
 }