Updating code to use the phonebook API

This commit is contained in:
Kris Schaller
2015-12-28 16:58:27 -08:00
parent f12684565c
commit 629c4cc231

View File

@@ -13,7 +13,7 @@ preferences{
input "lock1", "capability.lock", required: true input "lock1", "capability.lock", required: true
} }
section("Select the door contact sensor:") { section("Select the door contact sensor:") {
input "contact", "capability.contactSensor", required: true input "contact", "capability.contactSensor", required: true
} }
section("Automatically lock the door when closed...") { section("Automatically lock the door when closed...") {
input "minutesLater", "number", title: "Delay (in minutes):", required: true input "minutesLater", "number", title: "Delay (in minutes):", required: true
@@ -22,9 +22,10 @@ preferences{
input "secondsLater", "number", title: "Delay (in seconds):", required: true input "secondsLater", "number", title: "Delay (in seconds):", required: true
} }
section( "Notifications" ) { section( "Notifications" ) {
input "sendPushMessage", "enum", title: "Send a push notification?", metadata:[values:["Yes", "No"]], required: false input("recipients", "contact", title: "Send notifications to", required: false) {
input "phoneNumber", "phone", title: "Enter phone number to send text notification.", required: false input "phoneNumber", "phone", title: "Warn with text message (optional)", description: "Phone Number", required: false
} }
}
} }
def installed(){ def installed(){
@@ -42,55 +43,73 @@ def initialize(){
subscribe(lock1, "lock", doorHandler, [filterEvents: false]) subscribe(lock1, "lock", doorHandler, [filterEvents: false])
subscribe(lock1, "unlock", doorHandler, [filterEvents: false]) subscribe(lock1, "unlock", doorHandler, [filterEvents: false])
subscribe(contact, "contact.open", doorHandler) subscribe(contact, "contact.open", doorHandler)
subscribe(contact, "contact.closed", doorHandler) subscribe(contact, "contact.closed", doorHandler)
} }
def lockDoor(){ def lockDoor(){
log.debug "Locking the door." log.debug "Locking the door."
lock1.lock() lock1.lock()
log.debug ( "Sending Push Notification..." ) if(location.contactBookEnabled) {
if ( sendPushMessage != "No" ) sendPush( "${lock1} locked after ${contact} was closed for ${minutesLater} minutes!" ) if ( recipients ) {
log.debug("Sending text message...") log.debug ( "Sending Push Notification..." )
if ( phoneNumber != "0" ) sendSms( phoneNumber, "${lock1} locked after ${contact} was closed for ${minutesLater} minutes!" ) 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 unlockDoor(){ def unlockDoor(){
log.debug "Unlocking the door." log.debug "Unlocking the door."
lock1.unlock() lock1.unlock()
log.debug ( "Sending Push Notification..." ) if(location.contactBookEnabled) {
if ( sendPushMessage != "No" ) sendPush( "${lock1} unlocked after ${contact} was opened for ${secondsLater} seconds!" ) if ( recipients ) {
log.debug("Sending text message...") log.debug ( "Sending Push Notification..." )
if ( phoneNumber != "0" ) sendSms( phoneNumber, "${lock1} unlocked after ${contact} was opened for ${secondsLater} seconds!" ) 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){ def doorHandler(evt){
if ((contact.latestValue("contact") == "open") && (evt.value == "locked")) { // If the door is open and a person locks the door then... 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 //def delay = (secondsLater) // runIn uses seconds
runIn( delay, unlockDoor ) // ...schedule (in minutes) to unlock... We don't want the door to be closed while the lock is engaged. 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... 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. 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... 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. 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... 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 //def delay = (minutesLater * 60) // runIn uses seconds
runIn( delay, lockDoor ) // ...schedule (in minutes) to lock. 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... 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. 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... else if ((lock1.latestValue("lock") == "unlocked") && (evt.value == "closed")) { // If a person closes an unlocked door...
def delay = (minutesLater * 60) // runIn uses seconds //def delay = (minutesLater * 60) // runIn uses seconds
runIn( delay, lockDoor ) // ...schedule (in minutes) to lock. runIn( (minutesLater * 60), lockDoor ) // ...schedule (in minutes) to lock.
} }
else { //Opening or Closing door when locked (in case you have a handle lock) else { //Opening or Closing door when locked (in case you have a handle lock)
log.debug "Unlocking the door." log.debug "Unlocking the door."
lock1.unlock() lock1.unlock()
log.debug ( "Sending Push Notification..." ) if(location.contactBookEnabled) {
if ( sendPushMessage != "No" ) sendPush( "${lock1} unlocked after ${contact} was opened or closed when ${lock1} was locked!" ) if ( recipients ) {
log.debug("Sending text message...") log.debug ( "Sending Push Notification..." )
if ( phoneNumber != "0" ) sendSms( phoneNumber, "${lock1} unlocked after ${contact} was opened or closed when ${lock1} was locked!" ) 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!")
}
}
} }