-
Notifications
You must be signed in to change notification settings - Fork 0
/
Advanced_Broadlink_Remote-Child.groovy
327 lines (278 loc) · 9.04 KB
/
Advanced_Broadlink_Remote-Child.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
* Advance Broadlink Remote Child App
* Project URL: https://github.com/mboisson/Advanced_Broadlink_Remote
* Copyright 2024 Maxime Boissonneault
*
* This app requires it's parent app and device driver to function, please go to the project page for more information.
*
* 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.
*
*/
definition(
name: "Advanced Broadlink Remote Child",
namespace: "mboisson",
author: "Maxime Boissonneault",
description: "Use Broadlink remotes by tomw combined with power sensors to create virtual devices that turn dumb devices into quasi smart ones.",
category: "Green Living",
iconUrl: "https://raw.githubusercontent.com/mboisson/Advanced_Broadlink_Remote/main/Advanced_Broadlink_Remote-logo-small.png",
iconX2Url: "https://raw.githubusercontent.com/mboisson/Advanced_Broadlink_Remote/main/Advanced_Broadlink_Remote-logo.png",
importUrl: "https://raw.githubusercontent.com/mboisson/Advanced_Broadlink_Remote/main/Advanced_Broadlink_Remote-Child.groovy",
parent: "mboisson:Advanced Broadlink Remote Manager"
)
preferences {
page(name: "pageConfig") // Doing it this way elimiates the default app name/mode options.
}
def pageConfig() {
// Let's just set a few things before starting
installed = false
if (!state.deviceID) {
installed = true
}
// Display all options for a new instance of the Advanced Broadlink Remote
dynamicPage(name: "", title: "", install: true, uninstall: true, refreshInterval:0) {
section() {
label title: "Name of new Advanced Broadlink Remote app/device:", required: true
}
section("Select power sensor"){
input "sensor", "capability.powerMeter", title: "Power sensor", multiple: false, required: false
}
section("Select remote to use to control device"){
input "remote", "capability.actuator", title: "Remote", multiple: false
}
section("Delay between commands") {
input (name: "delay", type: "number", title: "When multiple commands are to be sent, how long to wait between them (in milliseconds)", required: true, defaultValue: 1000)
}
section("Log Settings...") {
input (name: "logLevel", type: "enum", title: "Live Logging Level: Messages with this level and higher will be logged", options: [[0: 'Disabled'], [1: 'Error'], [2: 'Warning'], [3: 'Info'], [4: 'Debug'], [5: 'Trace']], defaultValue: 3)
input "logDropLevelTime", "decimal", title: "Drop down to Info Level Minutes", required: true, defaultValue: 5
}
}
}
def installed() {
// Set log level as soon as it's installed to start logging what we do ASAP
int loggingLevel
if (settings.logLevel) {
loggingLevel = settings.logLevel.toInteger()
} else {
loggingLevel = 3
}
logger("trace", "Installed Running Broadlink Remote: $app.label")
// Generate a random DeviceID
state.deviceID = "avtvbr" + Math.abs(new Random().nextInt() % 9999) + 1
//Create Broadlink Remote device
def child
def label = app.getLabel()
logger("info", "Creating Broadlink Remote Child : ${label} with device id: ${state.deviceID}")
try {
child = addChildDevice("mboisson", "Advanced Broadlink Remote Device", state.deviceID, null, [label: label, name: label, completedSetup: true])
} catch(e) {
logger("error", "Error adding Broadlink Remote Child ${label}: ${e}")
}
initialize(child)
}
def updated() {
// Set log level to new value
int loggingLevel
if (settings.logLevel) {
loggingLevel = settings.logLevel.toInteger()
} else {
loggingLevel = 3
}
logger("trace", "Updated Running Broadlink Remote: $app.label")
initialize(getChild())
}
def uninstalled() {
logger("info", "Child Device " + state.deviceID + " removed") // This never shows in the logs, is it because of the way HE deals with the uninstalled method?
deleteChildDevice(state.deviceID)
}
//************************************************************
// initialize
// Set preferences in the associated device and subscribe to the selected sensors and remote device
// Also set logging preferences
//
// Signature(s)
// initialize(child)
//
// Parameters
// child : deviceWrapper
//
// Returns
// None
//
//************************************************************
def initialize(child) {
logger("trace", "Initialize Running Broadlink Remote: $app.label")
// First we need tu unsubscribe and unschedule any previous settings we had
unsubscribe()
unschedule()
// Recheck Log level in case it was changed in the child app
if (settings.logLevel) {
loggingLevel = settings.logLevel.toInteger()
} else {
loggingLevel = 3
}
// Log level was set to a higher level than 3, drop level to 3 in x number of minutes
if (loggingLevel > 3) {
logger("trace", "Initialize runIn $settings.logDropLevelTime")
runIn(settings.logDropLevelTime.toInteger() * 60, logsDropLevel)
}
logger("warn", "App logging level set to $loggingLevel")
logger("trace", "Initialize LogDropLevelTime: $settings.logDropLevelTime")
// Set device settings
child.setLogLevel(loggingLevel)
delay = settings.delay.toInteger()
// Subscribe to the new sensor(s) and device
subscribe(sensor, "power", powerHandler)
subscribe(child, "commandToSend", commandHandler)
}
//************************************************************
// getChild
// Gets current childDeviceWrapper from list of childs
//
// Signature(s)
// getChild()
//
// Parameters
// None
//
// Returns
// ChildDeviceWrapper
//
//************************************************************
def getChild() {
// Does this instance have a DeviceID
if (!state.deviceID){
//No DeviceID available what is going on, has the device been removed?
logger("error", "getChild cannot access deviceID!")
} else {
//We have a deviceID, continue and return ChildDeviceWrapper
logger("trace", "getChild for device " + state.deviceID)
def child = getChildDevices().find {
d -> d.deviceNetworkId.startsWith(state.deviceID)
}
logger("trace","getChild child is ${child}")
return child
}
}
//************************************************************
// powerHandler
// Handles a sensor power change event
// Do not call this directly, only used to handle events
//
// Signature(s)
// powerHandler(evt)
//
// Parameters
// evt : passed by the event subsciption
//
// Returns
// None
//
//************************************************************
def powerHandler(evt)
{
logger("debug", "Power changed to" + evt.doubleValue)
updatePower()
}
//************************************************************
// updatePower
// Update current power based on selected sensors
//
// Signature(s)
// updatePower()
//
// Parameters
// None
//
// Returns
// None
//
//************************************************************
def updatePower() {
def child=getChild()
if (sensor != null) {
child.setPower(sensor.currentValue("power"))
}
return sensor.currentValue("power")
}
def commandHandler(evt) {
sendCommand(evt.value)
}
def sendCommand(commandName)
{
def child = getChild()
if (commandName == "null") { return }
child.logger("trace", "sendCommand:" + commandName)
String[] commands = commandName.split(',')
for (command in commands) {
remote.push(command)
child.setLastCommand(command)
child.resetCommandToSend()
pauseExecution(settings.delay)
}
}
//************************************************************
// logger
// Wrapper function for all logging with level control via preferences
//
// Signature(s)
// logger(String level, String msg)
//
// Parameters
// level : Error level string
// msg : Message to log
//
// Returns
// None
//
//************************************************************
def logger(level, msg) {
switch(level) {
case "error":
if (loggingLevel >= 1) log.error msg
break
case "warn":
if (loggingLevel >= 2) log.warn msg
break
case "info":
if (loggingLevel >= 3) log.info msg
break
case "debug":
if (loggingLevel >= 4) log.debug msg
break
case "trace":
if (loggingLevel >= 5) log.trace msg
break
default:
log.debug msg
break
}
}
//************************************************************
// logsDropLevel
// Turn down logLevel to 3 in this app/device and log the change
//
// Signature(s)
// logsDropLevel()
//
// Parameters
// None
//
// Returns
// None
//
//************************************************************
def logsDropLevel() {
def child=getChild()
app.updateSetting("logLevel",[type:"enum", value:"3"])
child.setLogLevel(3)
loggingLevel = app.getSetting('logLevel').toInteger()
logger("warn","App logging level set to $loggingLevel")
}