-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
233 lines (192 loc) · 9.07 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
//
// Copyright 2017-2018 Chris Gross @ cgHome
//
/* jshint esversion: 6, strict: true, node: true */
"use strict";
const EventEmitter = require('events').EventEmitter;
const i18n = require("i18n");
const PluginName = "homebridge-digitalSTROM";
const PlatformName = "digitalSTROM";
const DigitalStromServer = require('./lib/dss');
const AccessoryFactory = require('./lib/accessories/factory');
let PlatformAccessory, Service, Characteristic, UUIDGen, HKCategories, CustomTypes;
module.exports = homebridge => {
console.log(`***** homebridge API version: ${homebridge.version} *****`);
PlatformAccessory = homebridge.platformAccessory;
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
UUIDGen = homebridge.hap.uuid;
HKCategories = homebridge.hap.Accessory.Categories;
CustomTypes = require('./lib/custom-types')(homebridge);
homebridge.registerPlatform(PluginName, PlatformName, DigitalStromPlatform, true);
};
class DigitalStromPlatform extends EventEmitter {
constructor(log, config, api) {
super();
const platform = this;
this.config = config;
this.log = log;
this.apartment = {};
this.accessories = {};
this.cachedAccessories = {};
this.exclude = ['DummyDevice'].concat(this.config.exclude || []);
this.log.info(`Plugin - ***** ${PluginName} init *****`);
i18n.configure({
locales: ['en', 'de'],
directory: process.env.HOME + '/data/locales',
logDebugFn: msg => {
log.debug(`i18n - ${msg}`)
},
logWarnFn: msg => {
log.warn(`i18n - ${msg}`)
},
logErrorFn: msg => {
log.error(`i18n - ${msg}`)
},
});
i18n.setLocale(this.config.lang || 'en');
// Initialize and start refreshEvent
if (this.config.refreshEvent !== "0") {
setInterval(() => { this.emit('refreshEvent') }, this.config.refreshEvent * 1000);
} else {
this.log.warn('Plugin - RefreshEvent disabled, to enable set refreshEvent > "0" (config.json)');
};
this.dSS = new DigitalStromServer(platform);
this.factory = new AccessoryFactory({ platform, PlatformAccessory, Service, Characteristic, UUIDGen, HKCategories, CustomTypes });
if (api) {
this.api = api;
if (this.api.version < 2.1) {
throw new Error("Unexpected API version.");
};
this.api.on('didFinishLaunching', () => {
this.log.info("Plugin - ***** DidFinishLaunching *****");
this.dSS.initialize();
this.dSS.on('loginSuccessful', () => {
this.initializeAccessories();
});
});
};
}
initializeAccessories() {
this.dSS.getApartment(apartment => {
this.dSS.getDevices(devices => {
this.apartment = apartment;
this.apartment.devices = devices;
this.log.debug('Plugin - ***** Create Devices *****')
this.factory.createDevices(devices);
this.log.debug('Plugin - ***** Create Apartment *****')
this.factory.createApartment(apartment);
this.log.debug('Plugin - ***** Remove unused Accessories *****')
Object.keys(this.cachedAccessories).forEach(UUID => {
const platformAccessory = this.cachedAccessories[UUID];
if (!this.accessories[platformAccessory.UUID]) {
this.log.info(`Plugin - Remove unused Accessory ${platformAccessory.displayName} - ${platformAccessory.UUID}`);
this.api.unregisterPlatformAccessories(PluginName, PlatformName, [platformAccessory]);
delete this.cachedAccessories[platformAccessory.UUID];
};
});
this.log.info(`Plugin - ***** ${Object.keys(this.accessories).length} Accessory registered (HomeKit limits 100 accessory per bridge) *****`)
});
});
}
configureAccessory(accessory) {
this.log.debug(`Plugin - Cached Accessory: ${accessory.displayName} - ${accessory.UUID}`);
accessory.updateReachability(false);
this.cachedAccessories[accessory.UUID] = accessory;
}
addAccessory(accessory) {
if (this.exclude.includes(accessory.constructor.name) || this.exclude.includes(accessory.id)) {
this.log.debug(`Plugin - Excluded Accessory ${accessory.displayName}::${accessory.id} (${accessory.constructor.name}) - ${accessory.UUID}`);
return
};
//* For test's https://testrack2.aizo.com:50443/ ;)
// if (accessory.id.indexOf('DE-3504175fe000000000000000000068d800') === -1) return;
// if (accessory.id !== 'ZS-Wohnen') return;
this.log.info(`Plugin - Add Accessory ${accessory.displayName}::${accessory.id} (${accessory.constructor.name}) - ${accessory.UUID}`)
if (!this.cachedAccessories[accessory.UUID]) {
this.log.info(`Plugin - Register Accessory ${accessory.displayName} (${accessory.constructor.name}) - ${accessory.UUID}`);
this.api.registerPlatformAccessories(PluginName, PlatformName, [accessory.platformAccessory]);
};
this.accessories[accessory.UUID] = accessory;
if (Object.keys(this.accessories).length >= 100) {
throw new Error("HomeKit enforces a limit of 100 accessories per bridge.");
};
return this
}
removeAccessory(accessory) {
this.log.info(`Plugin - Remove Accessory ${accessory.displayName}::${accessory.id} (${accessory.constructor.name}) - ${accessory.UUID}`);
if (this.accessories[accessory.UUID]) {
this.log.info(`Plugin - Unregister Accessory ${accessory.displayName} (${accessory.constructor.name}) - ${accessory.UUID}`);
this.api.unregisterPlatformAccessories(PluginName, PlatformName, [accessory.platformAccessory]);
delete this.accessories[accessory.UUID];
};
}
// see: https://github.com/KhaosT/homebridge-amazondash/blob/master/index.js
configurationRequestHandler(context, request, callback) {
this.log.info("Context: ", JSON.stringify(context));
this.log.info("Request: ", JSON.stringify(request));
// Check the request response
if (request && request.response && request.response.inputs && request.response.inputs.name) {
this.addAccessory(request.response.inputs.name);
// Invoke callback with config will let homebridge save the new config into config.json
// Callback = function(response, type, replace, config)
// set "type" to platform if the plugin is trying to modify platforms section
// set "replace" to true will let homebridge replace existing config in config.json
// "config" is the data platform trying to save
callback(null, "platform", true, { "platform": "digitalSTROM", "otherConfig": "SomeData" });
return;
}
// - UI Type: Input
// Can be used to request input from user
// User response can be retrieved from request.response.inputs next time
// when configurationRequestHandler being invoked
const respDict = {
"type": "Interface",
"interface": "input",
"title": "Add Accessory",
"items": [
{
"id": "name",
"title": "Name",
"placeholder": "Fancy Light"
}//,
// {
// "id": "pw",
// "title": "Password",
// "secure": true
// }
]
};
// - UI Type: List
// Can be used to ask user to select something from the list
// User response can be retrieved from request.response.selections next time
// when configurationRequestHandler being invoked
// var respDict = {
// "type": "Interface",
// "interface": "list",
// "title": "Select Something",
// "allowMultipleSelection": true,
// "items": [
// "A","B","C"
// ]
// }
// - UI Type: Instruction
// Can be used to ask user to do something (other than text input)
// Hero image is base64 encoded image data. Not really sure the maximum length HomeKit allows.
// var respDict = {
// "type": "Interface",
// "interface": "instruction",
// "title": "Almost There",
// "detail": "Please press the button on the bridge to finish the setup.",
// "heroImage": "base64 image data",
// "showActivityIndicator": true,
// "showNextButton": true,
// "buttonText": "Login in browser",
// "actionURL": "https://google.com"
// }
// Plugin can set context to allow it track setup process
context.ts = "Hello";
//invoke callback to update setup UI
callback(respDict);
}
}