Update turn-on-by-zip-code.groovy
[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
25 def installed() {
26     initialize()
27 }
28
29 def updated() {
30     unsubscribe()
31     initialize()
32 }
33
34 def initialize() {
35   subscribe(switches, "switch", switchHandler)
36 }
37
38 def switchHandler(evt) {
39   def s = switches.find{ evt.deviceId == it.id }
40   def phrase = location.helloHome.getPhrases().find { it.label == s.displayName }
41   if (phrase) {
42     location.helloHome.execute(phrase.label)
43   }
44   def mode = location.modes.find { it.name == s.displayName }
45   if (mode) {
46     setLocationMode(mode)
47   }
48 }
49
50 preferences {
51   page(name: selectSwitches)
52 }
53
54 def selectSwitches() {
55     dynamicPage(name: "selectSwitches", title: "Switches", install: true) {
56         section("Select switches named after Hello Home phrases") {
57             input "switches", "capability.switch", title: "Switches", multiple: true
58         }
59     }
60 }