[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

@@ -30,12 +30,15 @@ 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)") { 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 input "openThreshold", "number", description: "Number of minutes", required: false
} }
section("Delay between notifications (default 10 minutes") { section("Delay between notifications (default 10 minutes") {
input "frequency", "number", title: "Number of minutes", description: "", required: false input "frequency", "number", title: "Number of minutes", description: "", required: false
} }
section("Via text message at this number (or via push notification if not specified") { section("Via text message at this number (or via push notification if not specified") {
input("recipients", "contact", title: "Send notifications to") { input("recipients", "contact", title: "Send notifications to") {
input "phone", "phone", title: "Phone number (optional)", required: false input "phone", "phone", title: "Phone number (optional)", required: false
@@ -59,18 +62,15 @@ def subscribe() {
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 t0 = now()
def delay = (openThreshold != null && openThreshold != "") ? openThreshold * 60 : 600 def delay = (openThreshold != null && openThreshold != "") ? openThreshold * 60 : 600
runIn(delay, doorOpenTooLong, [overwrite: false]) runIn(delay, doorOpenTooLong, [overwrite: true])
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() {
@@ -92,15 +92,13 @@ def doorOpenTooLong() {
} }
} }
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 {
else {
if (phone) { if (phone) {
sendSms phone, msg sendSms phone, msg
} else { } else {