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