Update notify-me-with-hue.groovy
[smartapps.git] / official / door-knocker.groovy
1 /**
2  *  Door Knocker
3  *
4  *  Author: brian@bevey.org
5  *  Date: 9/10/13
6  *
7  *  Let me know when someone knocks on the door, but ignore
8  *  when someone is opening the door.
9  */
10
11 definition(
12     name: "Door Knocker",
13     namespace: "imbrianj",
14     author: "brian@bevey.org",
15     description: "Alert if door is knocked, but not opened.",
16     category: "Convenience",
17     iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
18     iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience%402x.png"
19 )
20
21 preferences {
22   section("When Someone Knocks?") {
23     input name: "knockSensor", type: "capability.accelerationSensor", title: "Where?"
24   }
25
26   section("But not when they open this door?") {
27     input name: "openSensor", type: "capability.contactSensor", title: "Where?"
28   }
29
30   section("Knock Delay (defaults to 5s)?") {
31     input name: "knockDelay", type: "number", title: "How Long?", required: false
32   }
33
34   section("Notifications") {
35     input "sendPushMessage", "enum", title: "Send a push notification?", metadata: [values: ["Yes", "No"]], required: false
36     input "phone", "phone", title: "Send a Text Message?", required: false
37   }
38 }
39
40 def installed() {
41   init()
42 }
43
44 def updated() {
45   unsubscribe()
46   init()
47 }
48
49 def init() {
50   state.lastClosed = 0
51   subscribe(knockSensor, "acceleration.active", handleEvent)
52   subscribe(openSensor, "contact.closed", doorClosed)
53 }
54
55 def doorClosed(evt) {
56   state.lastClosed = now()
57 }
58
59 def doorKnock() {
60   if((openSensor.latestValue("contact") == "closed") &&
61      (now() - (60 * 1000) > state.lastClosed)) {
62     log.debug("${knockSensor.label ?: knockSensor.name} detected a knock.")
63     send("${knockSensor.label ?: knockSensor.name} detected a knock.")
64   }
65
66   else {
67     log.debug("${knockSensor.label ?: knockSensor.name} knocked, but looks like it was just someone opening the door.")
68   }
69 }
70
71 def handleEvent(evt) {
72   def delay = knockDelay ?: 5
73   runIn(delay, "doorKnock")
74 }
75
76 private send(msg) {
77   if(sendPushMessage != "No") {
78     log.debug("Sending push message")
79     sendPush(msg)
80   }
81
82   if(phone) {
83     log.debug("Sending text message")
84     sendSms(phone, msg)
85   }
86
87   log.debug(msg)
88 }