Create empty_app.groovy
[smartapps.git] / official / turn-on-by-zip-code.groovy
1 /**
2  *  Turn on by ZIP code
3  *
4  *  Copyright 2015 SmartThings
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7  *  in compliance with the License. You may obtain a copy of the License at:
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
12  *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
13  *  for the specific language governing permissions and limitations under the License.
14  *
15  */
16 definition(
17         name: "Turn on by ZIP code",
18         namespace: "examples",
19         author: "SmartThings",
20         description: "Turn on lights based on ZIP code",
21         category: "My Apps",
22         iconUrl: "https://s3.amazonaws.com/smartapp-icons/ModeMagic/rise-and-shine.png",
23         iconX2Url: "https://s3.amazonaws.com/smartapp-icons/ModeMagic/rise-and-shine@2x.png",
24         iconX3Url: "https://s3.amazonaws.com/smartapp-icons/ModeMagic/rise-and-shine@2x.png")
25
26 preferences {
27     section("Lights") {
28         input "switches", "capability.switch", title: "Which lights to turn on?"
29     }
30     section("Sunset offset (optional)...") {
31         input "sunsetOffsetValue", "text", title: "HH:MM", required: false
32     }
33     section("Zip code") {
34         input "zipCode1", "text", required: false
35     }
36 }
37
38 def installed() {
39     initialize()
40 }
41
42 def updated() {
43     unsubscribe()
44     initialize()
45 }
46
47 def initialize() {
48     scheduleNextSunset()
49 }
50
51 def scheduleNextSunset(date = null) {
52     /*def s = getSunriseAndSunset(zipCode: zipCode1, sunsetOffset: getSunsetOffset(), date: date)
53     def now = new Date()
54     def setTime = s.sunset
55     log.debug "setTime: $setTime"
56
57     // use state to keep track of sunset times between executions
58     // if sunset time has changed, unschedule and reschedule handler with updated time
59     if(state.setTime != setTime.time) {
60         unschedule("sunsetHandler")
61
62         if(setTime.before(now)) {
63             setTime = setTime.next()
64         }
65
66         state.setTime = setTime.time
67
68         log.info "scheduling sunset handler for $setTime"
69         schedule(setTime, sunsetHandler)
70     }*/
71     schedule("someTime", sunsetHandler)
72 }
73
74 def sunsetHandler() {
75     log.debug "turning on lights"
76     switches.on()
77
78     // schedule for tomorrow
79     //scheduleNextSunset(new Date() + 1)
80 }
81
82 private getSunsetOffset() {
83     //if there is an offset, make negative since we only care about before
84     sunsetOffsetValue ? "-$sunsetOffsetValue" : null
85 }