Update groveStreams.groovy
[smartapps.git] / official / sunrise-sunset.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  *  Sunrise, Sunset
14  *
15  *  Author: SmartThings
16  *
17  *  Date: 2013-04-30
18  */
19 definition(
20     name: "Sunrise/Sunset",
21     namespace: "smartthings",
22     author: "SmartThings",
23     description: "Changes mode and controls lights based on local sunrise and sunset times.",
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 ("At sunrise...") {
31                 input "sunriseMode", "mode", title: "Change mode to?", required: false
32                 input "sunriseOn", "capability.switch", title: "Turn on?", required: false, multiple: true
33                 input "sunriseOff", "capability.switch", title: "Turn off?", required: false, multiple: true
34         }
35         section ("At sunset...") {
36                 input "sunsetMode", "mode", title: "Change mode to?", required: false
37                 input "sunsetOn", "capability.switch", title: "Turn on?", required: false, multiple: true
38                 input "sunsetOff", "capability.switch", title: "Turn off?", required: false, multiple: true
39         }
40         section ("Sunrise offset (optional)...") {
41                 input "sunriseOffsetValue", "text", title: "HH:MM", required: false
42                 input "sunriseOffsetDir", "enum", title: "Before or After", required: false, options: ["Before","After"]
43         }
44         section ("Sunset offset (optional)...") {
45                 input "sunsetOffsetValue", "text", title: "HH:MM", required: false
46                 input "sunsetOffsetDir", "enum", title: "Before or After", required: false, options: ["Before","After"]
47         }
48         section ("Zip code (optional, defaults to location coordinates)...") {
49                 input "zipCode", "text", required: false
50         }
51         section( "Notifications" ) {
52         input("recipients", "contact", title: "Send notifications to") {
53             input "sendPushMessage", "enum", title: "Send a push notification?", options: ["Yes", "No"], required: false
54             input "phoneNumber", "phone", title: "Send a text message?", required: false
55         }
56         }
57
58 }
59
60 def installed() {
61         initialize()
62 }
63
64 def updated() {
65         unsubscribe()
66         //unschedule handled in astroCheck method
67         initialize()
68 }
69
70 def initialize() {
71         subscribe(location, "position", locationPositionChange)
72         subscribe(location, "sunriseTime", sunriseSunsetTimeHandler)
73         subscribe(location, "sunsetTime", sunriseSunsetTimeHandler)
74
75         astroCheck()
76 }
77
78 def locationPositionChange(evt) {
79         log.trace "locationChange()"
80         astroCheck()
81 }
82
83 def sunriseSunsetTimeHandler(evt) {
84         log.trace "sunriseSunsetTimeHandler()"
85         astroCheck()
86 }
87
88 def astroCheck() {
89         def s = getSunriseAndSunset(zipCode: zipCode, sunriseOffset: sunriseOffset, sunsetOffset: sunsetOffset)
90
91         def now = new Date()
92         def riseTime = s.sunrise
93         def setTime = s.sunset
94         log.debug "riseTime: $riseTime"
95         log.debug "setTime: $setTime"
96
97         if (state.riseTime != riseTime.time) {
98                 unschedule("sunriseHandler")
99
100                 if(riseTime.before(now)) {
101                         riseTime = riseTime.next()
102                 }
103
104                 state.riseTime = riseTime.time
105
106                 log.info "scheduling sunrise handler for $riseTime"
107                 schedule(riseTime, sunriseHandler)
108         }
109
110         if (state.setTime != setTime.time) {
111                 unschedule("sunsetHandler")
112
113             if(setTime.before(now)) {
114                     setTime = setTime.next()
115             }
116
117                 state.setTime = setTime.time
118
119                 log.info "scheduling sunset handler for $setTime"
120             schedule(setTime, sunsetHandler)
121         }
122 }
123
124 def sunriseHandler() {
125         log.info "Executing sunrise handler"
126         if (sunriseOn) {
127                 sunriseOn.on()
128         }
129         if (sunriseOff) {
130                 sunriseOff.off()
131         }
132         changeMode(sunriseMode)
133 }
134
135 def sunsetHandler() {
136         log.info "Executing sunset handler"
137         if (sunsetOn) {
138                 sunsetOn.on()
139         }
140         if (sunsetOff) {
141                 sunsetOff.off()
142         }
143         changeMode(sunsetMode)
144 }
145
146 def changeMode(newMode) {
147         if (newMode && location.mode != newMode) {
148                 if (location.modes?.find{it.name == newMode}) {
149                         setLocationMode(newMode)
150                         send "${label} has changed the mode to '${newMode}'"
151                 }
152                 else {
153                         send "${label} tried to change to undefined mode '${newMode}'"
154                 }
155         }
156 }
157
158 private send(msg) {
159     if (location.contactBookEnabled) {
160         log.debug("sending notifications to: ${recipients?.size()}")
161         sendNotificationToContacts(msg, recipients)
162     }
163     else {
164         if (sendPushMessage != "No") {
165             log.debug("sending push message")
166             sendPush(msg)
167         }
168
169         if (phoneNumber) {
170             log.debug("sending text message")
171             sendSms(phoneNumber, msg)
172         }
173     }
174
175         log.debug msg
176 }
177
178 private getLabel() {
179         app.label ?: "SmartThings"
180 }
181
182 private getSunriseOffset() {
183         sunriseOffsetValue ? (sunriseOffsetDir == "Before" ? "-$sunriseOffsetValue" : sunriseOffsetValue) : null
184 }
185
186 private getSunsetOffset() {
187         sunsetOffsetValue ? (sunsetOffsetDir == "Before" ? "-$sunsetOffsetValue" : sunsetOffsetValue) : null
188 }
189