2 * Copyright 2015 SmartThings
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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 namespace: "smartthings",
21 author: "SmartThings",
22 description: "Saves the states of a specified set switches and thermostat setpoints and restores them at each mode change. To use 1) Set the mode, 2) Change switches and setpoint to where you want them for that mode, and 3) Install or update the app. Changing to that mode or touching the app will set the devices to the saved state.",
23 category: "Convenience",
24 iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/light_thermo-switch.png",
25 iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/light_thermo-switch@2x.png"
30 input "switches", "capability.switch", multiple: true, required: false
32 section("Thermostats") {
33 input "thermostats", "capability.thermostat", multiple: true, required: false
36 input "locks", "capability.lock", multiple: true, required: false
41 subscribe(location, changedLocationMode)
42 subscribe(app, appTouch)
48 subscribe(location, changedLocationMode)
49 subscribe(app, appTouch)
55 restoreState(currentMode)
58 def changedLocationMode(evt)
60 restoreState(evt.value)
63 private restoreState(mode)
65 log.info "restoring state for mode '$mode'"
66 def map = state[mode] ?: [:]
68 def value = map[it.id]
69 if (value?.switch == "on") {
70 def level = value.level
72 log.debug "setting $it.label level to $level"
76 log.debug "turning $it.label on"
80 else if (value?.switch == "off") {
81 log.debug "turning $it.label off"
87 def value = map[it.id]
88 if (value?.coolingSetpoint) {
89 log.debug "coolingSetpoint = $value.coolingSetpoint"
90 it.setCoolingSetpoint(value.coolingSetpoint)
92 if (value?.heatingSetpoint) {
93 log.debug "heatingSetpoint = $value.heatingSetpoint"
94 it.setHeatingSetpoint(value.heatingSetpoint)
99 def value = map[it.id]
114 def mode = currentMode
115 def map = state[mode] ?: [:]
118 map[it.id] = [switch: it.currentSwitch, level: it.currentLevel]
122 map[it.id] = [coolingSetpoint: it.currentCoolingSetpoint, heatingSetpoint: it.currentHeatingSetpoint]
126 map[it.id] = [locked: it.currentLock == "locked"]
130 log.debug "saved state for mode ${mode}: ${state[mode]}"
131 log.debug "state: $state"
134 private getCurrentMode()
136 location.mode ?: "_none_"