Update step-notifier.groovy
[smartapps.git] / official / notify-me-when.groovy
1 /**
2  *  Copyright 2015 SmartThings
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  *  in compliance with the License. You may obtain a copy of the License at:
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
10  *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
11  *  for the specific language governing permissions and limitations under the License.
12  *
13  *  Notify Me When
14  *
15  *  Author: SmartThings
16  *  Date: 2013-03-20
17  *
18  * Change Log:
19  *      1. Todd Wackford
20  *      2014-10-03:     Added capability.button device picker and button.pushed event subscription. For Doorbell.
21  */
22 definition(
23                 name: "Notify Me When",
24                 namespace: "smartthings",
25                 author: "SmartThings",
26                 description: "Receive notifications when anything happens in your home.",
27                 category: "Convenience",
28                 iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/window_contact.png",
29                 iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/window_contact@2x.png"
30 )
31
32 preferences {
33         section("Choose one or more, when..."){
34                 input "button", "capability.button", title: "Button Pushed", required: false, multiple: true //tw
35                 input "motion", "capability.motionSensor", title: "Motion Here", required: false, multiple: true
36                 input "contact", "capability.contactSensor", title: "Contact Opens", required: false, multiple: true
37                 input "contactClosed", "capability.contactSensor", title: "Contact Closes", required: false, multiple: true
38                 input "acceleration", "capability.accelerationSensor", title: "Acceleration Detected", required: false, multiple: true
39                 input "mySwitch", "capability.switch", title: "Switch Turned On", required: false, multiple: true
40                 input "mySwitchOff", "capability.switch", title: "Switch Turned Off", required: false, multiple: true
41                 input "arrivalPresence", "capability.presenceSensor", title: "Arrival Of", required: false, multiple: true
42                 input "departurePresence", "capability.presenceSensor", title: "Departure Of", required: false, multiple: true
43                 input "smoke", "capability.smokeDetector", title: "Smoke Detected", required: false, multiple: true
44                 input "water", "capability.waterSensor", title: "Water Sensor Wet", required: false, multiple: true
45         }
46         section("Send this message (optional, sends standard status message if not specified)"){
47                 input "messageText", "text", title: "Message Text", required: false
48         }
49         section("Via a push notification and/or an SMS message"){
50                 input("recipients", "contact", title: "Send notifications to") {
51                         input "phone", "phone", title: "Enter a phone number to get SMS", required: false
52                         paragraph "If outside the US please make sure to enter the proper country code"
53                         input "pushAndPhone", "enum", title: "Notify me via Push Notification", required: false, options: ["Yes", "No"]
54                 }
55         }
56         section("Minimum time between messages (optional, defaults to every message)") {
57                 input "frequency", "decimal", title: "Minutes", required: false
58         }
59 }
60
61 def installed() {
62         log.debug "Installed with settings: ${settings}"
63         subscribeToEvents()
64 }
65
66 def updated() {
67         log.debug "Updated with settings: ${settings}"
68         unsubscribe()
69         subscribeToEvents()
70 }
71
72 def subscribeToEvents() {
73         subscribe(button, "button.pushed", eventHandler) //tw
74         subscribe(contact, "contact.open", eventHandler)
75         subscribe(contactClosed, "contact.closed", eventHandler)
76         subscribe(acceleration, "acceleration.active", eventHandler)
77         subscribe(motion, "motion.active", eventHandler)
78         subscribe(mySwitch, "switch.on", eventHandler)
79         subscribe(mySwitchOff, "switch.off", eventHandler)
80         subscribe(arrivalPresence, "presence.present", eventHandler)
81         subscribe(departurePresence, "presence.not present", eventHandler)
82         subscribe(smoke, "smoke.detected", eventHandler)
83         subscribe(smoke, "smoke.tested", eventHandler)
84         subscribe(smoke, "carbonMonoxide.detected", eventHandler)
85         subscribe(water, "water.wet", eventHandler)
86 }
87
88 def eventHandler(evt) {
89         log.debug "Notify got evt ${evt}"
90         if (frequency) {
91                 def lastTime = state[evt.deviceId]
92                 if (lastTime == null || now() - lastTime >= frequency * 60000) {
93                         sendMessage(evt)
94                 }
95         }
96         else {
97                 sendMessage(evt)
98         }
99 }
100
101 private sendMessage(evt) {
102         String msg = messageText
103         Map options = [:]
104
105         if (!messageText) {
106                 msg = defaultText(evt)
107                 options = [translatable: true, triggerEvent: evt]
108         }
109         log.debug "$evt.name:$evt.value, pushAndPhone:$pushAndPhone, '$msg'"
110
111         if (location.contactBookEnabled) {
112                 sendNotificationToContacts(msg, recipients, options)
113         } else {
114                 if (phone) {
115                         options.phone = phone
116                         if (pushAndPhone != 'No') {
117                                 log.debug 'Sending push and SMS'
118                                 options.method = 'both'
119                         } else {
120                                 log.debug 'Sending SMS'
121                                 options.method = 'phone'
122                         }
123                 } else if (pushAndPhone != 'No') {
124                         log.debug 'Sending push'
125                         options.method = 'push'
126                 } else {
127                         log.debug 'Sending nothing'
128                         options.method = 'none'
129                 }
130                 sendNotification(msg, options)
131         }
132         if (frequency) {
133                 state[evt.deviceId] = now()
134         }
135 }
136
137 private defaultText(evt) {
138         if (evt.name == 'presence') {
139                 if (evt.value == 'present') {
140                         if (includeArticle) {
141                                 '{{ triggerEvent.linkText }} has arrived at the {{ location.name }}'
142                         }
143                         else {
144                                 '{{ triggerEvent.linkText }} has arrived at {{ location.name }}'
145                         }
146                 } else {
147                         if (includeArticle) {
148                                 '{{ triggerEvent.linkText }} has left the {{ location.name }}'
149                         }
150                         else {
151                                 '{{ triggerEvent.linkText }} has left {{ location.name }}'
152                         }
153                 }
154         } else {
155                 '{{ triggerEvent.descriptionText }}'
156         }
157 }
158
159 private getIncludeArticle() {
160         def name = location.name.toLowerCase()
161         def segs = name.split(" ")
162         !(["work","home"].contains(name) || (segs.size() > 1 && (["the","my","a","an"].contains(segs[0]) || segs[0].endsWith("'s"))))
163 }