Update speaker-weather-forecast.groovy
[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 definition(
17     name: "Example: Control a switch and lock with a contact sensor",
18     namespace: "com.smarthings.developers",
19     author: "Andrew Mager & Kris Schaller",
20     description: "Using a contact sensor, control a switch and a lock.",
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 // This is where a user will select devices to be used by this SmartApp
27 preferences {
28     // You can create multiple sections to organize the configuration fields of your SmartApp
29     section(title: "Select Devices") {
30         // Inputs assign variables to a group of physical devices
31         input "contact", "capability.contactSensor", title: "Select a contact sensor", multiple: false
32         input "light", "capability.switch", title: "Select a light or outlet", required: true
33     }
34 }
35
36 // This function runs when the SmartApp is installed
37 def installed() {
38     // This is a standard debug statement in Groovy
39     log.debug "Installed with settings: ${settings}"
40     initialize()
41 }
42
43 // This function runs when the SmartApp has been updated
44 def updated() {
45     log.debug "Updated with settings: ${settings}"
46     // Notice that all event subscriptions are removed when a SmartApp is updated
47     unsubscribe()
48     initialize()
49 }
50
51 // This function is where you initialize callbacks for event listeners
52 def initialize() {
53     subscribe(contact, "contact.open", openHandler)
54     subscribe(contact, "contact.closed", closedHandler)
55 }
56
57 // These are our callback methods
58 def openHandler(evt) {
59     log.debug "$evt.name: $evt.value"
60     // Turn the light on
61     light.on()
62 }
63
64 def closedHandler(evt) {
65     log.debug "$evt.name: $evt.value"
66     // Turn the light off and lock the lock
67     light.off()
68 }