Sunrise/Sunset Mode Changer SmartApp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Sunrise/Sunset | |
* | |
* Author: dianoga7@3dgo.net | |
* Date: 2013-06-26 | |
*/ | |
preferences { | |
section("Sunrise") { | |
input ('sunrise_mode', 'mode', title: 'Change to mode: ') | |
} | |
section("Sunset") { | |
input ('sunset_mode', 'mode', title: 'Change to mode: ') | |
} | |
section("Miscellaneous") { | |
input ('zip', 'number', title: 'Zip Code', required: false) | |
} | |
} | |
def installed() { | |
log.debug "Installed with settings: ${settings}" | |
initialize() | |
} | |
def updated() { | |
log.debug "Updated with settings: ${settings}" | |
unsubscribe() | |
initialize() | |
} | |
def initialize() { | |
def data = getWeatherFeature('astronomy', settings.zip as String) | |
def sunsetTime = data.moon_phase.sunset.hour + ':' + data.moon_phase.sunset.minute | |
def sunriseTime = data.moon_phase.sunrise.hour + ':' + data.moon_phase.sunrise.minute | |
def currentTime = data.moon_phase.current_time.hour + ':' + data.moon_phase.current_time.minute | |
def localData = getWeatherFeature('geolookup', settings.zip as String) | |
def timezone = TimeZone.getTimeZone(localData.location.tz_long) | |
log.debug( "Sunset today is at $sunsetTime" ) | |
log.debug( "Sunrise today is at $sunriseTime" ) | |
unschedule() | |
schedule(timeToday(sunriseTime, timezone), sunrise) | |
schedule(timeToday(sunsetTime, timezone), sunset) | |
schedule(timeTodayAfter(new Date(), '01:00', timezone), initialize) | |
} | |
def sunrise() { | |
changeMode(settings.sunrise_mode) | |
} | |
def sunset() { | |
changeMode(settings.sunset_mode) | |
} | |
def changeMode(newMode) { | |
if (location.mode != newMode) { | |
if (location.modes?.find{it.name == newMode}) { | |
setLocationMode(newMode) | |
log.debug "${label} has changed the mode to '${newMode}'" | |
} | |
else { | |
log.debug "${label} tried to change to undefined mode '${newMode}'" | |
} | |
} | |
} | |
// TODO: implement event handlers |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment