Update BetterLaundryMonitor.groovy
[smartapps.git] / third-party / garage-switch.groovy
1 /**
2  *  Control garage with switch
3  *
4  *  Copyright 2014 ObyCode
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7  *  in compliance with the License. You may obtain a copy of the License at:
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
12  *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
13  *  for the specific language governing permissions and limitations under the License.
14  *
15  */
16 definition(
17     name: "Control garage with switch",
18     namespace: "com.obycode",
19     author: "ObyCode",
20     description: "Use a z-wave light switch to control your garage. When the switch is pressed down, the garage door will close (if its not already), and likewise, it will open when up is pressed on the switch. Additionally, the indicator light on the switch will tell you if the garage door is open or closed.",
21     category: "Convenience",
22     iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
23     iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png"
24 )
25
26
27 preferences {
28     section("Use this switch...") {
29         input "theSwitch", "capability.switch", multiple: false, required: true
30     }
31     section("to control this garage door...") {
32         input "theOpener", "capability.momentary", multiple: false, required: true
33     }
34     section("whose status is given by this sensor...") {
35         input "theSensor", "capability.threeAxis", multiple: false, required: true
36     }
37 }
38
39 def installed() {
40     log.debug "Installed with settings: ${settings}"
41
42     initialize()
43 }
44
45 def updated() {
46     log.debug "Updated with settings: ${settings}"
47
48     unsubscribe()
49     initialize()
50 }
51
52 def initialize() {
53     subscribe(theSwitch, "switch", switchHit)
54     subscribe(theSensor, "status", statusChanged)
55 }
56
57 def switchHit(evt) {
58     log.debug "in switchHit: " + evt.value
59     def current = theSensor.currentState("status")
60     if (evt.value == "on") {
61         if (current.value == "closed") { 
62             theOpener.push()
63         }
64     } else {
65         if (current.value == "open") {
66             theOpener.push()
67         }
68     }
69 }
70
71 def statusChanged(evt) {
72     if (evt.value == "open") {
73         theSwitch.on()
74     } else if (evt.value == "closed") {
75         theSwitch.off()
76     }
77 }