Update step-notifier.groovy
[smartapps.git] / official / control-switch-with-contact-sensor.groovy
1 /**
2  *  Example: Control a switch with a contact sensor
3  *
4  *  Copyright 2015 Andrew Mager
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: "Example: Control a switch with a contact sensor",
18     namespace: "com.smarthings.dev",
19     author: "Andrew Mager",
20     description: "Using a contact sensor, control a switch.",
21     category: "My Apps",
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 preferences {
27     section("Select Devices") {
28         input "contact", "capability.contactSensor", title: "Select a contact sensor", multiple: false
29         input "light", "capability.switch", title: "Select a light or outlet"
30     }
31 }
32
33 def installed() {
34     log.debug "Installed with settings: ${settings}"
35     initialize()
36 }
37
38 def updated() {
39     log.debug "Updated with settings: ${settings}"
40     unsubscribe()
41     initialize()
42 }
43
44 def initialize() {
45     subscribe contact, "contact.open", openHandler
46     subscribe contact, "contact.closed", closedHandler
47 }
48
49 def openHandler(evt) {
50     log.debug "$evt.name: $evt.value"
51     light.on()
52 }
53
54 def closedHandler(evt) {
55     log.debug "$evt.name: $evt.value"
56     light.off()
57 }
58
59 // TODO: implement event handlers