Changing remote branch to PLRG Git server.
[smartapps.git] / official / 01-control-lights-and-locks-with-contact-sensor.groovy
1 /**
2  *  Example: Control a switch with a contact sensor
3  *
4  *  Copyright 2014 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
17 definition(
18     name: "Example: Control a switch and lock with a contact sensor",
19     namespace: "com.smarthings.developers",
20     author: "Andrew Mager & Kris Schaller",
21     description: "Using a contact sensor, control a switch and a lock.",
22     category: "My Apps",
23     iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
24     iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png")
25
26
27 // This is where a user will select devices to be used by this SmartApp
28 preferences {
29     // You can create multiple sections to organize the configuration fields of your SmartApp
30     section(title: "Select Devices") {
31         // Inputs assign variables to a group of physical devices
32         input "contact", "capability.contactSensor", title: "Select a contact sensor", multiple: false
33         input "light", "capability.switch", title: "Select a light or outlet", required: true
34     }
35 }
36
37 // This function runs when the SmartApp is installed
38 def installed() {
39     // This is a standard debug statement in Groovy
40     log.debug "Installed with settings: ${settings}"
41     initialize()
42 }
43
44 // This function runs when the SmartApp has been updated
45 def updated() {
46     log.debug "Updated with settings: ${settings}"
47     // Notice that all event subscriptions are removed when a SmartApp is updated
48     unsubscribe()
49     initialize()
50 }
51
52 // This function is where you initialize callbacks for event listeners
53 def initialize() {
54     subscribe(contact, "contact.open", openHandler)
55     subscribe(contact, "contact.closed", closedHandler)
56 }
57
58 // These are our callback methods
59 def openHandler(evt) {
60     log.debug "$evt.name: $evt.value"
61     // Turn the light on
62     light.on()
63 }
64
65 def closedHandler(evt) {
66     log.debug "$evt.name: $evt.value"
67     // Turn the light off and lock the lock
68     light.off()
69 }