Update notify-me-with-hue.groovy
[smartapps.git] / official / close-the-valve.groovy
1 /**
2  *  Close a valve if moisture is detected
3  *
4  *  Copyright 2014 SmartThings
5  *
6  *      Author: Juan Risso
7  *
8  *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
9  *  in compliance with the License. You may obtain a copy of the License at:
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
14  *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
15  *  for the specific language governing permissions and limitations under the License.
16  *
17  */
18 definition(
19     name: "Close The Valve",
20     namespace: "smartthings",
21     author: "SmartThings",
22     description: "Close a selected valve if moisture is detected, and get notified by SMS and push notification.",
23     category: "Safety & Security",
24     iconUrl: "https://s3.amazonaws.com/smartapp-icons/Developers/dry-the-wet-spot.png",
25     iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Developers/dry-the-wet-spot@2x.png"
26 )
27
28 preferences {
29         section("When water is sensed...") {
30                 input "sensor", "capability.waterSensor", title: "Where?", required: true, multiple: true
31         }
32         section("Close the valve...") {
33                 input "valve", "capability.valve", title: "Which?", required: true, multiple: false
34         }
35         section("Send this message (optional, sends standard status message if not specified)"){
36                 input "messageText", "text", title: "Message Text", required: false
37         }
38         section("Via a push notification and/or an SMS message"){
39                 input "phone", "phone", title: "Phone Number (for SMS, optional)", required: false
40                 input "pushAndPhone", "enum", title: "Both Push and SMS?", required: false, options: ["Yes","No"]
41         }
42         section("Minimum time between messages (optional)") {
43                 input "frequency", "decimal", title: "Minutes", required: false
44         }    
45 }
46
47 def installed() {
48         subscribe(sensor, "water", waterHandler)
49 }
50
51 def updated() {
52         unsubscribe()
53         subscribe(sensor, "water", waterHandler)
54 }
55
56 def waterHandler(evt) {
57         log.debug "Sensor says ${evt.value}"
58         if (evt.value == "wet") {
59                 valve.close()
60         }
61         if (frequency) {
62                 def lastTime = state[evt.deviceId]
63                 if (lastTime == null || now() - lastTime >= frequency * 60000) {
64                         sendMessage(evt)
65                 }
66         }
67         else {
68                 sendMessage(evt)
69         }    
70 }
71
72 private sendMessage(evt) {
73         def msg = messageText ?: "We closed the valve because moisture was detected"
74         log.debug "$evt.name:$evt.value, pushAndPhone:$pushAndPhone, '$msg'"
75
76         if (!phone || pushAndPhone != "No") {
77                 log.debug "sending push"
78                 sendPush(msg)
79         }
80         if (phone) {
81                 log.debug "sending SMS"
82                 sendSms(phone, msg)
83         }
84         if (frequency) {
85                 state[evt.deviceId] = now()
86         }
87 }