Update hue-mood-lighting.groovy
[smartapps.git] / official / rise-and-shine.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  *  Rise and Shine
14  *
15  *  Author: SmartThings
16  *  Date: 2013-03-07
17  */
18
19 definition(
20     name: "Rise and Shine",
21     namespace: "smartthings",
22     author: "SmartThings",
23     description: "Changes mode when someone wakes up after a set time in the morning.",
24     category: "Mode Magic",
25     iconUrl: "https://s3.amazonaws.com/smartapp-icons/ModeMagic/rise-and-shine.png",
26     iconX2Url: "https://s3.amazonaws.com/smartapp-icons/ModeMagic/rise-and-shine@2x.png"
27 )
28
29 preferences {
30         section("When there's motion on any of these sensors") {
31                 input "motionSensors", "capability.motionSensor", multiple: true
32         }
33         section("During this time window (default End Time is 4:00 PM)") {
34                 input "timeOfDay", "time", title: "Start Time?"
35                 input "endTime", "time", title: "End Time?", required: false
36         }
37         section("Change to this mode") {
38                 input "newMode", "mode", title: "Mode?"
39         }
40         section("And (optionally) turn on these appliances") {
41                 input "switches", "capability.switch", multiple: true, required: false
42         }
43         section( "Notifications" ) {
44         input("recipients", "contact", title: "Send notifications to") {
45             input "sendPushMessage", "enum", title: "Send a push notification?", options: ["Yes", "No"], required: false
46             input "phoneNumber", "phone", title: "Send a Text Message?", required: false
47         }
48         }
49 }
50
51 def installed() {
52         log.debug "installed, current mode = ${location.mode}, state.actionTakenOn = ${state.actionTakenOn}"
53         initialize()
54 }
55
56 def updated() {
57         log.debug "updated, current mode = ${location.mode}, state.actionTakenOn = ${state.actionTakenOn}"
58         unsubscribe()
59         initialize()
60 }
61
62 def initialize() {
63         log.trace "timeOfDay: $timeOfDay, endTime: $endTime"
64         subscribe(motionSensors, "motion.active", motionActiveHandler)
65         subscribe(location, modeChangeHandler)
66         if (state.modeStartTime == null) {
67                 state.modeStartTime = 0
68         }
69 }
70
71 def modeChangeHandler(evt) {
72         state.modeStartTime = now()
73 }
74
75 def motionActiveHandler(evt)
76 {
77         // for backward compatibility
78         if (state.modeStartTime == null) {
79                 subscribe(location, modeChangeHandler)
80                 state.modeStartTime = 0
81         }
82
83         def t0 = now()
84         def modeStartTime = new Date(state.modeStartTime)
85         def timeZone = location.timeZone ?: timeZone(timeOfDay)
86         def startTime = timeTodayAfter(modeStartTime, timeOfDay, timeZone)
87         def endTime = timeTodayAfter(startTime, endTime ?: "16:00", timeZone)
88         log.debug "startTime: $startTime, endTime: $endTime, t0: ${new Date(t0)}, modeStartTime: ${modeStartTime},  actionTakenOn: $state.actionTakenOn, currentMode: $location.mode, newMode: $newMode "
89
90         if (/*t0 >= startTime.time && t0 <= endTime.time*/true && location.mode != newMode) {
91                 def message = "Good morning! SmartThings changed the mode to '$newMode'"
92                 send(message)
93                 setLocationMode(newMode)
94                 log.debug message
95
96                 def dateString = new Date().format("yyyy-MM-dd")
97                 log.debug "last turned on switches on ${state.actionTakenOn}, today is ${dateString}"
98                 if (state.actionTakenOn != dateString) {
99                         log.debug "turning on switches"
100                         state.actionTakenOn = dateString
101                         switches?.on()
102                 }
103
104         }
105         else {
106                 log.debug "not in time window, or mode is already set, currentMode = ${location.mode}, newMode = $newMode"
107         }
108 }
109
110 private send(msg) {
111
112     if (location.contactBookEnabled) {
113         log.debug("sending notifications to: ${recipients?.size()}")
114         sendNotificationToContacts(msg, recipients)
115     }
116     else {
117         if (sendPushMessage != "No") {
118             log.debug("sending push message")
119             sendPush(msg)
120         }
121
122         if (phoneNumber) {
123             log.debug("sending text message")
124             sendSms(phoneNumber, msg)
125         }
126     }
127
128         log.debug msg
129 }