Update notify-me-with-hue.groovy
[smartapps.git] / official / make-it-so.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  *  Make it So
14  *
15  *  Author: SmartThings
16  *  Date: 2013-03-06
17  */
18 definition(
19     name: "Make It So",
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"
26 )
27
28 preferences {
29         section("Switches") {
30                 input "switches", "capability.switch", multiple: true, required: false
31         }
32         section("Thermostats") {
33                 input "thermostats", "capability.thermostat", multiple: true, required: false
34         }
35         section("Locks") {
36                 input "locks", "capability.lock", multiple: true, required: false
37         }
38 }
39
40 def installed() {
41         subscribe(location, changedLocationMode)
42         subscribe(app, appTouch)
43         saveState()
44 }
45
46 def updated() {
47         unsubscribe()
48         subscribe(location, changedLocationMode)
49         subscribe(app, appTouch)
50         saveState()
51 }
52
53 def appTouch(evt)
54 {
55         restoreState(currentMode)
56 }
57
58 def changedLocationMode(evt)
59 {
60         restoreState(evt.value)
61 }
62
63 private restoreState(mode)
64 {
65         log.info "restoring state for mode '$mode'"
66         def map = state[mode] ?: [:]
67         switches?.each {
68                 def value = map[it.id]
69                 if (value?.switch == "on") {
70                         def level = value.level
71                         if (level) {
72                                 log.debug "setting $it.label level to $level"
73                                 it.setLevel(level)
74                         }
75                         else {
76                                 log.debug "turning $it.label on"
77                                 it.on()
78                         }
79                 }
80                 else if (value?.switch == "off") {
81                         log.debug "turning $it.label off"
82                         it.off()
83                 }
84         }
85
86         thermostats?.each {
87                 def value = map[it.id]
88                 if (value?.coolingSetpoint) {
89                         log.debug "coolingSetpoint = $value.coolingSetpoint"
90                         it.setCoolingSetpoint(value.coolingSetpoint)
91                 }
92                 if (value?.heatingSetpoint) {
93                         log.debug "heatingSetpoint = $value.heatingSetpoint"
94                         it.setHeatingSetpoint(value.heatingSetpoint)
95                 }
96         }
97
98         locks?.each {
99                 def value = map[it.id]
100                 if (value) {
101                         if (value?.locked) {
102                                 it.lock()
103                         }
104                         else {
105                                 it.unlock()
106                         }
107                 }
108         }
109 }
110
111
112 private saveState()
113 {
114         def mode = currentMode
115         def map = state[mode] ?: [:]
116
117         switches?.each {
118                 map[it.id] = [switch: it.currentSwitch, level: it.currentLevel]
119         }
120
121         thermostats?.each {
122                 map[it.id] = [coolingSetpoint: it.currentCoolingSetpoint, heatingSetpoint: it.currentHeatingSetpoint]
123         }
124
125         locks?.each {
126                 map[it.id] = [locked: it.currentLock == "locked"]
127         }
128
129         state[mode] = map
130         log.debug "saved state for mode ${mode}: ${state[mode]}"
131         log.debug "state: $state"
132 }
133
134 private getCurrentMode()
135 {
136         location.mode ?: "_none_"
137 }