Update README.md
[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 "zipCode1", "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, "sunriseTime", sunriseSunsetTimeHandler)
72         subscribe(location, "sunsetTime", sunriseSunsetTimeHandler)
73         
74         //astroCheck()
75 }
76
77 def locationPositionChange(evt) {
78         log.trace "locationChange()"
79         schedule("someTime", sunriseHandler)
80         schedule("someTime", sunsetHandler)
81         //astroCheck()
82 }
83
84 def sunriseSunsetTimeHandler(evt) {
85         log.trace "sunriseSunsetTimeHandler()"
86         schedule("someTime", sunriseHandler)
87         schedule("someTime", sunsetHandler)
88         //astroCheck()
89 }
90
91 def astroCheck() {
92         def s = getSunriseAndSunset(zipCode: zipCode1, sunriseOffset: sunriseOffset, sunsetOffset: sunsetOffset)
93
94         def now = new Date()
95         def riseTime = s.sunrise
96         def setTime = s.sunset
97         log.debug "riseTime: $riseTime"
98         log.debug "setTime: $setTime"
99
100         if (state.riseTime != riseTime.time) {
101                 unschedule("sunriseHandler")
102
103                 if(riseTime.before(now)) {
104                         riseTime = riseTime.next()
105                 }
106
107                 state.riseTime = riseTime.time
108
109                 log.info "scheduling sunrise handler for $riseTime"
110                 schedule(riseTime, sunriseHandler)
111         }
112
113         if (state.setTime != setTime.time) {
114                 unschedule("sunsetHandler")
115
116             if(setTime.before(now)) {
117                     setTime = setTime.next()
118             }
119
120                 state.setTime = setTime.time
121
122                 log.info "scheduling sunset handler for $setTime"
123             schedule(setTime, sunsetHandler)
124         }
125 }
126
127 def sunriseHandler() {
128         log.info "Executing sunrise handler"
129         if (sunriseOn) {
130                 sunriseOn.on()
131         }
132         if (sunriseOff) {
133                 sunriseOff.off()
134         }
135         changeMode(sunriseMode)
136 }
137
138 def sunsetHandler() {
139         log.info "Executing sunset handler"
140         if (sunsetOn) {
141                 sunsetOn.on()
142         }
143         if (sunsetOff) {
144                 sunsetOff.off()
145         }
146         changeMode(sunsetMode)
147 }
148
149 def changeMode(newMode) {
150         if (newMode && location.mode != newMode) {
151                 if (location.modes?.find{it.name == newMode}) {
152                         setLocationMode(newMode)
153                         send "${label} has changed the mode to '${newMode}'"
154                 }
155                 else {
156                         send "${label} tried to change to undefined mode '${newMode}'"
157                 }
158         }
159 }
160
161 private send(msg) {
162     if (location.contactBookEnabled) {
163         log.debug("sending notifications to: ${recipients?.size()}")
164         sendNotificationToContacts(msg, recipients)
165     }
166     else {
167         if (sendPushMessage != "No") {
168             log.debug("sending push message")
169             sendPush(msg)
170         }
171
172         if (phoneNumber) {
173             log.debug("sending text message")
174             sendSms(phoneNumber, msg)
175         }
176     }
177
178         log.debug msg
179 }
180
181 private getLabel() {
182         app.label ?: "SmartThings"
183 }
184
185 private getSunriseOffset() {
186         sunriseOffsetValue ? (sunriseOffsetDir == "Before" ? "-$sunriseOffsetValue" : sunriseOffsetValue) : null
187 }
188
189 private getSunsetOffset() {
190         sunsetOffsetValue ? (sunsetOffsetDir == "Before" ? "-$sunsetOffsetValue" : sunsetOffsetValue) : null
191 }
192