Update thermostat-mode-director.groovy
[smartapps.git] / official / switch-activates-home-phrase-or-mode.groovy
1 /**
2  *  Switch Activates Home Phrase or Mode
3  *
4  *  Copyright 2015 Michael Struck
5  *  Version 1.0.1 6/20/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 Hello, Home phrase 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 Hello, Home phrases.
18  *  Connect the switch to an IFTTT action, and the Hello, Home phrase will fire with the switch state change.
19  *
20  *
21  */
22 definition(
23     name: "Switch Activates Home Phrase or Mode",
24     namespace: "MichaelStruck",
25     author: "Michael Struck",
26     description: "Ties a Hello, Home phrase or 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
33 preferences {
34         page(name: "getPref")
35 }
36         
37 def getPref() {    
38     dynamicPage(name: "getPref", install:true, uninstall: true) {
39         section("Choose a switch to use...") {
40                         input "controlSwitch", "capability.switch", title: "Switch", multiple: false, required: true
41         }
42    
43         def phrases = location.helloHome?.getPhrases()*.label
44                         if (phrases) {
45                         phrases.sort()
46                                 section("Perform which phrase when...") {
47                                         input "phrase_on", "enum", title: "Switch is on", options: phrases, required: false
48                                         input "phrase_off", "enum", title: "Switch is off", options: phrases, required: false
49                                 }
50                         }
51                 section("Change to which mode when...") {
52                         input "onMode", "mode", title: "Switch is on", required: false
53                         input "offMode", "mode", title: "Switch is off", required: false 
54                 }
55                 section([mobileOnly:true], "Options") {
56                         label(title: "Assign a name", required: false)
57                 mode title: "Set for specific mode(s)", required: false
58                         href "pageAbout", title: "About ${textAppName()}", description: "Tap to get application version, license and instructions"
59                 }
60     }
61 }
62
63 page(name: "pageAbout", title: "About ${textAppName()}") {
64         section {
65             paragraph "${textVersion()}\n${textCopyright()}\n\n${textLicense()}\n"
66         }
67         section("Instructions") {
68             paragraph textHelp()
69         }
70 }
71
72 def installed() {
73         log.debug "Installed with settings: ${settings}"
74         subscribe(controlSwitch, "switch", "switchHandler")
75 }
76
77 def updated() {
78         log.debug "Updated with settings: ${settings}"
79         unsubscribe()
80         subscribe(controlSwitch, "switch", "switchHandler")
81 }
82
83 def switchHandler(evt) {
84         if (evt.value == "on" && (phrase_on || onMode)) {
85         if (phrase_on){
86                 location.helloHome.execute(settings.phrase_on)
87         }
88         if (onMode) {
89                 changeMode(onMode)
90         }
91     } 
92     else if (evt.value == "off" && (phrase_off || offMode)) {
93         if (phrase_off){
94                 location.helloHome.execute(settings.phrase_off)
95         }
96         if (offMode) {
97                 changeMode(offMode)
98         }
99     }
100 }
101
102 def changeMode(newMode) {
103         if (location.mode != newMode) {
104                 if (location.modes?.find{it.name == newMode}) {
105                         setLocationMode(newMode)
106                 } else {
107                         log.debug "Unable to change to undefined mode '${newMode}'"
108                 }
109         }
110 }
111
112 //Version/Copyright/Information/Help
113
114 private def textAppName() {
115         def text = "Switch Activates Home Phrase or Mode"
116 }       
117
118 private def textVersion() {
119     def text = "Version 1.0.1 (06/20/2015)"
120 }
121
122 private def textCopyright() {
123     def text = "Copyright © 2015 Michael Struck"
124 }
125
126 private def textLicense() {
127     def text =
128                 "Licensed under the Apache License, Version 2.0 (the 'License'); "+
129                 "you may not use this file except in compliance with the License. "+
130                 "You may obtain a copy of the License at"+
131                 "\n\n"+
132                 "    http://www.apache.org/licenses/LICENSE-2.0"+
133                 "\n\n"+
134                 "Unless required by applicable law or agreed to in writing, software "+
135                 "distributed under the License is distributed on an 'AS IS' BASIS, "+
136                 "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. "+
137                 "See the License for the specific language governing permissions and "+
138                 "limitations under the License."
139 }
140
141 private def textHelp() {
142         def text =
143         "Ties a Hello, Home phrase or mode to a switch's (virtual or real) on/off state. Perfect for use with IFTTT. "+
144                 "Simple define a switch to be used, then tie the on/off state of the switch to a specific Hello, Home phrases or mode. "+
145                 "Connect the switch to an IFTTT action, and the Hello, Home phrase or mode will fire with the switch state change." 
146 }