Update influxdb-logger.groovy
[smartapps.git] / official / scheduled-mode-change.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  *  Scheduled Mode Change - Presence Optional
14  *
15  *  Author: SmartThings
16
17  *
18  */
19
20 definition(
21     name: "Scheduled Mode Change",
22     namespace: "smartthings",
23     author: "SmartThings",
24     description: "Changes mode at a specific time of day.",
25     category: "Mode Magic",
26     iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/App-LightUpMyWorld.png",
27     iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/App-LightUpMyWorld@2x.png"
28 )
29
30 preferences {
31         section("At this time every day") {
32                 input "time", "time", title: "Time of Day"
33         }
34         section("Change to this mode") {
35                 input "newMode", "mode", title: "Mode?"
36         }
37         section( "Notifications" ) {
38         input("recipients", "contact", title: "Send notifications to") {
39             input "sendPushMessage", "enum", title: "Send a push notification?", options: ["Yes", "No"], required: false
40             input "phoneNumber", "phone", title: "Send a text message?", required: false
41         }
42         }
43 }
44
45 def installed() {
46         initialize()
47 }
48
49 def updated() {
50         unschedule()
51         initialize()
52 }
53
54 def initialize() {
55         schedule(time, changeMode)
56 }
57
58 def changeMode() {
59         log.debug "changeMode, location.mode = $location.mode, newMode = $newMode, location.modes = $location.modes"
60         if (location.mode != newMode) {
61                 if (location.modes?.find{it.name == newMode}) {
62                         setLocationMode(newMode)
63                         send "${label} has changed the mode to '${newMode}'"
64                 }
65                 else {
66                         send "${label} tried to change to undefined mode '${newMode}'"
67                 }
68         }
69 }
70
71 private send(msg) {
72
73     if (location.contactBookEnabled) {
74         log.debug("sending notifications to: ${recipients?.size()}")
75         sendNotificationToContacts(msg, recipients)
76     }
77     else {
78         if (sendPushMessage == "Yes") {
79             log.debug("sending push message")
80             sendPush(msg)
81         }
82
83         if (phoneNumber) {
84             log.debug("sending text message")
85             sendSms(phoneNumber, msg)
86         }
87     }
88
89         log.debug msg
90 }
91
92 private getLabel() {
93         app.label ?: "SmartThings"
94 }