X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=official%2Fflood-alert.groovy;fp=official%2Fflood-alert.groovy;h=f44aa48607b05c3f44f486379914d1b7256820ba;hb=3325c1b0cc49b9fbbc497cb3612f7aeff5263eca;hp=0000000000000000000000000000000000000000;hpb=6770acaa437dc24e7a7be71f3adb72f41e80ce2c;p=smartapps.git diff --git a/official/flood-alert.groovy b/official/flood-alert.groovy new file mode 100755 index 0000000..f44aa48 --- /dev/null +++ b/official/flood-alert.groovy @@ -0,0 +1,73 @@ +/** + * Copyright 2015 SmartThings + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License + * for the specific language governing permissions and limitations under the License. + * + * Flood Alert + * + * Author: SmartThings + */ +definition( + name: "Flood Alert!", + namespace: "smartthings", + author: "SmartThings", + description: "Get a push notification or text message when water is detected where it doesn't belong.", + category: "Safety & Security", + iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/water_moisture.png", + iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/water_moisture@2x.png" +) + +preferences { + section("When there's water detected...") { + input "alarm", "capability.waterSensor", title: "Where?" + } + section("Send a notification to...") { + input("recipients", "contact", title: "Recipients", description: "Send notifications to") { + input "phone", "phone", title: "Phone number?", required: false + } + } +} + +def installed() { + subscribe(alarm, "water.wet", waterWetHandler) +} + +def updated() { + unsubscribe() + subscribe(alarm, "water.wet", waterWetHandler) +} + +def waterWetHandler(evt) { + def deltaSeconds = 60 + + def timeAgo = new Date(now() - (1000 * deltaSeconds)) + def recentEvents = alarm.eventsSince(timeAgo) + log.debug "Found ${recentEvents?.size() ?: 0} events in the last $deltaSeconds seconds" + + def alreadySentSms = recentEvents.count { it.value && it.value == "wet" } > 1 + + if (alreadySentSms) { + log.debug "SMS already sent within the last $deltaSeconds seconds" + } else { + def msg = "${alarm.displayName} is wet!" + log.debug "$alarm is wet, texting phone number" + + if (location.contactBookEnabled) { + sendNotificationToContacts(msg, recipients) + } + else { + sendPush(msg) + if (phone) { + sendSms(phone, msg) + } + } + } +} +