-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathtemp2
More file actions
514 lines (419 loc) · 19.3 KB
/
temp2
File metadata and controls
514 lines (419 loc) · 19.3 KB
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
import groovy.json.*;
import org.codehaus.groovy.runtime.metaclass.*;
metadata {
definition(name: "AVR Master Device", namespace: "Denon AVR", author: "Thomas Howard") {
capability "Refresh"
capability "Actuator"
deviceSetup("Main");
}
preferences {
input ("deviceIp", "text", title: "Denon AVR IP Address");
input ("zone2child", "bool", title: "Enable Zone 2 Child Device", defaultValue: zone2child);
}
}
def deviceSetup(device_name){
command "executeCommand",["command"]
deviceCommands = getDeviceCommands();
deviceCommands."$device_name".each{ func, choices ->
if (choices instanceof Map){
def val = false;
//if a capability is defined, add it
if (choices.capability){
capability "${choices.capability}"
} else {
def list = choices.commands.collect{
key, element ->
if (element instanceof Map) {
if (element.val == true) val = true;
return element.name;
}
else return null;
}.findAll { e ->
return (boolean) e;
}
if (val){
params = [
[name: "${func}",
description: "Set the ${device} ${func}",
constraints: list,
type: "ENUM"
],
[name: "value",
description: "Set the ${device} ${func}",
type: "NUMBER"
]
];
} else {
params = [
[name: "${func}",
method: "blah",
description: "Set the ${device} ${func}",
constraints: list,
type: "ENUM"
]
];
}
//Set the command
command "Set${func}", params
attribute "${func}", "string"
}
}
}
}
def getDeviceCommands(){
command_level = [
auto: [command: "AUTO", name: "Auto", val: false, delay: 1],
low: [command: "LOW", name: "Low", val: false, delay: 1],
mid: [command: "MID", name: "Mid", val: false, delay: 1],
high: [command: "HIGH", name: "High", val: false, delay: 1],
off: [command: "OFF", name: "Off", val: false, delay: 1],
];
command_on_standby = [
on: [command: "ON", name: "On", val: false, delay: 10, capability: "Switch", capability_func: "on", capability_value: "on"],
off: [command: "STANDBY", name: "Off", val: false, delay: 1, capability: "Switch", capability_func: "off", capability_value: "off"],
];
command_on_off = [
on: [command: "ON", name: "On", val: false, delay: 1, capability: "Switch", capability_func: "on", capability_value: "on"],
off: [command: "OFF", name: "Off", val: false, delay: 1, capability: "Switch", capability_func: "off", capability_value: "off"],
];
command_mute = [
on: [command: "ON", name: "On", val: false, delay: 1, capability: "AudioVolume", capability_func: "mute", capability_value: "muted"],
off: [command: "OFF", name: "Off", val: false, delay: 1, capability: "AudioVolume", capability_func: "unmute", capability_value: "unmuted"],
];
command_volume = [
up: [command: "UP", name: "Up", val: false, delay: 1, capability: "AudioVolume", capability_func: "volumeUp", capability_value: "volume"],
down: [command: "DOWN", name: "Down", val: false, delay: 1, capability: "AudioVolume", capability_func: "volumeDown", capability_value: "volume"],
set: [command: "", name: "Set", val: true, delay: 1, capability: "AudioVolume", capability_func: "setVolume", capability_value: "volume", capability_vars: 1],
];
command_up_down = [
up: [command: "UP", name: "Up", val: false, delay: 1],
down: [command: "DOWN", name: "Down", val: false, delay: 1],
set: [command: "", name: "Set", val: true, delay: 1],
];
command_input = [
tuner: [command: "TUNER", name: "Tuner", val: false, delay: 1],
dvd: [command: "DVD", name: "DVD", val: false, delay: 1],
blueray: [command: "BD", name: "BlueRay", val: false, delay: 1],
tv: [command: "TV", name: "TV", val: false, delay: 1],
sat_cbl: [command: "SAT/CBL", name: "Satellite/Cable", val: false, delay: 1],
mplay: [command: "MPLAY", name: "Media Player", val: false, delay: 1],
game: [command: "GAME", name: "Game", val: false, delay: 1],
aux: [command: "AUX1", name: "Auxilary", val: false, delay: 1],
network: [command: "NET", name: "Network", val: false, delay: 1],
];
video_select = [
off: [command: "OFF", name: "Off", val: false, delay: 1],
dvd: [command: "DVD", name: "DVD", val: false, delay: 1],
blueray: [command: "BD", name: "BlueRay", val: false, delay: 1],
tv: [command: "TV", name: "TV", val: false, delay: 1],
sat_cbl: [command: "SAT/CBL", name: "Satellite/Cable", val: false, delay: 1],
mplay: [command: "MPLAY", name: "Media Player", val: false, delay: 1],
game: [command: "GAME", name: "Game", val: false, delay: 1],
vaux: [command: "V.AUX", name: "Auxilary", val: false, delay: 1],
network: [command: "NET", name: "Network", val: false, delay: 1],
dock: [command: "DOCK", name: "Dock", val: false, delay: 1],
source: [command: "SOURCE", name: "Source", val: false, delay: 1],
];
command_mode = [
movie: [command: "MOVIE", name: "Movie", val: false, delay: 1],
music: [command: "MUSIC", name: "Music", val: false, delay: 1],
game: [command: "GAME", name: "Game", val: false, delay: 1],
direct: [command: "DIRECT", name: "Direct", val: false, delay: 1],
pure_direct: [command: "PURE DIRECT", name: "Pure Direct", val: false, delay: 1],
stereo: [command: "STEREO", name: "Stereo", val: false, delay: 1],
standard: [command: "STANDARD", name: "Standard", val: false, delay: 1],
dolby_digital:[command: "DOLBY DIGITAL", name: "Dolby Digital", val: false, delay: 1],
dts_surround: [command: "DTS SUROUND", name: "DTS Surround Sound", val: false, delay: 1],
mch_stero: [command: "MCH STEREO", name: "Multi-Channel Stereo", val: false, delay: 1],
];
command_audyssey = [
audyssey: [command: "AUDYSSEY", name: "On", val: false, delay: 1],
byp_lr: [command: "BYP.LR", name: "Bypass", val: false, delay: 1],
flat: [command: "FLAT", name: "Flat", val: false, delay: 1],
manual: [command: "MANUAL", name: "Manual", val: false, delay: 1],
off: [command: "OFF", name: "Off", val: false, delay: 1],
];
requirements_tone = [
[var: "Tone", val: "On"],
[var: "AudioMode", not: "Direct"],
[var: "AudioMode", not: "Pure Direct"]
];
deviceCommands = [
Main:
[
name: "Main",
Power: [
name: "Power", prefix: "PW", status: true,
commands: command_on_standby,
capability: "Switch", capability_var: "switch"
],
Volume: [
name: "Volume", prefix: "MV", status: true,
commands: command_volume,
capability: "AudioVolume", capability_var: "volume"
],
Mute: [
name: "Mute", prefix: "MU", status: true,
commands: command_mute,
capability: "AudioVolume", capability_var: "mute"
],
Input: [
name: "Input", prefix: "SI", status: true,
commands: command_input
],
AudioMode: [
name: "Audio Mode", prefix: "MS", status: true,
commands: command_mode
],
Select: [
name: "Video Select", prefix: "SV", status: true,
commands: video_select
],
Tone: [
name: "Tone Control", prefix: "PSTONE CTRL ", status: true,
commands: command_on_off
],
Bass: [
name: "Bass", prefix: "PSBAS ", status: true,
commands: command_up_down,
requirements: requirements_tone
],
Treble: [
name: "Treble", prefix: "PSTRE ", status: true,
commands: command_up_down,
requirements: requirements_tone
],
LowFrequencyEffect: [
name: "Low Frequency Effect", prefix: "PSLFE ", status: true,
commands: command_up_down
],
DirectChange: [
name: "Direct Change", prefix: "PSDRC ", status: true,
commands: command_level
],
AudysseyMode: [
name: "Audyssey", prefix: "PSMULTEQ:", status: true,
commands: command_audyssey
],
MainZone: [
name: "Main Zone", prefix: "ZM", status: true,
commands: command_on_off
],
DynamicEqualizer: [
name: "Dynamic Equalizer", prefix: "PSDYNEQ ", status: true,
commands: command_on_off,
],
],
Zone2: [
Name: "Zone 2",
Power: [
name: "Power", prefix: "Z2", status: true,
commands: command_on_off,
capability: "Switch", capability_var: "switch"
],
Input: [
name: "Input", prefix: "Z2", status: false,
commands: command_input
],
Volume: [
name: "Volume", prefix: "Z2", status: true,
commands: command_volume,
capability: "AudioVolume", capability_var: "volume"
],
Mute: [
name: "Mute", prefix: "Z2MU", status: true,
commands: command_mute,
capability: "AudioVolume", capability_var: "mute"
],
],
Speakers: [
Front_Left: [name: "Front Left Speaker", prefix: "CVFL ", status: false, commands: command_up_down],
Front_Right: [name: "Front Right Speaker", prefix: "CVFR ", status: false, commands: command_up_down],
Center: [name: "Center Speaker", prefix: "CVC ", status: false, commands: command_up_down],
Subwoofer: [name: "Subwoofer", prefix: "CVSW ", status: false, commands: command_up_down],
Surround_Left: [name: "Surround Left Speaker", prefix: "CVSL ", status: false, commands: command_up_down],
Surround_Right: [name: "Surround Right Speaker", prefix: "CVSR ", status: false, commands: command_up_down],
Surround_Left_Back: [name: "Surround Left Back Speaker", prefix: "CVSBL ", status: false, commands: command_up_down],
Surround_Right_Back: [name: "Surround Right Back Speaker", prefix: "CVSBR ", status: false, commands: command_up_down],
Status: [name: "Speaker Status", prefix: "CV", status: true, commands: ""],
],
];
return deviceCommands;
}
def getString(val){
if (val<10) return "0"+val.toString();
else return val.toString();
}
def executeAVRCommand(delegate, cmd, value, func, dev){
deviceCommands = getDeviceCommands();
def functions = deviceCommands."${dev}"."${func}";
def pre = functions.prefix;
def command = functions.commands.find{key, val-> val.name == cmd}.value;
def match = command.command;
def num_val = command.val;
def command_to_send = "$pre$match";
if (value!=-1 && num_val)
command_to_send += getString(value);
//Test for Validity of Command
sendCommand = true;
if (functions.requirements){
functions.requirements.each {requirement->
def currentState = delegate.device.currentValue("$requirement.var");
def expectedState = requirement.val ? requirement.val : null;
def unexpectedState = requirement.not ? requirement.not : null;
if (expectedState && currentState != expectedState) {
sendCommand = false;
log.debug("Requirement Error: ${requirement.var} must be $expectedState :: detected state = $currentState");
}
if (unexpecteState && currentState == unexpectedState){
sendCommand = false;
log.debug("Requirement Error: ${requirement.var} must NOT be $currentState");
}
}
}
if (sendCommand == true){
executeCommand(command_to_send);
}
}
def setupFunctions(instance, dev){
log.debug("Setting up Functions ${instance}");
deviceCommands = getDeviceCommands();
//Setup the Class Methods
deviceCommands."${dev}".each{ func, choices ->
if (choices instanceof Map) {
def function_name = "Set${func}";
def prefix = choices.prefix;
if (choices.capability){
choices.commands.each{command, opt->
if (opt instanceof Map){
if (choices.capability == opt.capability){
if (opt.capability_vars)
instance.class.metaClass."${opt.capability_func}" << { var1 ->
executeAVRCommand(delegate, "${opt.name}", var1, func, dev);
}
else
instance.class.metaClass."${opt.capability_func}" << {
executeAVRCommand(delegate, "${opt.name}", -1, func, dev);
}
}
}
}
} else {
instance.class.metaClass."${function_name}" << { cmd, value=-1->
executeAVRCommand(delegate, cmd, value, func, dev);
};
}
//instance.metaClass = instance.class.metaClass;
}
}
}
def setupChildDevices(){
def zone2DeviceId = "AVR_ZONE2_ID";
log.debug(device.label);
//Setup Zone 2 Child Device
if (zone2child == true){
child = getChildDevice(zone2DeviceId);
if (!child){
child = addChildDevice("Denon AVR", "AVR Child Device", "AVR_ZONE2_ID", [name: "Denon Zone 2"])
}
//child.updated();
setupFunctions(child, "Zone2");
}
}
def updated(){
log.debug("Initializing Device to IP = $deviceIp");
init(deviceIp, "23");
setupFunctions(this, "Main");
setupChildDevices();
}
def initialized(){
log.debug("Initialized");
updated();
}
def init(ip, port) {
disconnect();
log.debug("Connecting to ${ip}@${port}");
connect(ip, port);
}
def close() {
disconnect()
}
def connect(ip, port) {
telnetConnect([termChars:[13]], ip, port.toInteger(), null, null)
}
def disconnect() {
telnetClose()
}
//commands
def executeCommand(command) {
log.debug("Sending command: $command");
command = command;
sendHubCommand(new hubitat.device.HubAction(command, hubitat.device.Protocol.TELNET))
}
def refresh(){
deviceCommands = getDeviceCommands();
//Setup the Class Methods
deviceCommands.Main.each{ func, choices ->
if (choices instanceof Map){
if (choices.status){
executeCommand("${choices.prefix}?");
}
}
}
}
def getNumbers(input){
return input.findAll("[0-9][0-9]")*.toInteger()
}
def parseResponse(resp){
parseResponseDev(this, "Main", resp);
def children = getChildDevices();
if (children)
children.each{child->
parseResponseDev(child, "Zone2", resp);
}
}
def parseResponseDev(dev, name, resp){
deviceCommands = getDeviceCommands();
deviceCommands."$name".each{ func, choices ->
if (choices instanceof Map) {
if (resp.matches("${choices.prefix}(.*)")){
suffix = resp - choices.prefix;
choices.commands.each{cmd, data->
num = getNumbers(suffix);
if (num!=[] && data.val){
num = num[0];
if (num<10) suffix = suffix-("0"+num.toString());
else suffix = suffix-num.toString();
if (suffix.length() > 0 && suffix[suffix.length()-1]==" "){
suffix = suffix.substring(0, suffix.length()-1);
}
} else {
num = null;
}
if (suffix.matches("${data.command}")){
if (num != null) {
if (choices.capability && data.capability && choices.capability && data.capability)
dev.sendEvent(name: choices.capability_var, value: num);
else
dev.sendEvent(name: "${func}", value: num, description: "${data.name} recieved from AVR");
} else {
if (choices.capability && data.capability && choices.capability && data.capability)
dev.sendEvent(name: choices.capability_var, value: data.capability_value);
else
dev.sendEvent(name: "${func}", value: "${data.name}", description: "${data.name} recieved from AVR");
}
}
}
}
}
}
}
def parse(command) {
log.debug("Got Response: ${command}");
commandReceived = true;
parseResponse(command);
return null;
}
def refreshTimeout(){
log.debug("commandReceived = ${commandReceived}");
}