Commit #8: New version of extractor with running the preferences method make things...
[smartthings-infrastructure.git] / Extractor / App1 / App1.groovy
index 1c2c55f1302f347ebad33690ef38eff3ac551876..b94d720c26e4d62334b42874fbf0925656a6fdd7 100644 (file)
-//////////
+////////////////
 definition(
 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"
-)
+    name: "NFC Tag Toggle",
+    namespace: "smartthings",
+    author: "SmartThings",
+    description: "Allows toggling of a switch, lock, or garage door based on an NFC Tag touch event",
+    category: "SmartThings Internal",
+    iconUrl: "https://s3.amazonaws.com/smartapp-icons/Developers/nfc-tag-executor.png",
+    iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Developers/nfc-tag-executor@2x.png",
+    iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Developers/nfc-tag-executor@2x.png")
 
 
-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("Automatically unlock the door when open...") {
-        input "secondsLater", "number", title: "Delay (in seconds):", required: true
-    }
-    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
+
+preferences {
+    page(name: "pageOne", title: "Device selection", uninstall: true, nextPage: "pageTwo") {
+        section("Select an NFC tag") {
+            input "tag", "capability.touchSensor", title: "NFC Tag"
+        }
+        section("Select devices to control") {
+            input "switch1", "capability.switch", title: "Light or switch", required: false, multiple: true
+            input "lock", "capability.lock", title: "Lock", required: false, multiple: true
+            input "garageDoor", "capability.doorControl", title: "Garage door controller", required: false, multiple: true
         }
     }
         }
     }
+    
+    page(name: "pageTwo", title: "Master devices", install: true, uninstall: true)
 }
 
 }
 
-def installed(){
-    initialize()
+def pageTwo() {
+       dynamicPage(name: "pageTwo") {
+       section("If set, the state of these devices will be toggled each time the tag is touched, " + 
+                "e.g. a light that's on will be turned off and one that's off will be turned on, " +
+                "other devices of the same type will be set to the same state as their master device. " +
+                "If no master is designated then the majority of devices of the same type will be used " +
+                "to determine whether to turn on or off the devices.") {
+            
+            if (switch1 || masterSwitch) {
+                input "masterSwitch", "enum", title: "Master switch", options: switch1.collect{[(it.id): it.displayName]}, required: false
+            }
+            if (lock || masterLock) {
+                input "masterLock", "enum", title: "Master lock", options: lock.collect{[(it.id): it.displayName]}, required: false
+            }
+            if (garageDoor || masterDoor) {
+                input "masterDoor", "enum", title: "Master door", options: garageDoor.collect{[(it.id): it.displayName]}, required: false
+            }            
+               }
+               section([mobileOnly:true]) {
+                       label title: "Assign a name", required: false
+                       mode title: "Set for specific mode(s)", required: false
+               }        
+    }
 }
 
 }
 
-def updated(){
-    unsubscribe()
-    unschedule()
-    initialize()
+def installed() {
+       log.debug "Installed with settings: ${settings}"
+
+       initialize()
 }
 
 }
 
-def initialize(){
-    log.debug "Settings: ${settings}"
-    subscribe(lock1, "lock", doorHandler, [filterEvents: false])
-    subscribe(lock1, "unlock", doorHandler, [filterEvents: false])  
-    subscribe(contact, "contact.open", doorHandler)
-    subscribe(contact, "contact.closed", doorHandler)
+def updated() {
+       log.debug "Updated with settings: ${settings}"
+
+       unsubscribe()
+       initialize()
 }
 
 }
 
-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 initialize() {
+       subscribe tag, "nfcTouch", touchHandler
+    subscribe app, touchHandler
 }
 
 }
 
-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)
-        }
+private currentStatus(devices, master, attribute) {
+       log.trace "currentStatus($devices, $master, $attribute)"
+       def result = null
+       if (master) {
+       result = devices.find{it.id == master}?.currentValue(attribute)
     }
     }
-    if ( phoneNumber ) {
-        log.debug("Sending text message...")
-        sendSms( phoneNumber, "${lock1} unlocked after ${contact} was opened for ${secondsLater} seconds!")
+    else {
+       def map = [:]
+        devices.each {
+               def value = it.currentValue(attribute)
+            map[value] = (map[value] ?: 0) + 1
+            log.trace "$it.displayName: $value"
+        }
+        log.trace map
+        result = map.collect{it}.sort{it.value}[-1].key
     }
     }
+    log.debug "$attribute = $result"
+    result
 }
 
 }
 
-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 ((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.
+def touchHandler(evt) {
+       log.trace "touchHandler($evt.descriptionText)"
+    if (switch1) {
+       def status = currentStatus(switch1, masterSwitch, "switch")
+        switch1.each {
+            if (status == "on") {
+                it.off()
+            }
+            else {
+                it.on()
+            }
+        }
     }
     }
-    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 (lock) {
+       def status = currentStatus(lock, masterLock, "lock")
+        lock.each {
+            if (status == "locked") {
+                lock.unlock()
+            }
+            else {
+                lock.lock()
             }
         }
             }
         }
-        if ( phoneNumber ) {
-            log.debug("Sending text message...")
-            sendSms( phoneNumber, "${lock1} unlocked after ${contact} was opened or closed when ${lock1} was locked!")
+    }
+    
+    if (garageDoor) {
+        def status = currentStatus(garageDoor, masterDoor, "status")
+       garageDoor.each {
+               if (status == "open") {
+               it.close()
+            }
+            else {
+               it.open()
+            }
         }
     }
 }
         }
     }
 }