Update lighting-director.groovy
[smartapps.git] / official / carpool-notifier.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  * title: Carpool Notifier
14  *
15  * description:
16  * Do you carpool to work with your spouse? Do you pick your children up from school? Have they been waiting in doors for you? Let them know you've arrived with Carpool Notifier.
17  *
18  * This SmartApp is designed to send notifications to your carpooling buddies when you arrive to pick them up. What separates this SmartApp from other notification SmartApps is that it will only send a notification if your carpool buddy is not with you.
19  *
20  * category: Family
21
22  * icon:                https://s3.amazonaws.com/smartapp-icons/Family/App-IMadeIt.png
23  * icon2X:      https://s3.amazonaws.com/smartapp-icons/Family/App-IMadeIt%402x.png
24  *
25  *  Author: steve
26  *  Date: 2013-11-19
27  */
28
29 definition(
30     name: "Carpool Notifier",
31     namespace: "smartthings",
32     author: "SmartThings",
33     description: "Send notifications to your carpooling buddies when you arrive to pick them up. If the person you are picking up is home, and has been for 5 minutes or more, they will get a notification when you arrive.",
34     category: "Green Living",
35     iconUrl: "https://s3.amazonaws.com/smartapp-icons/Family/App-IMadeIt.png",
36     iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Family/App-IMadeIt@2x.png"
37 )
38
39 preferences {
40         section() {
41                 input(name: "driver", type: "capability.presenceSensor", required: true, multiple: false, title: "When this person arrives", description: "Who's driving?")
42                 input("recipients", "contact", title: "Notify", description: "Send notifications to") {
43                         input(name: "phoneNumber", type: "phone", required: true, multiple: false, title: "Send a text to", description: "Phone number")
44                 }
45                 input(name: "message", type: "text", required: false, multiple: false, title: "With the message:", description: "Your ride is here!")
46                 input(name: "rider", type: "capability.presenceSensor", required: true, multiple: false, title: "But only when this person is not with you", description: "Who are you picking up?")
47         }
48 }
49
50 def installed() {
51         log.debug "Installed with settings: ${settings}"
52
53         initialize()
54 }
55
56 def updated() {
57         log.debug "Updated with settings: ${settings}"
58
59         unsubscribe()
60         initialize()
61 }
62
63 def initialize() {
64         subscribe(driver, "presence.present", presence)
65 }
66
67 def presence(evt) {
68
69         if (evt.value == "present" && riderIsHome())
70         {
71 //              log.debug "Rider Is Home; Send A Text"
72                 sendText()
73         }
74
75 }
76
77 def riderIsHome() {
78
79 //      log.debug "rider presence: ${rider.currentPresence}"
80
81         if (rider.currentPresence != "present")
82         {
83                 return false
84         }
85
86         def riderState = rider.currentState("presence")
87 //      log.debug "riderState: ${riderState}"
88         if (!riderState)
89         {
90                 return true
91         }
92
93         def latestState = rider.latestState("presence")
94
95         def now = new Date()
96         def minusFive = new Date(minutes: now.minutes - 5)
97
98
99         if (minusFive > latestState.date)
100         {
101                 return true
102         }
103
104         return false
105 }
106
107 def sendText() {
108         if (location.contactBookEnabled) {
109                 sendNotificationToContacts(message ?: "Your ride is here!", recipients)
110         }
111         else {
112                 sendSms(phoneNumber, message ?: "Your ride is here!")
113         }
114 }