Last active
August 29, 2015 14:06
-
-
Save bflorian/5feb80fa68a9aabc5a5c to your computer and use it in GitHub Desktop.
Mode Levels 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
/** | |
* Mode Levels | |
* | |
* Copyright 2014 Bob Florian | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | |
* in compliance with the License. You may obtain a copy of the License at: | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed | |
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License | |
* for the specific language governing permissions and limitations under the License. | |
* | |
*/ | |
definition( | |
name: "Mode Levels", | |
namespace: "bflorian", | |
author: "Bob Florian", | |
description: "Set your lights to a different level for each mode.", | |
category: "Convenience", | |
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png", | |
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png") | |
preferences { | |
page name: "mainPage", title: "Set dimmer levels for each mode", install: true, uninstall: true | |
} | |
def installed() { | |
log.debug "Installed with settings: ${settings}" | |
initialize() | |
} | |
def updated() { | |
log.debug "Updated with settings: ${settings}" | |
unsubscribe() | |
initialize() | |
} | |
def initialize() { | |
subscribe location, modeChange | |
} | |
def mainPage() { | |
dynamicPage(name: "mainPage") { | |
section { | |
input "dimmers", "capability.switchLevel", title: "Select dimmers", multiple: true | |
} | |
section("Specify dimmer level for each mode") { | |
location.modes.each {mode -> | |
input "level_${mode.name}", "number", title: mode.name, required: false | |
} | |
} | |
} | |
} | |
def modeChange(evt) { | |
log.trace "modeChange($evt.value)" | |
def level = settings["level_"+ evt.value] | |
log.trace "level: $level" | |
if (level) { | |
state.onOffStates = dimmers.collectEntries{[it.id, it.currentValue("switch")]} | |
log.trace state.onOffStates | |
dimmers.setLevel(level) | |
log.trace "scheduling 'restoreStates' to run in 10 sec" | |
//runIn(10, restoreStates, [overwrite: false]) | |
restoreStates(10000) | |
} | |
} | |
def restoreStates(delay=0) { | |
log.debug("restoreStates") | |
def map = state.onOffStates | |
dimmers.each {d -> | |
def value = map[d.id] | |
if (value == "off") { | |
d.off(delay: delay) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment