forked from joelwetzel/Hubitat-Combined-Presence
-
Notifications
You must be signed in to change notification settings - Fork 1
/
combinedPresenceInstance.groovy
178 lines (140 loc) · 4.25 KB
/
combinedPresenceInstance.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* Combined Presence Instance v2.2.1
*
* Copyright 2020 Joel Wetzel
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
* for the specific language governing permissions and limitations under the License.
*
*/
import groovy.time.*
definition(
name: "Combined Presence Boolean Combiner",
parent: "joelwetzel:Combined Presence",
namespace: "joelwetzel",
author: "Joel Wetzel",
description: "This will set a virtual presence sensor to the logical-OR of all the input sensors. It is the child app of Combined Presence.",
category: "Safety & Security",
iconUrl: "",
iconX2Url: "",
iconX3Url: "")
def inputSensors = [
name: "inputSensors",
type: "capability.presenceSensor",
title: "Input Sensors",
description: "The sensors that will be combined with a boolean OR operation.",
multiple: true,
required: true
]
def outputSensor = [
name: "outputSensor",
type: "capability.presenceSensor",
title: "Output Sensor",
description: "The virtual presence sensor that will be controlled by this combiner.",
multiple: false,
required: true
]
def notificationDevice = [
name: "notificationDevice",
type: "capability.notification",
title: "Devices for Notifications",
description: "Send notifications to devices. ie. push notifications to a phone.",
required: false,
multiple: true
]
def notifyAboutStateChanges = [
name: "notifyAboutStateChanges",
type: "bool",
title: "Notify about state changes to the Output sensor",
default: false
]
def enableLogging = [
name: "enableLogging",
type: "bool",
title: "Enable Debug Logging?",
defaultValue: false,
required: true
]
preferences {
page(name: "mainPage", title: "", install: true, uninstall: true) {
section(getFormat("title", "Boolean-OR Combiner")) {
}
section() {
input inputSensors
input outputSensor
}
section(hideable: true, hidden: true, "Notifications") {
input notificationDevice
input notifyAboutStateChanges
paragraph "This will send a notification any time the state of the Output Sensor is changed by Combined Presence."
}
section() {
input enableLogging
}
}
}
def installed() {
log.info "Installed with settings: ${settings}"
initialize()
}
def updated() {
log.info "Updated with settings: ${settings}"
initialize()
}
def initialize() {
unschedule()
unsubscribe()
subscribe(inputSensors, "presence", presenceChangedHandler)
app.updateLabel("Boolean-OR Combiner for ${outputSensor.displayName}")
}
def sendNotification(msg) {
if (msg && msg.size() > 0) {
if (notificationDevice) {
notificationDevice.deviceNotification(msg)
}
}
}
def presenceChangedHandler(evt) {
log "PRESENCE CHANGED for: ${evt.device.name}"
def present = false
inputSensors.each { inputSensor ->
if (inputSensor.currentValue("presence") == "present") {
present = true
}
}
def oldPresent = outputSensor.currentValue("presence")
if (present) {
outputSensor.arrived()
if (oldPresent != "present") {
log "${outputSensor.displayName}.arrived()"
if (notifyAboutStateChanges) {
sendNotification("Arrived: ${outputSensor.displayName}")
}
}
}
else {
outputSensor.departed()
if (oldPresent == "present") {
log "${outputSensor.displayName}.departed()"
if (notifyAboutStateChanges) {
sendNotification("Departed: ${outputSensor.displayName}")
}
}
}
}
def getFormat(type, myText=""){
if(type == "header-green") return "<div style='color:#ffffff;font-weight: bold;background-color:#81BC00;border: 1px solid;box-shadow: 2px 3px #A9A9A9'>${myText}</div>"
if(type == "line") return "\n<hr style='background-color:#1A77C9; height: 1px; border: 0;'></hr>"
if(type == "title") return "<h2 style='color:#1A77C9;font-weight: bold'>${myText}</h2>"
}
def log(msg) {
if (enableLogging) {
log.debug msg
}
}