@switchbot/homebridge-switchbot 1.15.1-beta.0 → 1.15.1-beta.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/ISSUE_TEMPLATE/bug-report.yml +1 -0
- package/.github/ISSUE_TEMPLATE/feature-request.yml +1 -0
- package/.github/ISSUE_TEMPLATE/support-request.yml +1 -0
- package/.prettierrc +1 -1
- package/CHANGELOG.md +9 -0
- package/config.schema.json +11 -4
- package/dist/device/bot.d.ts +8 -7
- package/dist/device/bot.d.ts.map +1 -1
- package/dist/device/bot.js +104 -28
- package/dist/device/bot.js.map +1 -1
- package/dist/device/colorbulb.d.ts +8 -7
- package/dist/device/colorbulb.d.ts.map +1 -1
- package/dist/device/colorbulb.js +223 -58
- package/dist/device/colorbulb.js.map +1 -1
- package/dist/device/contact.d.ts +3 -3
- package/dist/device/contact.d.ts.map +1 -1
- package/dist/device/contact.js +54 -11
- package/dist/device/contact.js.map +1 -1
- package/dist/device/curtain.d.ts +9 -8
- package/dist/device/curtain.d.ts.map +1 -1
- package/dist/device/curtain.js +102 -28
- package/dist/device/curtain.js.map +1 -1
- package/dist/device/humidifier.d.ts +8 -7
- package/dist/device/humidifier.d.ts.map +1 -1
- package/dist/device/humidifier.js +187 -66
- package/dist/device/humidifier.js.map +1 -1
- package/dist/device/lock.d.ts +8 -7
- package/dist/device/lock.d.ts.map +1 -1
- package/dist/device/lock.js +98 -22
- package/dist/device/lock.js.map +1 -1
- package/dist/device/meter.d.ts +4 -4
- package/dist/device/meter.d.ts.map +1 -1
- package/dist/device/meter.js +55 -12
- package/dist/device/meter.js.map +1 -1
- package/dist/device/meterplus.d.ts +3 -3
- package/dist/device/meterplus.d.ts.map +1 -1
- package/dist/device/meterplus.js +53 -10
- package/dist/device/meterplus.js.map +1 -1
- package/dist/device/motion.d.ts +3 -3
- package/dist/device/motion.d.ts.map +1 -1
- package/dist/device/motion.js +53 -13
- package/dist/device/motion.js.map +1 -1
- package/dist/device/plug.d.ts +8 -7
- package/dist/device/plug.d.ts.map +1 -1
- package/dist/device/plug.js +100 -24
- package/dist/device/plug.js.map +1 -1
- package/dist/device/striplight.d.ts +8 -7
- package/dist/device/striplight.d.ts.map +1 -1
- package/dist/device/striplight.js +186 -47
- package/dist/device/striplight.js.map +1 -1
- package/dist/irdevice/airconditioner.d.ts +7 -6
- package/dist/irdevice/airconditioner.d.ts.map +1 -1
- package/dist/irdevice/airconditioner.js +40 -6
- package/dist/irdevice/airconditioner.js.map +1 -1
- package/dist/irdevice/airpurifier.d.ts +7 -6
- package/dist/irdevice/airpurifier.d.ts.map +1 -1
- package/dist/irdevice/airpurifier.js +46 -11
- package/dist/irdevice/airpurifier.js.map +1 -1
- package/dist/irdevice/camera.d.ts +7 -6
- package/dist/irdevice/camera.d.ts.map +1 -1
- package/dist/irdevice/camera.js +40 -6
- package/dist/irdevice/camera.js.map +1 -1
- package/dist/irdevice/fan.d.ts +8 -7
- package/dist/irdevice/fan.d.ts.map +1 -1
- package/dist/irdevice/fan.js +40 -6
- package/dist/irdevice/fan.js.map +1 -1
- package/dist/irdevice/light.d.ts +7 -6
- package/dist/irdevice/light.d.ts.map +1 -1
- package/dist/irdevice/light.js +40 -6
- package/dist/irdevice/light.js.map +1 -1
- package/dist/irdevice/other.d.ts +7 -6
- package/dist/irdevice/other.d.ts.map +1 -1
- package/dist/irdevice/other.js +40 -6
- package/dist/irdevice/other.js.map +1 -1
- package/dist/irdevice/tv.d.ts +8 -7
- package/dist/irdevice/tv.d.ts.map +1 -1
- package/dist/irdevice/tv.js +40 -6
- package/dist/irdevice/tv.js.map +1 -1
- package/dist/irdevice/vacuumcleaner.d.ts +7 -6
- package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
- package/dist/irdevice/vacuumcleaner.js +40 -6
- package/dist/irdevice/vacuumcleaner.js.map +1 -1
- package/dist/irdevice/waterheater.d.ts +7 -6
- package/dist/irdevice/waterheater.d.ts.map +1 -1
- package/dist/irdevice/waterheater.js +40 -6
- package/dist/irdevice/waterheater.js.map +1 -1
- package/dist/platform.d.ts +10 -6
- package/dist/platform.d.ts.map +1 -1
- package/dist/platform.js +211 -93
- package/dist/platform.js.map +1 -1
- package/dist/settings.d.ts +4 -21
- package/dist/settings.d.ts.map +1 -1
- package/dist/settings.js +3 -3
- package/dist/settings.js.map +1 -1
- package/package.json +12 -12
- package/src/device/bot.ts +104 -37
- package/src/device/colorbulb.ts +227 -95
- package/src/device/contact.ts +53 -14
- package/src/device/curtain.ts +105 -38
- package/src/device/humidifier.ts +189 -84
- package/src/device/lock.ts +97 -30
- package/src/device/meter.ts +55 -16
- package/src/device/meterplus.ts +52 -13
- package/src/device/motion.ts +52 -16
- package/src/device/plug.ts +101 -33
- package/src/device/striplight.ts +189 -68
- package/src/irdevice/airconditioner.ts +44 -9
- package/src/irdevice/airpurifier.ts +52 -17
- package/src/irdevice/camera.ts +46 -11
- package/src/irdevice/fan.ts +50 -15
- package/src/irdevice/light.ts +48 -13
- package/src/irdevice/other.ts +46 -11
- package/src/irdevice/tv.ts +56 -21
- package/src/irdevice/vacuumcleaner.ts +46 -11
- package/src/irdevice/waterheater.ts +46 -11
- package/src/platform.ts +232 -107
- package/src/settings.ts +4 -25
package/dist/platform.js
CHANGED
|
@@ -24,9 +24,12 @@ const airpurifier_1 = require("./irdevice/airpurifier");
|
|
|
24
24
|
const waterheater_1 = require("./irdevice/waterheater");
|
|
25
25
|
const vacuumcleaner_1 = require("./irdevice/vacuumcleaner");
|
|
26
26
|
const airconditioner_1 = require("./irdevice/airconditioner");
|
|
27
|
-
const
|
|
28
|
-
const
|
|
27
|
+
const https_1 = __importDefault(require("https"));
|
|
28
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
29
|
+
const buffer_1 = require("buffer");
|
|
29
30
|
const fakegato_history_1 = __importDefault(require("fakegato-history"));
|
|
31
|
+
const fs_1 = require("fs");
|
|
32
|
+
const settings_1 = require("./settings");
|
|
30
33
|
/**
|
|
31
34
|
* HomebridgePlatform
|
|
32
35
|
* This class is the main constructor for your plugin, this is where you should
|
|
@@ -41,9 +44,6 @@ class SwitchBotPlatform {
|
|
|
41
44
|
this.Characteristic = this.api.hap.Characteristic;
|
|
42
45
|
// this is used to track restored cached accessories
|
|
43
46
|
this.accessories = [];
|
|
44
|
-
this.axios = axios_1.default.create({
|
|
45
|
-
responseType: 'json',
|
|
46
|
-
});
|
|
47
47
|
this.version = require('../package.json').version || '1.12.8'; // eslint-disable-line @typescript-eslint/no-var-requires
|
|
48
48
|
this.logs();
|
|
49
49
|
this.debugLog('Finished initializing platform:', this.config.name);
|
|
@@ -67,12 +67,6 @@ class SwitchBotPlatform {
|
|
|
67
67
|
}
|
|
68
68
|
return;
|
|
69
69
|
}
|
|
70
|
-
// setup axios interceptor to add headers / api key to each request
|
|
71
|
-
this.axios.interceptors.request.use((request) => {
|
|
72
|
-
request.headers.Authorization = this.config.credentials?.openToken;
|
|
73
|
-
request.headers['Content-Type'] = 'application/json; charset=utf8';
|
|
74
|
-
return request;
|
|
75
|
-
});
|
|
76
70
|
// import fakegato-history module
|
|
77
71
|
this.fakegatoAPI = (0, fakegato_history_1.default)(api);
|
|
78
72
|
// When this event is fired it means Homebridge has restored all cached accessories from disk.
|
|
@@ -83,7 +77,12 @@ class SwitchBotPlatform {
|
|
|
83
77
|
this.debugLog('Executed didFinishLaunching callback');
|
|
84
78
|
// run the method to discover / register your devices as accessories
|
|
85
79
|
try {
|
|
86
|
-
this.
|
|
80
|
+
if (this.config.credentials?.openToken && !this.config.credentials.token) {
|
|
81
|
+
await this.updateToken();
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
this.discoverDevices();
|
|
85
|
+
}
|
|
87
86
|
}
|
|
88
87
|
catch (e) {
|
|
89
88
|
this.errorLog('Failed to Discover Devices.', JSON.stringify(e.message));
|
|
@@ -126,7 +125,7 @@ class SwitchBotPlatform {
|
|
|
126
125
|
/**
|
|
127
126
|
* Verify the config passed to the plugin is valid
|
|
128
127
|
*/
|
|
129
|
-
verifyConfig() {
|
|
128
|
+
async verifyConfig() {
|
|
130
129
|
this.config.options = this.config.options || {};
|
|
131
130
|
const platformConfig = {};
|
|
132
131
|
if (this.config.options.logging) {
|
|
@@ -169,8 +168,8 @@ class SwitchBotPlatform {
|
|
|
169
168
|
}
|
|
170
169
|
}
|
|
171
170
|
}
|
|
172
|
-
if (this.config.options.refreshRate <
|
|
173
|
-
throw new Error('Refresh Rate must be above
|
|
171
|
+
if (this.config.options.refreshRate < 5) {
|
|
172
|
+
throw new Error('Refresh Rate must be above 5 (5 seconds).');
|
|
174
173
|
}
|
|
175
174
|
if (!this.config.options.refreshRate) {
|
|
176
175
|
// default 120 seconds (2 minutes)
|
|
@@ -190,12 +189,64 @@ class SwitchBotPlatform {
|
|
|
190
189
|
this.debugLog('Missing Credentials');
|
|
191
190
|
}
|
|
192
191
|
}
|
|
193
|
-
if (!this.config.credentials?.
|
|
192
|
+
if (!this.config.credentials?.token) {
|
|
194
193
|
if (this.platformLogging?.includes('debug')) {
|
|
195
|
-
this.errorLog('Missing
|
|
194
|
+
this.errorLog('Missing token');
|
|
196
195
|
this.warnLog('Cloud Enabled SwitchBot Devices & IR Devices will not work');
|
|
197
196
|
}
|
|
198
197
|
}
|
|
198
|
+
if (this.config.credentials?.token) {
|
|
199
|
+
if (!this.config.credentials?.secret) {
|
|
200
|
+
if (this.platformLogging?.includes('debug')) {
|
|
201
|
+
this.errorLog('Missing secret');
|
|
202
|
+
this.warnLog('Cloud Enabled SwitchBot Devices & IR Devices will not work');
|
|
203
|
+
}
|
|
204
|
+
throw Error('Missing secret');
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* The openToken was old config.
|
|
211
|
+
* This method saves the openToken as the token in the config.json file
|
|
212
|
+
* @param this.config.credentials.openToken
|
|
213
|
+
*/
|
|
214
|
+
async updateToken() {
|
|
215
|
+
try {
|
|
216
|
+
// check the new token was provided
|
|
217
|
+
if (!this.config.credentials?.openToken) {
|
|
218
|
+
throw new Error('New token not provided');
|
|
219
|
+
}
|
|
220
|
+
// load in the current config
|
|
221
|
+
const currentConfig = JSON.parse((0, fs_1.readFileSync)(this.api.user.configPath(), 'utf8'));
|
|
222
|
+
// check the platforms section is an array before we do array things on it
|
|
223
|
+
if (!Array.isArray(currentConfig.platforms)) {
|
|
224
|
+
throw new Error('Cannot find platforms array in config');
|
|
225
|
+
}
|
|
226
|
+
// find this plugins current config
|
|
227
|
+
const pluginConfig = currentConfig.platforms.find((x) => x.platform === settings_1.PLATFORM_NAME);
|
|
228
|
+
if (!pluginConfig) {
|
|
229
|
+
throw new Error(`Cannot find config for ${settings_1.PLATFORM_NAME} in platforms array`);
|
|
230
|
+
}
|
|
231
|
+
// check the .credentials is an object before doing object things with it
|
|
232
|
+
if (typeof pluginConfig.credentials !== 'object') {
|
|
233
|
+
throw new Error('pluginConfig.credentials is not an object');
|
|
234
|
+
}
|
|
235
|
+
// Move openToken to token
|
|
236
|
+
this.warnLog('This plugin has been updated to use OpenAPI v1.1, config is set with openToken.');
|
|
237
|
+
this.warnLog('`openToken` config has been moved to the `token` config, Please restart Homebridge.');
|
|
238
|
+
// set the refresh token
|
|
239
|
+
pluginConfig.credentials.token = this.config.credentials?.openToken;
|
|
240
|
+
pluginConfig.credentials.openToken = 'Delete';
|
|
241
|
+
if (this.platformLogging?.includes('debug')) {
|
|
242
|
+
this.warnLog(`Token: ${pluginConfig.credentials.token}`);
|
|
243
|
+
}
|
|
244
|
+
// save the config, ensuring we maintain pretty json
|
|
245
|
+
(0, fs_1.writeFileSync)(this.api.user.configPath(), JSON.stringify(currentConfig, null, 4));
|
|
246
|
+
this.verifyConfig();
|
|
247
|
+
}
|
|
248
|
+
catch (e) {
|
|
249
|
+
this.errorLog(e);
|
|
199
250
|
}
|
|
200
251
|
}
|
|
201
252
|
/**
|
|
@@ -203,85 +254,123 @@ class SwitchBotPlatform {
|
|
|
203
254
|
*/
|
|
204
255
|
async discoverDevices() {
|
|
205
256
|
try {
|
|
206
|
-
if (this.config.credentials?.
|
|
207
|
-
const
|
|
208
|
-
|
|
209
|
-
this.
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
257
|
+
if (this.config.credentials?.token) {
|
|
258
|
+
const t = Date.now();
|
|
259
|
+
const nonce = 'requestID';
|
|
260
|
+
const data = this.config.credentials?.token + t + nonce;
|
|
261
|
+
const signTerm = crypto_1.default.createHmac('sha256', this.config.credentials?.secret).update(buffer_1.Buffer.from(data, 'utf-8')).digest();
|
|
262
|
+
const sign = signTerm.toString('base64');
|
|
263
|
+
this.debugLog(sign);
|
|
264
|
+
const options = {
|
|
265
|
+
hostname: settings_1.HostDomain,
|
|
266
|
+
port: 443,
|
|
267
|
+
path: settings_1.DevicePath,
|
|
268
|
+
method: 'GET',
|
|
269
|
+
headers: {
|
|
270
|
+
'Authorization': this.config.credentials?.token,
|
|
271
|
+
'sign': sign,
|
|
272
|
+
'nonce': nonce,
|
|
273
|
+
't': t,
|
|
274
|
+
'Content-Type': 'application/json',
|
|
275
|
+
},
|
|
276
|
+
};
|
|
277
|
+
const req = https_1.default.request(options, (res) => {
|
|
278
|
+
this.debugLog(`statusCode: ${res.statusCode}`);
|
|
279
|
+
this.debugLog(`headers: ${JSON.stringify(res.headers)}`);
|
|
280
|
+
let rawData = '';
|
|
281
|
+
res.on('data', (d) => {
|
|
282
|
+
rawData += d;
|
|
283
|
+
this.debugLog(`d: ${d}`);
|
|
284
|
+
});
|
|
285
|
+
res.on('end', () => {
|
|
286
|
+
try {
|
|
287
|
+
const devicesAPI = JSON.parse(rawData);
|
|
288
|
+
this.debugLog(`devicesAPI: ${JSON.stringify(devicesAPI.body)}`);
|
|
289
|
+
// SwitchBot Devices
|
|
290
|
+
if (devicesAPI.body.deviceList.length !== 0) {
|
|
291
|
+
this.infoLog(`Total SwitchBot Devices Found: ${devicesAPI.body.deviceList.length}`);
|
|
225
292
|
}
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
293
|
+
else {
|
|
294
|
+
this.debugLog(`Total SwitchBot Devices Found: ${devicesAPI.body.deviceList.length}`);
|
|
295
|
+
}
|
|
296
|
+
const deviceLists = devicesAPI.body.deviceList;
|
|
297
|
+
if (!this.config.options?.devices) {
|
|
298
|
+
this.debugLog(`SwitchBot Device Config Not Set: ${JSON.stringify(this.config.options?.devices)}`);
|
|
299
|
+
const devices = deviceLists.map((v) => v);
|
|
300
|
+
for (const device of devices) {
|
|
301
|
+
if (device.deviceType) {
|
|
302
|
+
if (device.configDeviceName) {
|
|
303
|
+
device.deviceName = device.configDeviceName;
|
|
304
|
+
}
|
|
305
|
+
this.createDevice(device);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
else if (this.config.credentials?.token && this.config.options.devices) {
|
|
310
|
+
this.debugLog(`SwitchBot Device Config Set: ${JSON.stringify(this.config.options?.devices)}`);
|
|
311
|
+
const deviceConfigs = this.config.options?.devices;
|
|
312
|
+
const mergeBydeviceId = (a1, a2) => a1.map((itm) => ({
|
|
313
|
+
...a2.find((item) => item.deviceId.toUpperCase().replace(/[^A-Z0-9]+/g, '') === itm.deviceId.toUpperCase().replace(/[^A-Z0-9]+/g, '') && item),
|
|
314
|
+
...itm,
|
|
315
|
+
}));
|
|
316
|
+
const devices = mergeBydeviceId(deviceLists, deviceConfigs);
|
|
317
|
+
this.debugLog(`SwitchBot Devices: ${JSON.stringify(devices)}`);
|
|
318
|
+
for (const device of devices) {
|
|
319
|
+
if (device.deviceType) {
|
|
320
|
+
if (device.configDeviceName) {
|
|
321
|
+
device.deviceName = device.configDeviceName;
|
|
322
|
+
}
|
|
323
|
+
this.createDevice(device);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
else {
|
|
328
|
+
this.errorLog('SwitchBot Token Supplied, Issue with Auth.');
|
|
329
|
+
}
|
|
330
|
+
// IR Devices
|
|
331
|
+
if (devicesAPI.body.infraredRemoteList.length !== 0) {
|
|
332
|
+
this.infoLog(`Total IR Devices Found: ${devicesAPI.body.infraredRemoteList.length}`);
|
|
333
|
+
}
|
|
334
|
+
else {
|
|
335
|
+
this.debugLog(`Total IR Devices Found: ${devicesAPI.body.infraredRemoteList.length}`);
|
|
336
|
+
}
|
|
337
|
+
const irDeviceLists = devicesAPI.body.infraredRemoteList;
|
|
338
|
+
if (!this.config.options?.irdevices) {
|
|
339
|
+
this.debugLog(`IR Device Config Not Set: ${JSON.stringify(this.config.options?.irdevices)}`);
|
|
340
|
+
const devices = irDeviceLists.map((v) => v);
|
|
341
|
+
for (const device of devices) {
|
|
342
|
+
if (device.remoteType) {
|
|
343
|
+
this.createIRDevice(device);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
else {
|
|
348
|
+
this.debugLog(`IR Device Config Set: ${JSON.stringify(this.config.options?.irdevices)}`);
|
|
349
|
+
const irDeviceConfig = this.config.options?.irdevices;
|
|
350
|
+
const mergeIRBydeviceId = (a1, a2) => a1.map((itm) => ({
|
|
351
|
+
...a2.find((item) => item.deviceId.toUpperCase().replace(/[^A-Z0-9]+/g, '') === itm.deviceId.toUpperCase().replace(/[^A-Z0-9]+/g, '') && item),
|
|
352
|
+
...itm,
|
|
353
|
+
}));
|
|
354
|
+
const devices = mergeIRBydeviceId(irDeviceLists, irDeviceConfig);
|
|
355
|
+
this.debugLog(`IR Devices: ${JSON.stringify(devices)}`);
|
|
356
|
+
for (const device of devices) {
|
|
357
|
+
if (device.remoteType) {
|
|
358
|
+
this.createIRDevice(device);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
243
361
|
}
|
|
244
|
-
this.createDevice(device);
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
else {
|
|
249
|
-
this.errorLog('Neither SwitchBot OpenToken or Device Config are not set.');
|
|
250
|
-
}
|
|
251
|
-
// IR Devices
|
|
252
|
-
if (devicesAPI.body.infraredRemoteList.length !== 0) {
|
|
253
|
-
this.infoLog(`Total IR Devices Found: ${devicesAPI.body.infraredRemoteList.length}`);
|
|
254
|
-
}
|
|
255
|
-
else {
|
|
256
|
-
this.debugLog(`Total IR Devices Found: ${devicesAPI.body.infraredRemoteList.length}`);
|
|
257
|
-
}
|
|
258
|
-
const irDeviceLists = devicesAPI.body.infraredRemoteList;
|
|
259
|
-
if (!this.config.options?.irdevices) {
|
|
260
|
-
this.debugLog(`IR Device Config Not Set: ${JSON.stringify(this.config.options?.irdevices)}`);
|
|
261
|
-
const devices = irDeviceLists.map((v) => v);
|
|
262
|
-
for (const device of devices) {
|
|
263
|
-
if (device.remoteType) {
|
|
264
|
-
this.createIRDevice(device);
|
|
265
362
|
}
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
else {
|
|
269
|
-
this.debugLog(`IR Device Config Set: ${JSON.stringify(this.config.options?.irdevices)}`);
|
|
270
|
-
const irDeviceConfig = this.config.options?.irdevices;
|
|
271
|
-
const mergeIRBydeviceId = (a1, a2) => a1.map((itm) => ({
|
|
272
|
-
...a2.find((item) => item.deviceId.toUpperCase().replace(/[^A-Z0-9]+/g, '') === itm.deviceId.toUpperCase().replace(/[^A-Z0-9]+/g, '') && item),
|
|
273
|
-
...itm,
|
|
274
|
-
}));
|
|
275
|
-
const devices = mergeIRBydeviceId(irDeviceLists, irDeviceConfig);
|
|
276
|
-
this.debugLog(`IR Devices: ${JSON.stringify(devices)}`);
|
|
277
|
-
for (const device of devices) {
|
|
278
|
-
if (device.remoteType) {
|
|
279
|
-
this.createIRDevice(device);
|
|
363
|
+
catch (e) {
|
|
364
|
+
this.errorLog(e.message);
|
|
280
365
|
}
|
|
281
|
-
}
|
|
282
|
-
}
|
|
366
|
+
});
|
|
367
|
+
});
|
|
368
|
+
req.on('error', (error) => {
|
|
369
|
+
this.errorLog(error);
|
|
370
|
+
});
|
|
371
|
+
req.end();
|
|
283
372
|
}
|
|
284
|
-
else if (!this.config.credentials?.
|
|
373
|
+
else if (!this.config.credentials?.token && this.config.options?.devices) {
|
|
285
374
|
this.debugLog(`SwitchBot Device Manual Config Set: ${JSON.stringify(this.config.options?.devices)}`);
|
|
286
375
|
const deviceConfigs = this.config.options?.devices;
|
|
287
376
|
const devices = deviceConfigs.map((v) => v);
|
|
@@ -294,7 +383,7 @@ class SwitchBotPlatform {
|
|
|
294
383
|
}
|
|
295
384
|
}
|
|
296
385
|
else {
|
|
297
|
-
this.errorLog('Neither SwitchBot
|
|
386
|
+
this.errorLog('Neither SwitchBot Token or Device Config are not set.');
|
|
298
387
|
}
|
|
299
388
|
}
|
|
300
389
|
catch (e) {
|
|
@@ -1579,7 +1668,36 @@ class SwitchBotPlatform {
|
|
|
1579
1668
|
async deviceInfo(device) {
|
|
1580
1669
|
if (this.platformLogging?.includes('debug')) {
|
|
1581
1670
|
this.warnLog(JSON.stringify(device));
|
|
1582
|
-
|
|
1671
|
+
const t = Date.now();
|
|
1672
|
+
const nonce = 'requestID';
|
|
1673
|
+
const data = this.config.credentials?.token + t + nonce;
|
|
1674
|
+
const signTerm = crypto_1.default.createHmac('sha256', this.config.credentials?.secret).update(buffer_1.Buffer.from(data, 'utf-8')).digest();
|
|
1675
|
+
const sign = signTerm.toString('base64');
|
|
1676
|
+
this.debugLog(sign);
|
|
1677
|
+
const options = {
|
|
1678
|
+
hostname: settings_1.HostDomain,
|
|
1679
|
+
port: 443,
|
|
1680
|
+
path: `${settings_1.DevicePath}/${device.deviceId}/status`,
|
|
1681
|
+
method: 'GET',
|
|
1682
|
+
headers: {
|
|
1683
|
+
Authorization: this.config.credentials?.token,
|
|
1684
|
+
sign: sign,
|
|
1685
|
+
nonce: nonce,
|
|
1686
|
+
t: t,
|
|
1687
|
+
'Content-Type': 'application/json',
|
|
1688
|
+
},
|
|
1689
|
+
};
|
|
1690
|
+
const req = https_1.default.request(options, (res) => {
|
|
1691
|
+
this.debugLog(`statusCode: ${res.statusCode}`);
|
|
1692
|
+
res.on('data', (d) => {
|
|
1693
|
+
process.stdout.write(d);
|
|
1694
|
+
});
|
|
1695
|
+
});
|
|
1696
|
+
req.on('error', (error) => {
|
|
1697
|
+
this.errorLog(error);
|
|
1698
|
+
});
|
|
1699
|
+
req.end();
|
|
1700
|
+
this.deviceStatus = req;
|
|
1583
1701
|
if (this.deviceStatus.message === 'success') {
|
|
1584
1702
|
this.warnLog(`${device.deviceName} deviceInfoStatus: ${JSON.stringify(this.deviceStatus)}`);
|
|
1585
1703
|
}
|