Update Hue-Party-Mode.groovy
[smartapps.git] / third-party / AlarmThing-AlertSensor.groovy
1 /**
2  *  AlarmThing Alert Sensor
3  *
4  *  Copyright 2014 ObyCode
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: "AlarmThing Sensor Alert",
18     namespace: "com.obycode",
19     author: "ObyCode",
20     description: "Alert me when there is activity on one or more of my alarm's sensors.",
21     category: "Safety & Security",
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 preferences {
27     page(name: "selectAlarm")
28     page(name: "selectSensors")
29     page(name: "selectStates")
30 }
31
32 def selectAlarm() {
33     dynamicPage(name: "selectAlarm", title: "Configure Alarm", nextPage:"selectSensors", uninstall: true) {
34         section("When there is activity on this alarm...") {
35             input "theAlarm", "capability.alarm", multiple: false, required: true
36         }
37     }
38 }
39
40 def selectSensors() {
41     dynamicPage(name: "selectSensors", title: "Configure Sensors", uninstall: true, nextPage:"selectStates") {
42         def sensors = theAlarm.supportedAttributes*.name
43         if (sensors) {
44             section("On these sensors...") {
45                 input "theSensors", "enum", required: true, multiple:true, metadata:[values:sensors], refreshAfterSelection:true
46             }
47         }
48         section([mobileOnly:true]) {
49             label title: "Assign a name", required: false
50         }
51     }
52 }
53
54 def selectStates() {
55     dynamicPage(name: "selectStates", title: "Which states should trigger a notification?", uninstall: true, install: true) {
56         theSensors.each() {
57             def sensor = it
58             def states = []
59             // TODO: Cannot figure out how to get these possible states, so have to guess them based on the current value
60             switch(theAlarm.currentValue("$it")) {
61             case "active":
62             case "inactive":
63                 states = ["active", "inactive"]
64                 break
65             case "on":
66             case "off":
67                 states = ["on", "off"]
68                 break
69             case "detected":
70             case "clear":
71             case "tested":
72                 states = ["detected", "clear", "tested"]
73                 break
74             case "closed":
75             case "open":
76                 states =  ["closed", "open"]
77                 break
78             default:
79                 log.debug "value not handled: ${theAlarm.currentValue("$sensor")}"
80             }           
81             if (states) {
82                 section() {
83                     input "${sensor}States", "enum", title:"For $sensor...", required: true, multiple:true, metadata:[values:states], refreshAfterSelection:true
84                 }
85             }
86         }
87     }
88 }
89
90 def installed() {
91     log.debug "Installed with settings: ${settings}"
92     initialize()
93 }
94
95 def updated() {
96     log.debug "Updated with settings: ${settings}"
97
98     unsubscribe()
99     initialize()
100 }
101
102 def initialize() {
103     theSensors.each() {
104         def sensor = it
105         settings."${it}States".each() {
106             subscribe(theAlarm, "${sensor}.$it", sensorTriggered)
107         }
108     }
109 }
110
111 def sensorTriggered(evt) {
112     sendPush("Alarm: ${evt.name} is ${evt.value}")
113     log.debug "Alarm: ${evt.name} is ${evt.value}"
114 }