Update groveStreams.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 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         section("Change to this mode") {
34                     input "newMode", "mode", title: "Mode?"
35             }
36     }
37 }
38
39 def installed() {
40     initialize()
41 }
42
43 def updated() {
44     unsubscribe()
45     initialize()
46 }
47
48 def initialize() {
49   subscribe(switches, "switch", switchHandler)
50 }
51
52 def switchHandler(evt) {
53   def s = switches.find{ evt.deviceId == it.id }
54   def phrase = location.helloHome.getPhrases().find { it.label == s.displayName }
55   if (phrase) {
56     location.helloHome.execute(phrase.label)
57   }
58   // This looks like a bug because device's displayName has nothing to do with modes
59   //def mode = location.modes.find { it.name == s.displayName }
60   if (newMode) {
61     setLocationMode(newMode)
62   }
63 }