-
-
Notifications
You must be signed in to change notification settings - Fork 88
/
index.js
75 lines (60 loc) · 2.18 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
let Homebridge;
let Device = require('./lib/device');
let Storage = require('./lib/storage');
const PLUGIN_NAME = 'homebridge-samsung-tizen';
const PLATFORM_NAME = 'SamsungTizen';
module.exports = (homebridge) => {
Homebridge = homebridge;
Homebridge.registerPlatform(PLUGIN_NAME, PLATFORM_NAME, SamsungPlatform, true);
}
class SamsungPlatform {
constructor(log, config, api) {
if (!config) { return; }
this.log = log;
this.api = api;
this.storage = new Storage(api);
this.cachedAccessories = [];
this.config = {
delay : config.delay,
keys : config.keys || {},
inputs : config.inputs || [],
devices : config.devices || [],
switches : config.switches || [],
method : config.method,
refresh : config.refresh,
timeout : config.timeout,
api_key : config.api_key || null
};
if (this.api) {
this.api.on('didFinishLaunching', this.init.bind(this));
}
}
async init() {
await this.storage.init();
for (let device of this.config.devices) {
try {
let mainAccessory = null;
device = new Device(device, this, Homebridge);
for (let index in device.accessories) {
let accessory = device.accessories[index];
if (accessory.type == 'television') {
mainAccessory = accessory;
}
else if (mainAccessory) {
mainAccessory.addAccessory(accessory);
}
}
this.api.publishExternalAccessories(PLUGIN_NAME, [mainAccessory.platformAccessory]);
} catch(error) {
this.log.error(error.message);
this.log.debug(error.stack);
}
}
for (let cachedAccessory of this.cachedAccessories) {
this.api.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [cachedAccessory]);
}
}
configureAccessory(accessory) {
this.cachedAccessories.push(accessory);
}
}