[DVCSMP-2612] Unschedule execution for Left It Open when door is closed

This commit is contained in:
Aaron Miller
2017-05-05 13:22:46 -05:00
parent 68f5cda945
commit b6136bf1d5

View File

@@ -27,84 +27,82 @@ definition(
preferences { preferences {
section("Monitor this door or window") { section("Monitor this door or window") {
input "contact", "capability.contactSensor" input "contact", "capability.contactSensor"
} }
section("And notify me if it's open for more than this many minutes (default 10)") {
input "openThreshold", "number", description: "Number of minutes", required: false section("And notify me if it's open for more than this many minutes (default 10)") {
} input "openThreshold", "number", description: "Number of minutes", required: false
section("Delay between notifications (default 10 minutes") { }
input "frequency", "number", title: "Number of minutes", description: "", required: false
section("Delay between notifications (default 10 minutes") {
input "frequency", "number", title: "Number of minutes", description: "", required: false
}
section("Via text message at this number (or via push notification if not specified") {
input("recipients", "contact", title: "Send notifications to") {
input "phone", "phone", title: "Phone number (optional)", required: false
} }
section("Via text message at this number (or via push notification if not specified") { }
input("recipients", "contact", title: "Send notifications to") {
input "phone", "phone", title: "Phone number (optional)", required: false
}
}
} }
def installed() { def installed() {
log.trace "installed()" log.trace "installed()"
subscribe() subscribe()
} }
def updated() { def updated() {
log.trace "updated()" log.trace "updated()"
unsubscribe() unsubscribe()
subscribe() subscribe()
} }
def subscribe() { def subscribe() {
subscribe(contact, "contact.open", doorOpen) subscribe(contact, "contact.open", doorOpen)
subscribe(contact, "contact.closed", doorClosed) subscribe(contact, "contact.closed", doorClosed)
} }
def doorOpen(evt) def doorOpen(evt) {
{ log.trace "doorOpen($evt.name: $evt.value)"
log.trace "doorOpen($evt.name: $evt.value)" def delay = (openThreshold != null && openThreshold != "") ? openThreshold * 60 : 600
def t0 = now() runIn(delay, doorOpenTooLong, [overwrite: true])
def delay = (openThreshold != null && openThreshold != "") ? openThreshold * 60 : 600
runIn(delay, doorOpenTooLong, [overwrite: false])
log.debug "scheduled doorOpenTooLong in ${now() - t0} msec"
} }
def doorClosed(evt) def doorClosed(evt) {
{ log.trace "doorClosed($evt.name: $evt.value)"
log.trace "doorClosed($evt.name: $evt.value)" unschedule(doorOpenTooLong)
} }
def doorOpenTooLong() { def doorOpenTooLong() {
def contactState = contact.currentState("contact") def contactState = contact.currentState("contact")
def freq = (frequency != null && frequency != "") ? frequency * 60 : 600 def freq = (frequency != null && frequency != "") ? frequency * 60 : 600
if (contactState.value == "open") { if (contactState.value == "open") {
def elapsed = now() - contactState.rawDateCreated.time def elapsed = now() - contactState.rawDateCreated.time
def threshold = ((openThreshold != null && openThreshold != "") ? openThreshold * 60000 : 60000) - 1000 def threshold = ((openThreshold != null && openThreshold != "") ? openThreshold * 60000 : 60000) - 1000
if (elapsed >= threshold) { if (elapsed >= threshold) {
log.debug "Contact has stayed open long enough since last check ($elapsed ms): calling sendMessage()" log.debug "Contact has stayed open long enough since last check ($elapsed ms): calling sendMessage()"
sendMessage() sendMessage()
runIn(freq, doorOpenTooLong, [overwrite: false]) runIn(freq, doorOpenTooLong, [overwrite: false])
} else { } else {
log.debug "Contact has not stayed open long enough since last check ($elapsed ms): doing nothing" log.debug "Contact has not stayed open long enough since last check ($elapsed ms): doing nothing"
} }
} else { } else {
log.warn "doorOpenTooLong() called but contact is closed: doing nothing" log.warn "doorOpenTooLong() called but contact is closed: doing nothing"
} }
} }
void sendMessage() void sendMessage() {
{ def minutes = (openThreshold != null && openThreshold != "") ? openThreshold : 10
def minutes = (openThreshold != null && openThreshold != "") ? openThreshold : 10 def msg = "${contact.displayName} has been left open for ${minutes} minutes."
def msg = "${contact.displayName} has been left open for ${minutes} minutes." log.info msg
log.info msg if (location.contactBookEnabled) {
if (location.contactBookEnabled) { sendNotificationToContacts(msg, recipients)
sendNotificationToContacts(msg, recipients) } else {
} if (phone) {
else { sendSms phone, msg
if (phone) { } else {
sendSms phone, msg sendPush msg
} else {
sendPush msg
}
} }
}
} }