ba2d817e3432d4c7481da99bd4b909bd55496a53
[smartapps.git] / third-party / Switches.groovy
1 /**
2  *  Copyright 2015 Jesse Newland
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  */
14 definition(
15     name: "Hello Home / Mode Switches",
16     namespace: "jnewland",
17     author: "Jesse Newland",
18     description: "Name on/off tiles the same as your Hello Home phrases or Modes",
19     category: "SmartThings Labs",
20     iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
21     iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png",
22     iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png")
23
24 preferences {
25   page(name: "selectSwitches")
26 }
27
28 def selectSwitches() {
29     dynamicPage(name: "selectSwitches", title: "Switches", install: true) {
30         section("Select switches named after Hello Home phrases") {
31             input "switches", "capability.switch", title: "Switches", multiple: true
32         }
33     }
34 }
35
36 def installed() {
37     initialize()
38 }
39
40 def updated() {
41     unsubscribe()
42     initialize()
43 }
44
45 def initialize() {
46   subscribe(switches, "switch", switchHandler)
47 }
48
49 def switchHandler(evt) {
50   def s = switches.find{ evt.deviceId == it.id }
51   def phrase = location.helloHome.getPhrases().find { it.label == s.displayName }
52   if (phrase) {
53     location.helloHome.execute(phrase.label)
54   }
55   def mode = location.modes.find { it.name == s.displayName }
56   if (mode) {
57     setLocationMode(mode)
58   }
59 }