forked from thkl/homebridge-homematic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
598 lines (471 loc) · 18.5 KB
/
index.js
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
'use strict';
var request = require("request");
var HomeMaticRPC = require("./HomeMaticRPC.js").HomeMaticRPC;
var HomeMaticRegaRequest = require("./HomeMaticRegaRequest.js").HomeMaticRegaRequest;
var HomeMaticChannelLoader = require("./HomeMaticChannelLoader.js").HomeMaticChannelLoader;
var inherits = require('util').inherits;
var path = require('path');
var fs = require('fs');
var uuid;
var Service, Characteristic;
module.exports = function(homebridge) {
uuid = homebridge.hap.uuid;
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerPlatform("homebridge-homematic", "HomeMatic", HomeMaticPlatform);
}
function HomeMaticPlatform(log, config) {
this.log = log;
this.uuid = uuid;
this.log("Homematic Plugin Version " + this.getVersion());
this.log("Plugin by thkl https://github.com/thkl");
this.log("Homematic is a registered trademark of the EQ-3 AG");
this.log("Please report any issues to https://github.com/thkl/homebridge-homematic/issues");
this.ccuIP = config["ccu_ip"];
this.filter_device = config["filter_device"];
this.filter_channel = config["filter_channel"];
this.outlets = config["outlets"];
this.iosworkaround = config["ios10"];
this.doors = config["doors"];
this.windows = config["windows"];
this.variables = config["variables"];
this.programs = config["programs"];
this.subsection = config["subsection"];
this.localCache = config["lcache"];
this.vuc = config["variable_update_trigger_channel"];
if ((this.subsection==undefined) || (this.subsection=="")) {
this.log.warn("Uuhhh. There is no value for the key subsection in config.json.");
this.log.warn("There will be no devices fetched from your ccu.");
this.log.warn("Please create a subsection and put in all the channels,");
this.log.warn("you want to import into homekit. Then add the name of that");
this.log.warn("section into your config.json as \"subsection\"=\"....\".");
return;
}
this.sendQueue = [];
this.timer = 0;
this.foundAccessories = [];
this.adressesToQuery = [];
this.config = config;
var port = config["local_port"];
if (port==undefined) {
port = 9090;
}
this.xmlrpc = new HomeMaticRPC(this.log, this.ccuIP, port, 0, this);
this.xmlrpc.init();
if (config["enable_wired"]!=undefined) {
this.xmlrpcwired = new HomeMaticRPC(this.log, this.ccuIP, port+1, 1, this);
this.xmlrpcwired.init();
}
if (config["enable_hmip"]!=undefined) {
this.xmlrpchmip = new HomeMaticRPC(this.log, this.ccuIP, port+2, 2, this);
this.xmlrpchmip.init();
}
var that = this;
process.on("SIGINT", function() {
if (that.xmlrpc.stopping) {
return;
}
that.xmlrpc.stopping = true;
that.xmlrpc.stop();
if (that.xmlrpcwired!=undefined) {
that.xmlrpcwired.stop();
}
if (that.xmlrpchmip!=undefined) {
that.xmlrpchmip.stop();
}
setTimeout(process.exit(0), 2000);
});
process.on("SIGTERM", function() {
if (that.xmlrpc.stopping) {
return;
}
that.xmlrpc.stopping = true;
that.xmlrpc.stop();
if (that.xmlrpcwired!=undefined) {
that.xmlrpcwired.stop();
}
if (that.xmlrpchmip!=undefined) {
that.xmlrpchmip.stop();
}
setTimeout(process.exit(0), 2000);
});
}
HomeMaticPlatform.prototype.accessories = function(callback) {
var that = this;
that.foundAccessories = [];
if ((this.subsection==undefined) || (this.subsection=="")) {
callback(that.foundAccessories);
return;
}
this.log("Fetching Homematic devices...");
var internalconfig = this.internalConfig();
var channelLoader = new HomeMaticChannelLoader(this.log);
channelLoader.init(this.config["services"]);
var script = "string sDeviceId;string sChannelId;boolean df = true;Write(\'{\"devices\":[\');foreach(sDeviceId, root.Devices().EnumIDs()){object oDevice = dom.GetObject(sDeviceId);if(oDevice){var oInterface = dom.GetObject(oDevice.Interface());if(df) {df = false;} else { Write(\',\');}Write(\'{\');Write(\'\"id\": \"\' # sDeviceId # \'\",\');Write(\'\"name\": \"\' # oDevice.Name() # \'\",\');Write(\'\"address\": \"\' # oDevice.Address() # \'\",\');Write(\'\"type\": \"\' # oDevice.HssType() # \'\",\');Write(\'\"channels\": [\');boolean bcf = true;foreach(sChannelId, oDevice.Channels().EnumIDs()){object oChannel = dom.GetObject(sChannelId);if(bcf) {bcf = false;} else {Write(\',\');}Write(\'{\');Write(\'\"cId\": \' # sChannelId # \',\');Write(\'\"name\": \"\' # oChannel.Name() # \'\",\');if(oInterface){Write(\'\"intf\": \"\' # oInterface.Name() # \'\",\');Write(\'\"address\": \"\' # oInterface.Name() #\'.'\ # oChannel.Address() # \'\",\');}Write(\'\"type\": \"\' # oChannel.HssType() # \'\"\');Write(\'}\');}Write(\']}\');}}Write(\']\');";
script = script + "var s = dom.GetObject(\"" ;
script = script + this.subsection;
script = script + "\");string cid;boolean sdf = true;if (s) {Write(\',\"subsection\":[\');foreach(cid, s.EnumUsedIDs()){ ";
script = script +" if(sdf) {sdf = false;}";
script = script +" else { Write(\',\');}Write(cid);}Write(\']\');}";
script = script + "Write('\}'\);";
var localcache = './.homebridge/ccu.json';
var regarequest = new HomeMaticRegaRequest(this.log, this.ccuIP).script(script, function(data) {
var json;
if (data != undefined) {
try {
// read Json
json = JSON.parse(data)
if ((json != undefined) && (json["devices"] != undefined)) {
// seems to be valid json
if (that.localCache != undefined) {
fs.writeFile(localcache, data, function (err) {
if (err) {
that.log.warn('Cannot cache ccu data ',err);
}
that.log('will cache ccu response ...');
});
}
}
} catch (e) {
that.log.warn("Unable to parse live ccu data. Will try cache if there is one");
}
}
// check if we got valid json from ccu
if ((json == undefined) && (that.localCache != undefined)) {
// try to load Data
try {
fs.accessSync(localcache, fs.F_OK);
// try to load Data
data = fs.readFileSync(localcache).toString();
if (data != undefined) {
try {
json = JSON.parse(data)
that.log("loaded ccu data from local cache ... WARNING: your mileage may vary");
} catch (e) {
that.log.warn("Unable to parse cached ccu data. giving up");
}
}
} catch (e) {
that.log.warn("Unable to load cached ccu data. giving up");
}
}
if ((json != undefined) && (json["devices"] !== undefined)) {
json["devices"].map(function(device) {
var cfg = that.deviceInfo(internalconfig,device["type"]);
var isFiltered = false;
if ((that.filter_device !== undefined) && (that.filter_device.indexOf(device.address) > -1)) {
isFiltered = true;
} else {
isFiltered = false;
}
// that.log('device address:', device.address);
if ((device["channels"] !== undefined) && (!isFiltered)) {
device["channels"].map(function(ch) {
var isChannelFiltered = false;
var isSubsectionSelected = false;
// if we have a subsection list check if the channel is here
if (json["subsection"]!=undefined) {
var cin = (json["subsection"].indexOf(ch.cId) > -1);
// if not .. set filter flag
isChannelFiltered = !cin;
isSubsectionSelected = cin;
}
if ((cfg!=undefined) && (cfg["filter"]!=undefined) && (cfg["filter"].indexOf(ch.type)>-1)) {
isChannelFiltered = true;
}
if ((that.filter_channel !== undefined) && (that.filter_channel.indexOf(ch.address) > -1)) {
isChannelFiltered = true;
}
// that.log('name', ch.name, ' -> address:', ch.address);
if ((ch.address !== undefined) && (!isChannelFiltered)) {
// Switch found
// Check if marked as Outlet or Door
var special = undefined;
if ((that.outlets!=undefined) && (that.outlets.indexOf(ch.address) > -1)) {special = "OUTLET";}
if ((that.doors!=undefined) && (that.doors.indexOf(ch.address) > -1)) {special = "DOOR";}
if ((that.windows!=undefined) && (that.windows.indexOf(ch.address) > -1)) {special = "WINDOW";}
// Check if VIRTUAL KEY is Set as Variable Trigger
if ((that.vuc != undefined) && (ch.type=="VIRTUAL_KEY") && (ch.name == that.vuc)) {
that.log('Channel ' + that.vuc + ' added as Variable Update Trigger');
ch.type = "VARIABLE_UPDATE_TRIGGER";
channelLoader.loadChannelService(that.foundAccessories,"VARIABLE_UPDATE_TRIGGER",ch ,that, that.variables ,cfg, Service, Characteristic);
} else {
channelLoader.loadChannelService(that.foundAccessories, device["type"],ch ,that, special ,cfg, Service, Characteristic);
}
} else {
// Channel is in the filter
}
});
} else {
that.log(device.name + " has no channels or is filtered");
}
});
if (that.programs!=undefined) {
that.programs.map(function(program){
var prgtype = ""
if (that.iosworkaround==undefined) {
that.log('Program ' + program + ' added as Program_Launcher');
var ch = {};
var cfg = {};
ch.type = "PROGRAM_LAUNCHER";
ch.address = program;
ch.name = program;
channelLoader.loadChannelService(that.foundAccessories, "PROGRAM_LAUNCHER",ch,that, "PROGRAM",cfg, Service, Characteristic);
} else {
var cfg = that.deviceInfo(internalconfig,"");
that.log('Program ' + program + ' added as SWITCH cause of IOS 10');
var ch = {};
ch.type = "SWITCH";
ch.address = program;
ch.name = program;
channelLoader.loadChannelService(that.foundAccessories, "SWITCH" ,ch ,that, "PROGRAM" ,cfg, Service, Characteristic);
}
});
}
// Add Optional Variables
if (that.variables!=undefined) {
that.variables.map(function(variable) {
var ch = {};
var cfg = {};
ch.type = "VARIABLE";
ch.address = variable;
ch.name = variable;
ch.intf = "Variable";
channelLoader.loadChannelService(that.foundAccessories, "VARIABLE" ,ch ,that, "VARIABLE" ,cfg, Service, Characteristic);
});
}
callback(that.foundAccessories);
} else {
callback(that.foundAccessories);
}
// check number of devices
var noD = that.foundAccessories.length;
that.log("Number of mapped devices : " + noD);
if (noD > 100) {
that.log.warn("********************************************");
that.log.warn("* You are using more than 100 HomeKit *");
that.log.warn("* devices behind a bridge. At this time *");
that.log.warn("* HomeKit only supports up to 100 devices. *");
that.log.warn("* This may end up that iOS is not able to *");
that.log.warn("* connect to the bridge anymore. *");
that.log.warn("********************************************");
} else
if (noD > 90) {
that.log.warn("You are using more than 90 HomeKit");
that.log.warn("devices behind a bridge. At this time");
that.log.warn("HomeKit only supports up to 100 devices.");
that.log.warn("This is just a warning. Everything should");
that.log.warn("work fine until you are below that 100.");
}
});
// Version Check
this.fetch_npmVersion("homebridge-homematic",function(npmVersion){
npmVersion = npmVersion.replace('\n','');
that.log("NPM %s vs Local %s",npmVersion,that.getVersion());
if (npmVersion > that.getVersion()) {
that.log("There is a new Version available. Please update with sudo npm -g update homebridge-homematic");
}
});
}
HomeMaticPlatform.prototype.setValue = function(intf,channel, datapoint, value) {
if (channel != undefined) {
if (intf != undefined) {
var rpc = false;
if (intf == "BidCos-RF") {
rpc = true;
this.xmlrpc.setValue(channel, datapoint, value);
return;
}
if (intf == "BidCos-Wired") {
rpc = true;
if (this.xmlrpcwired!=undefined) {
this.xmlrpcwired.setValue(channel, datapoint, value);
} else {
// Send over Rega
var rega = new HomeMaticRegaRequest(this.log, this.ccuIP);
rega.setValue(channel, datapoint, value);
}
return;
}
if (intf == "HmIP-RF") {
rpc = true;
if (this.xmlrpchmip!=undefined) {
this.xmlrpchmip.setValue(channel, datapoint, value);
} else {
// Send over Rega
var rega = new HomeMaticRegaRequest(this.log, this.ccuIP);
rega.setValue(channel, datapoint, value);
}
return;
}
if (intf == "Variable") {
var rega = new HomeMaticRegaRequest(this.log, this.ccuIP);
rega.setVariable(channel, value);
rpc = true;
return;
}
// Rega Fallback
if (rpc == false) {
var rega = new HomeMaticRegaRequest(this.log, this.ccuIP);
rega.setValue(channel, datapoint, value);
return;
}
} else {
// Undefined Interface -> Rega should know how to deal with it
var rega = new HomeMaticRegaRequest(this.log, this.ccuIP);
rega.setValue(channel, datapoint, value);
return;
}
}
}
HomeMaticPlatform.prototype.remoteSetValue = function(channel,datapoint,value) {
var that = this;
this.foundAccessories.map(function(accessory) {
if ((accessory.adress == channel) || ((accessory.cadress != undefined) && (accessory.cadress == channel))) {
accessory.event(datapoint, value);
}
});
return;
}
HomeMaticPlatform.prototype.setRegaValue = function(channel, datapoint, value) {
var rega = new HomeMaticRegaRequest(this.log, this.ccuIP);
rega.setValue(channel, datapoint, value);
return;
}
HomeMaticPlatform.prototype.sendRegaCommand = function(command,callback) {
var rega = new HomeMaticRegaRequest(this.log, this.ccuIP);
var that = this;
rega.script(command, function(data) {
if (callback!=undefined) {
callback(data);
}
});
return;
}
HomeMaticPlatform.prototype.getValue = function(intf,channel, datapoint, callback) {
if (channel != undefined) {
if (intf != undefined) {
var rpc = false;
if (intf == "BidCos-RF") {
this.xmlrpc.getValue(channel, datapoint, callback);
rpc = true;
return;
}
if (intf == "BidCos-Wired") {
rpc = true;
if (this.xmlrpcwired!=undefined) {
this.xmlrpcwired.getValue(channel, datapoint, callback);
} else {
// Send over Rega
var rega = new HomeMaticRegaRequest(this.log, this.ccuIP);
rega.getValue(channel, datapoint, callback);
}
return;
}
if (intf == "HmIP-RF") {
if (this.xmlrpchmip!=undefined) {
this.xmlrpchmip.getValue(channel, datapoint, callback);
} else {
// Send over Rega
var rega = new HomeMaticRegaRequest(this.log, this.ccuIP);
rega.getValue(channel, datapoint, callback);
}
return;
}
if (intf == "Variable") {
var rega = new HomeMaticRegaRequest(this.log, this.ccuIP);
rega.getVariable(channel, callback);
rpc = true;
return;
}
// Fallback to Rega
if (rpc == false) {
var rega = new HomeMaticRegaRequest(this.log, this.ccuIP);
rega.getValue(channel, datapoint, callback);
return;
}
} else {
// Undefined Interface -> Rega should know how to deal with it
var rega = new HomeMaticRegaRequest(this.log, this.ccuIP);
rega.getValue(channel, datapoint, callback);
return;
}
}
}
HomeMaticPlatform.prototype.prepareRequest = function(accessory, script) {
var that = this;
this.sendQueue.push(script);
that.delayed(100);
}
HomeMaticPlatform.prototype.sendPreparedRequests = function() {
var that = this;
var script = "var d;";
this.sendQueue.map(function(command) {
script = script + command;
});
this.sendQueue = [];
var regarequest = new HomeMaticRegaRequest(this.log, this.ccuIP).script(script, function(data) {});
}
HomeMaticPlatform.prototype.sendRequest = function(accessory, script, callback) {
var regarequest = new HomeMaticRegaRequest(this.log, this.ccuIP).script(script, function(data) {
if (data !== undefined) {
try {
var json = JSON.parse(data);
callback(json);
} catch (err) {
callback(undefined);
}
return;
}
});
}
HomeMaticPlatform.prototype.delayed = function(delay) {
var timer = this.delayed[delay];
if (timer) {
this.log("removing old command");
clearTimeout(timer);
}
var that = this;
this.delayed[delay] = setTimeout(function() {
clearTimeout(that.delayed[delay]);
that.sendPreparedRequests();
}, delay ? delay : 100);
this.log("New Timer was set");
}
HomeMaticPlatform.prototype.deviceInfo = function(config,devicetype) {
var cfg = undefined;
if (config != undefined) {
var di = config["deviceinfo"];
di.map(function(device) {
if (device["type"]==devicetype) {
cfg = device;
}
});
}
return cfg;
}
HomeMaticPlatform.prototype.internalConfig = function() {
try {
var config_path = path.join(__dirname, './internalconfig.json');
var config = JSON.parse(fs.readFileSync(config_path));
return config;
}
catch (err) {
throw err;
}
return undefined
}
HomeMaticPlatform.prototype.getVersion = function() {
var pjPath = path.join(__dirname, './package.json');
var pj = JSON.parse(fs.readFileSync(pjPath));
return pj.version;
}
HomeMaticPlatform.prototype.fetch_npmVersion = function(pck, callback) {
var exec = require('child_process').exec;
var cmd = 'npm view '+pck+' version';
exec(cmd, function(error, stdout, stderr) {
var npm_version = stdout;
npm_version = npm_version.replace('\n','');
callback(npm_version);
});
}