Update loft.groovy
[smartapps.git] / official / door-jammed-notification.groovy
1 /**
2  *  Door Jammed Notification
3  *
4  *  Copyright 2015 John Rucker
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: "Door Jammed Notification",
19     namespace: "JohnRucker",
20     author: "John.Rucker@Solar-current.com",
21     description: "Sends a SmartThings notification and text messages when your CoopBoss detects a door jam.",
22     category: "My Apps",
23     iconUrl: "http://coopboss.com/images/SmartThingsIcons/coopbossLogo.png",
24     iconX2Url: "http://coopboss.com/images/SmartThingsIcons/coopbossLogo2x.png",
25     iconX3Url: "http://coopboss.com/images/SmartThingsIcons/coopbossLogo3x.png")
26
27 preferences {
28     section("When the door state changes") {
29         paragraph "Send a SmartThings notification when the coop's door jammed and did not close."
30                 input "doorSensor", "capability.doorControl", title: "Select CoopBoss", required: true, multiple: false            
31         input("recipients", "contact", title: "Recipients", description: "Send notifications to") {
32                 input "phone", "phone", title: "Phone number?", required: true}
33         }
34 }
35
36 def installed() {
37         log.debug "Installed with settings: ${settings}"
38         initialize()
39 }
40
41 def updated() {
42         log.debug "Updated with settings: ${settings}"
43         unsubscribe()
44         initialize()
45 }
46
47 def initialize() {
48         subscribe(doorSensor, "doorState", coopDoorStateHandler)
49 }
50
51 def coopDoorStateHandler(evt) {
52         if (evt.value == "jammed"){
53         def msg = "WARNING ${doorSensor.displayName} door is jammed and did not close!"
54         log.debug "WARNING ${doorSensor.displayName} door is jammed and did not close, texting $phone"
55
56         if (location.contactBookEnabled) {
57             sendNotificationToContacts(msg, recipients)
58         }
59         else {
60             sendPush(msg)
61             if (phone) {
62                 sendSms(phone, msg)
63             }
64         }
65         }        
66 }