Update WorkingFromHome.groovy
[smartapps.git] / official / switch-changes-mode.groovy
1 /**
2  *  Switch Changes Mode
3  *
4  *  Copyright 2015 Michael Struck
5  *  Version 1.01 3/8/15
6  *
7  *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  *  in compliance with the License. You may obtain a copy of the License at:
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
13  *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
14  *  for the specific language governing permissions and limitations under the License.
15  *
16  *  Ties a mode to a switch's (virtual or real) on/off state. Perfect for use with IFTTT.
17  *  Simple define a switch to be used, then tie the on/off state of the switch to a specific mode.
18  *  Connect the switch to an IFTTT action, and the mode will fire with the switch state change.
19  *
20  *
21  */
22 definition(
23     name: "Switch Changes Mode",
24     namespace: "MichaelStruck",
25     author: "Michael Struck",
26     description: "Ties a mode to a switch's state. Perfect for use with IFTTT.",
27     category: "Convenience",
28     iconUrl: "https://raw.githubusercontent.com/MichaelStruck/SmartThings/master/IFTTT-SmartApps/App1.png",
29     iconX2Url: "https://raw.githubusercontent.com/MichaelStruck/SmartThings/master/IFTTT-SmartApps/App1@2x.png",
30     iconX3Url: "https://raw.githubusercontent.com/MichaelStruck/SmartThings/master/IFTTT-SmartApps/App1@2x.png")
31
32 preferences {
33         page(name: "getPref", title: "Choose Switch and Modes", install:true, uninstall: true) {
34         section("Choose a switch to use...") {
35                         input "controlSwitch", "capability.switch", title: "Switch", multiple: false, required: true
36                 }
37                 section("Change to a new mode when...") {
38                         input "onMode", "mode", title: "Switch is on", required: false
39                         input "offMode", "mode", title: "Switch is off", required: false 
40                 }
41         }
42 }
43
44 def installed() {
45         log.debug "Installed with settings: ${settings}"
46
47         subscribe(controlSwitch, "switch", "switchHandler")
48 }
49
50 def updated() {
51         log.debug "Updated with settings: ${settings}"
52
53         unsubscribe()
54         subscribe(controlSwitch, "switch", "switchHandler")
55 }
56
57 def switchHandler(evt) {
58         if (evt.value == "on") {
59         changeMode(onMode)
60     } else {
61         changeMode(offMode)
62     }
63 }
64
65 def changeMode(newMode) {
66
67         if (newMode && location.mode != newMode) {
68                 if (location.modes?.find{it.name == newMode}) {
69                         setLocationMode(newMode)
70                 }
71                 else {
72                 log.debug "Unable to change to undefined mode '${newMode}'"
73                 }
74         }
75 }
76