Update hvac-auto-off.smartapp.groovy
[smartapps.git] / third-party / DeviceTamperAlarm.groovy
1 /**
2  *  Device Tamper Alarm
3  *
4  *  Author: Mitch Pond, SmartThings
5  *  Date: 2013-03-20
6  */
7 definition(
8     name: "Device Tamper Alarm",
9     namespace: "mitchpond",
10     author: "Mitch Pond",
11     description: "Receive notification when a device is tampered with. Currently supports Quirky Tripper.",
12     category: "Safety & Security",
13     iconUrl: "https://s3.amazonaws.com/smartapp-icons/Solution/tampering@2x.png",
14     iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Solution/tampering@2x.png",
15     iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Solution/tampering@2x.png"
16 )
17
18 preferences {
19         section("Choose devices..."){
20                 input "contact", "capability.contactSensor", title: "Devices supporting tamper", required: false, multiple: true
21         }
22         section("Via a push notification and/or an SMS message"){
23                 input "phone", "phone", title: "Phone Number (for SMS, optional)", required: false
24                 input "pushAndPhone", "enum", title: "Both Push and SMS?", required: false, options: ["Yes","No"]
25         }
26     section("Sound these alarms..."){
27         input "alarms", "capability.alarm", title: "Alarm Devices", required: false, multiple: true
28     }
29 }
30
31 def installed() {
32         log.debug "Installed with settings: ${settings}"
33         subscribeToEvents()
34 }
35
36 def updated() {
37         log.debug "Updated with settings: ${settings}"
38         unsubscribe()
39         subscribeToEvents()
40 }
41
42 def subscribeToEvents() {
43         subscribe(contact, "tamper.tampered", eventHandler)
44 }
45
46 def eventHandler(evt) {
47         String msg = "${evt.displayName} has been tampered with!"
48     log.debug msg
49     
50         sendMessage(msg)
51     //alarms ?: soundAlarms(alarms)
52     soundAlarms(alarms)
53 }
54
55 private sendMessage(msg) {
56         if (!phone || pushAndPhone != "No") {
57                 log.debug "sending push"
58                 sendPush(msg)
59         }
60         if (phone) {
61                 log.debug "sending SMS"
62                 sendSms(phone, msg)
63         }
64 }
65
66 private soundAlarms(alarms){
67         alarms?.both()
68 }