Update nfc-tag-toggle.groovy
[smartapps.git] / official / mail-arrived.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  *  Mail Arrived
14  *
15  *  Author: SmartThings
16  */
17 definition(
18     name: "Mail Arrived",
19     namespace: "smartthings",
20     author: "SmartThings",
21     description: "Send a text when mail arrives in your mailbox using a SmartSense Multi on your mailbox door. Note: battery life may be impacted in cold climates.",
22     category: "Convenience",
23     iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/mail_contact.png",
24     iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/mail_contact@2x.png"
25 )
26
27 preferences {
28         section("When mail arrives...") {
29                 input "accelerationSensor", "capability.accelerationSensor", title: "Where?"
30         }
31         section("Notify me...") {
32         input("recipients", "contact", title: "Send notifications to") {
33             input "pushNotification", "bool", title: "Push notification", required: false, defaultValue: "true"
34             input "phone1", "phone", title: "Phone number", required: false
35         }
36         }
37 }
38
39 def installed() {
40         subscribe(accelerationSensor, "acceleration.active", accelerationActiveHandler)
41 }
42
43 def updated() {
44         unsubscribe()
45         subscribe(accelerationSensor, "acceleration.active", accelerationActiveHandler)
46 }
47
48 def accelerationActiveHandler(evt) {
49         log.trace "$evt.value: $evt, $settings"
50
51         // Don't send a continuous stream of notifications
52         def deltaSeconds = 5
53         def timeAgo = new Date(now() - (1000 * deltaSeconds))
54         def recentEvents = accelerationSensor.eventsSince(timeAgo)
55         log.trace "Found ${recentEvents?.size() ?: 0} events in the last $deltaSeconds seconds"
56         def alreadySentNotifications = recentEvents.count { it.value && it.value == "active" } > 1
57
58         if (alreadySentNotifications) {
59                 log.debug "Notifications already sent within the last $deltaSeconds seconds (phone1: $phone1, pushNotification: $pushNotification)"
60         }
61         else {
62         if (location.contactBookEnabled) {
63             log.debug "$accelerationSensor has moved, notifying ${recipients?.size()}"
64             sendNotificationToContacts("Mail has arrived!", recipients)
65         }
66         else {
67         if (phone1 != null && phone1 != "") {
68             log.debug "$accelerationSensor has moved, texting $phone1"
69             sendSms(phone1, "Mail has arrived!")
70         }
71         if (pushNotification) {
72             log.debug "$accelerationSensor has moved, sending push"
73             sendPush("Mail has arrived!")
74         }
75     }
76         }
77 }