@switchbot/homebridge-switchbot 2.6.0-beta.4 → 2.6.0
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/.vscode/tasks.json +13 -0
- package/CHANGELOG.md +4 -2
- package/README.md +18 -9
- package/dist/device/blindtilt.d.ts +1 -5
- package/dist/device/blindtilt.d.ts.map +1 -1
- package/dist/device/blindtilt.js +80 -140
- package/dist/device/blindtilt.js.map +1 -1
- package/dist/device/bot.d.ts +1 -5
- package/dist/device/bot.d.ts.map +1 -1
- package/dist/device/bot.js +79 -138
- package/dist/device/bot.js.map +1 -1
- package/dist/device/ceilinglight.d.ts +2 -6
- package/dist/device/ceilinglight.d.ts.map +1 -1
- package/dist/device/ceilinglight.js +157 -304
- package/dist/device/ceilinglight.js.map +1 -1
- package/dist/device/colorbulb.d.ts +2 -6
- package/dist/device/colorbulb.d.ts.map +1 -1
- package/dist/device/colorbulb.js +164 -311
- package/dist/device/colorbulb.js.map +1 -1
- package/dist/device/contact.d.ts +1 -5
- package/dist/device/contact.d.ts.map +1 -1
- package/dist/device/contact.js +36 -72
- package/dist/device/contact.js.map +1 -1
- package/dist/device/curtain.d.ts +1 -5
- package/dist/device/curtain.d.ts.map +1 -1
- package/dist/device/curtain.js +78 -138
- package/dist/device/curtain.js.map +1 -1
- package/dist/device/hub.d.ts +2 -6
- package/dist/device/hub.d.ts.map +1 -1
- package/dist/device/hub.js +30 -63
- package/dist/device/hub.js.map +1 -1
- package/dist/device/humidifier.d.ts +1 -5
- package/dist/device/humidifier.d.ts.map +1 -1
- package/dist/device/humidifier.js +115 -228
- package/dist/device/humidifier.js.map +1 -1
- package/dist/device/iosensor.d.ts +1 -5
- package/dist/device/iosensor.d.ts.map +1 -1
- package/dist/device/iosensor.js +38 -74
- package/dist/device/iosensor.js.map +1 -1
- package/dist/device/{striplight.d.ts → lightstrip.d.ts} +2 -10
- package/dist/device/lightstrip.d.ts.map +1 -0
- package/dist/device/{striplight.js → lightstrip.js} +138 -194
- package/dist/device/lightstrip.js.map +1 -0
- package/dist/device/lock.d.ts +1 -5
- package/dist/device/lock.d.ts.map +1 -1
- package/dist/device/lock.js +66 -126
- package/dist/device/lock.js.map +1 -1
- package/dist/device/meter.d.ts +1 -5
- package/dist/device/meter.d.ts.map +1 -1
- package/dist/device/meter.js +38 -74
- package/dist/device/meter.js.map +1 -1
- package/dist/device/meterplus.d.ts +1 -5
- package/dist/device/meterplus.d.ts.map +1 -1
- package/dist/device/meterplus.js +39 -75
- package/dist/device/meterplus.js.map +1 -1
- package/dist/device/motion.d.ts +1 -5
- package/dist/device/motion.d.ts.map +1 -1
- package/dist/device/motion.js +35 -72
- package/dist/device/motion.js.map +1 -1
- package/dist/device/plug.d.ts +1 -5
- package/dist/device/plug.d.ts.map +1 -1
- package/dist/device/plug.js +65 -126
- package/dist/device/plug.js.map +1 -1
- package/dist/device/robotvacuumcleaner.d.ts +1 -5
- package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
- package/dist/device/robotvacuumcleaner.js +79 -151
- package/dist/device/robotvacuumcleaner.js.map +1 -1
- package/dist/irdevice/airconditioner.d.ts +3 -7
- package/dist/irdevice/airconditioner.d.ts.map +1 -1
- package/dist/irdevice/airconditioner.js +34 -62
- package/dist/irdevice/airconditioner.js.map +1 -1
- package/dist/irdevice/airpurifier.d.ts +3 -7
- package/dist/irdevice/airpurifier.d.ts.map +1 -1
- package/dist/irdevice/airpurifier.js +31 -59
- package/dist/irdevice/airpurifier.js.map +1 -1
- package/dist/irdevice/camera.d.ts +1 -5
- package/dist/irdevice/camera.d.ts.map +1 -1
- package/dist/irdevice/camera.js +30 -57
- package/dist/irdevice/camera.js.map +1 -1
- package/dist/irdevice/fan.d.ts +2 -6
- package/dist/irdevice/fan.d.ts.map +1 -1
- package/dist/irdevice/fan.js +39 -66
- package/dist/irdevice/fan.js.map +1 -1
- package/dist/irdevice/light.d.ts +1 -5
- package/dist/irdevice/light.d.ts.map +1 -1
- package/dist/irdevice/light.js +32 -59
- package/dist/irdevice/light.js.map +1 -1
- package/dist/irdevice/other.d.ts +1 -5
- package/dist/irdevice/other.d.ts.map +1 -1
- package/dist/irdevice/other.js +30 -57
- package/dist/irdevice/other.js.map +1 -1
- package/dist/irdevice/tv.d.ts +3 -7
- package/dist/irdevice/tv.d.ts.map +1 -1
- package/dist/irdevice/tv.js +46 -76
- package/dist/irdevice/tv.js.map +1 -1
- package/dist/irdevice/vacuumcleaner.d.ts +2 -6
- package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
- package/dist/irdevice/vacuumcleaner.js +30 -58
- package/dist/irdevice/vacuumcleaner.js.map +1 -1
- package/dist/irdevice/waterheater.d.ts +7 -23
- package/dist/irdevice/waterheater.d.ts.map +1 -1
- package/dist/irdevice/waterheater.js +41 -74
- package/dist/irdevice/waterheater.js.map +1 -1
- package/dist/platform.d.ts +14 -5
- package/dist/platform.d.ts.map +1 -1
- package/dist/platform.js +156 -149
- package/dist/platform.js.map +1 -1
- package/dist/settings.d.ts +4 -0
- package/dist/settings.d.ts.map +1 -1
- package/dist/settings.js +5 -1
- package/dist/settings.js.map +1 -1
- package/nodemon.json +1 -1
- package/package.json +10 -10
- package/src/device/blindtilt.ts +122 -181
- package/src/device/bot.ts +96 -149
- package/src/device/ceilinglight.ts +172 -315
- package/src/device/colorbulb.ts +178 -324
- package/src/device/contact.ts +48 -78
- package/src/device/curtain.ts +93 -148
- package/src/device/hub.ts +34 -63
- package/src/device/humidifier.ts +135 -243
- package/src/device/iosensor.ts +49 -79
- package/src/device/{striplight.ts → lightstrip.ts} +148 -200
- package/src/device/lock.ts +78 -134
- package/src/device/meter.ts +48 -78
- package/src/device/meterplus.ts +50 -80
- package/src/device/motion.ts +46 -77
- package/src/device/plug.ts +76 -134
- package/src/device/robotvacuumcleaner.ts +91 -159
- package/src/irdevice/airconditioner.ts +56 -81
- package/src/irdevice/airpurifier.ts +41 -67
- package/src/irdevice/camera.ts +35 -60
- package/src/irdevice/fan.ts +46 -71
- package/src/irdevice/light.ts +38 -63
- package/src/irdevice/other.ts +35 -60
- package/src/irdevice/tv.ts +52 -80
- package/src/irdevice/vacuumcleaner.ts +36 -62
- package/src/irdevice/waterheater.ts +46 -76
- package/src/platform.ts +144 -158
- package/src/settings.ts +5 -0
- package/.github/npm-version-script.js +0 -81
- package/dist/device/striplight.d.ts.map +0 -1
- package/dist/device/striplight.js.map +0 -1
|
@@ -1,14 +1,11 @@
|
|
|
1
|
-
import https from 'https';
|
|
2
|
-
import crypto from 'crypto';
|
|
3
1
|
import { Context } from 'vm';
|
|
4
|
-
import {
|
|
5
|
-
import
|
|
2
|
+
import { request } from 'undici';
|
|
3
|
+
import { sleep } from '../utils';
|
|
6
4
|
import { interval, Subject } from 'rxjs';
|
|
7
5
|
import { SwitchBotPlatform } from '../platform';
|
|
8
6
|
import { debounceTime, skipWhile, take, tap } from 'rxjs/operators';
|
|
7
|
+
import { device, devicesConfig, deviceStatus, switchbot, hs2rgb, rgb2hs, m2hs, serviceData, ad, Devices } from '../settings';
|
|
9
8
|
import { Service, PlatformAccessory, CharacteristicValue, ControllerConstructor, Controller, ControllerServiceMap } from 'homebridge';
|
|
10
|
-
import { device, devicesConfig, deviceStatus, switchbot, hs2rgb, rgb2hs, m2hs, serviceData, ad, HostDomain, DevicePath } from '../settings';
|
|
11
|
-
import { sleep } from '../utils';
|
|
12
9
|
|
|
13
10
|
/**
|
|
14
11
|
* Platform Accessory
|
|
@@ -177,7 +174,7 @@ export class CeilingLight {
|
|
|
177
174
|
this.accessory.context.adaptiveLighting = true;
|
|
178
175
|
this.debugLog(
|
|
179
176
|
`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLighting: ${this.accessory.context.adaptiveLighting},` +
|
|
180
|
-
|
|
177
|
+
` adaptiveLightingShift: ${this.adaptiveLightingShift}`,
|
|
181
178
|
);
|
|
182
179
|
}
|
|
183
180
|
|
|
@@ -206,7 +203,7 @@ export class CeilingLight {
|
|
|
206
203
|
} catch (e: any) {
|
|
207
204
|
this.apiError(e);
|
|
208
205
|
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with ${this.device.connectionType} Connection,`
|
|
209
|
-
|
|
206
|
+
+ ` Error Message: ${JSON.stringify(e.message)}`);
|
|
210
207
|
}
|
|
211
208
|
this.ceilingLightUpdateInProgress = false;
|
|
212
209
|
});
|
|
@@ -237,7 +234,7 @@ export class CeilingLight {
|
|
|
237
234
|
} else {
|
|
238
235
|
await this.offlineOff();
|
|
239
236
|
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
|
|
240
|
-
|
|
237
|
+
+ ` ${this.device.connectionType}, parseStatus will not happen.`);
|
|
241
238
|
}
|
|
242
239
|
}
|
|
243
240
|
|
|
@@ -271,15 +268,15 @@ export class CeilingLight {
|
|
|
271
268
|
|
|
272
269
|
// Color, Hue & Brightness
|
|
273
270
|
if (this.color) {
|
|
274
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} color: ${
|
|
271
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} color: ${JSON.stringify(this.color)}`);
|
|
275
272
|
const [red, green, blue] = this.color!.split(':');
|
|
276
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} red: ${
|
|
277
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} green: ${
|
|
278
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} blue: ${
|
|
273
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} red: ${JSON.stringify(red)}`);
|
|
274
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} green: ${JSON.stringify(green)}`);
|
|
275
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} blue: ${JSON.stringify(blue)}`);
|
|
279
276
|
|
|
280
277
|
const [hue, saturation] = rgb2hs(Number(red), Number(green), Number(blue));
|
|
281
278
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
|
|
282
|
-
|
|
279
|
+
+ ` hs: ${JSON.stringify(rgb2hs(Number(red), Number(green), Number(blue)))}`);
|
|
283
280
|
|
|
284
281
|
// Hue
|
|
285
282
|
this.Hue = hue;
|
|
@@ -315,7 +312,7 @@ export class CeilingLight {
|
|
|
315
312
|
} else {
|
|
316
313
|
await this.offlineOff();
|
|
317
314
|
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
|
|
318
|
-
|
|
315
|
+
+ ` ${this.device.connectionType}, refreshStatus will not happen.`);
|
|
319
316
|
}
|
|
320
317
|
}
|
|
321
318
|
|
|
@@ -341,8 +338,8 @@ export class CeilingLight {
|
|
|
341
338
|
switchbot.onadvertisement = async (ad: any) => {
|
|
342
339
|
this.address = ad.address;
|
|
343
340
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},`
|
|
344
|
-
|
|
345
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${
|
|
341
|
+
+ ` BLE Address Found: ${this.address}`);
|
|
342
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
|
|
346
343
|
this.serviceData = ad.serviceData;
|
|
347
344
|
//this.state = ad.serviceData.state;
|
|
348
345
|
//this.delay = ad.serviceData.delay;
|
|
@@ -351,7 +348,7 @@ export class CeilingLight {
|
|
|
351
348
|
//this.wifiRssi = ad.serviceData.wifiRssi;
|
|
352
349
|
//this.overload = ad.serviceData.overload;
|
|
353
350
|
//this.currentPower = ad.serviceData.currentPower;
|
|
354
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${
|
|
351
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
|
|
355
352
|
/*this.debugLog(
|
|
356
353
|
`${this.device.deviceType}: ${this.accessory.displayName} state: ${ad.serviceData.state}, ` +
|
|
357
354
|
`delay: ${ad.serviceData.delay}, timer: ${ad.serviceData.timer}, syncUtcTime: ${ad.serviceData.syncUtcTime} ` +
|
|
@@ -371,13 +368,13 @@ export class CeilingLight {
|
|
|
371
368
|
return await sleep(this.scanDuration * 1000);
|
|
372
369
|
})
|
|
373
370
|
.then(async () => {
|
|
374
|
-
|
|
371
|
+
// Stop to monitor
|
|
375
372
|
await this.stopScanning(switchbot);
|
|
376
373
|
})
|
|
377
374
|
.catch(async (e: any) => {
|
|
378
375
|
this.apiError(e);
|
|
379
376
|
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}`
|
|
380
|
-
|
|
377
|
+
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
381
378
|
await this.BLERefreshConnection(switchbot);
|
|
382
379
|
});
|
|
383
380
|
} else {
|
|
@@ -388,56 +385,27 @@ export class CeilingLight {
|
|
|
388
385
|
async openAPIRefreshStatus() {
|
|
389
386
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
|
|
390
387
|
try {
|
|
391
|
-
const
|
|
392
|
-
|
|
393
|
-
const data = this.platform.config.credentials?.token + t + nonce;
|
|
394
|
-
const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
|
|
395
|
-
const sign = signTerm.toString('base64');
|
|
396
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
|
|
397
|
-
const options = {
|
|
398
|
-
hostname: HostDomain,
|
|
399
|
-
port: 443,
|
|
400
|
-
path: `${DevicePath}/${this.device.deviceId}/status`,
|
|
401
|
-
method: 'GET',
|
|
402
|
-
headers: {
|
|
403
|
-
Authorization: this.platform.config.credentials?.token,
|
|
404
|
-
sign: sign,
|
|
405
|
-
nonce: nonce,
|
|
406
|
-
t: t,
|
|
407
|
-
'Content-Type': 'application/json',
|
|
408
|
-
},
|
|
409
|
-
};
|
|
410
|
-
const req = https.request(options, (res) => {
|
|
411
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus statusCode: ${res.statusCode}`);
|
|
412
|
-
this.statusCode({ res });
|
|
413
|
-
let rawData = '';
|
|
414
|
-
res.on('data', (d) => {
|
|
415
|
-
rawData += d;
|
|
416
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
|
|
417
|
-
});
|
|
418
|
-
res.on('end', () => {
|
|
419
|
-
try {
|
|
420
|
-
this.deviceStatus = JSON.parse(rawData);
|
|
421
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus: ${superStringify(this.deviceStatus)}`);
|
|
422
|
-
this.power = this.deviceStatus.body.power;
|
|
423
|
-
this.color = this.deviceStatus.body.color;
|
|
424
|
-
this.brightness = this.deviceStatus.body.brightness;
|
|
425
|
-
this.colorTemperature = this.deviceStatus.body.colorTemperature;
|
|
426
|
-
this.openAPIparseStatus();
|
|
427
|
-
this.updateHomeKitCharacteristics();
|
|
428
|
-
} catch (e: any) {
|
|
429
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
|
|
430
|
-
}
|
|
431
|
-
});
|
|
388
|
+
const { body, statusCode, headers } = await request(`${Devices}/${this.device.deviceId}/status`, {
|
|
389
|
+
headers: this.platform.generateHeaders(),
|
|
432
390
|
});
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
391
|
+
const deviceStatus = await body.json();
|
|
392
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Devices: ${JSON.stringify(deviceStatus.body)}`);
|
|
393
|
+
this.statusCode(statusCode);
|
|
394
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Headers: ${JSON.stringify(headers)}`);
|
|
395
|
+
this.debugLog(
|
|
396
|
+
`${this.device.deviceType}: ${this.accessory.displayName
|
|
397
|
+
} refreshStatus: ${JSON.stringify(deviceStatus)}`,
|
|
398
|
+
);
|
|
399
|
+
this.power = deviceStatus.body.power;
|
|
400
|
+
this.color = deviceStatus.body.color;
|
|
401
|
+
this.brightness = deviceStatus.body.brightness;
|
|
402
|
+
this.colorTemperature = deviceStatus.body.colorTemperature;
|
|
403
|
+
this.openAPIparseStatus();
|
|
404
|
+
this.updateHomeKitCharacteristics();
|
|
437
405
|
} catch (e: any) {
|
|
438
406
|
this.apiError(e);
|
|
439
407
|
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
|
|
440
|
-
|
|
408
|
+
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
441
409
|
}
|
|
442
410
|
}
|
|
443
411
|
|
|
@@ -462,7 +430,7 @@ export class CeilingLight {
|
|
|
462
430
|
} else {
|
|
463
431
|
await this.offlineOff();
|
|
464
432
|
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
|
|
465
|
-
|
|
433
|
+
+ ` ${this.device.connectionType}, pushChanges will not happen.`);
|
|
466
434
|
}
|
|
467
435
|
// Refresh the status from the API
|
|
468
436
|
interval(15000)
|
|
@@ -509,270 +477,158 @@ export class CeilingLight {
|
|
|
509
477
|
.catch(async (e: any) => {
|
|
510
478
|
this.apiError(e);
|
|
511
479
|
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}`
|
|
512
|
-
|
|
480
|
+
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
513
481
|
await this.BLEPushConnection();
|
|
514
482
|
});
|
|
515
483
|
} else {
|
|
516
484
|
this.debugLog(
|
|
517
485
|
`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges.` + `On: ${this.On}, `
|
|
518
|
-
|
|
486
|
+
+ `OnCached: ${this.accessory.context.On}`,
|
|
519
487
|
);
|
|
520
488
|
}
|
|
521
489
|
}
|
|
522
490
|
|
|
523
491
|
async openAPIpushChanges(): Promise<void> {
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
} else {
|
|
541
|
-
command = 'turnOff';
|
|
542
|
-
}
|
|
543
|
-
const body = superStringify({
|
|
544
|
-
'command': `${command}`,
|
|
545
|
-
'parameter': 'default',
|
|
546
|
-
'commandType': 'command',
|
|
547
|
-
});
|
|
548
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
|
|
549
|
-
const options = {
|
|
550
|
-
hostname: HostDomain,
|
|
551
|
-
port: 443,
|
|
552
|
-
path: `${DevicePath}/${this.device.deviceId}/commands`,
|
|
492
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} OpenAPI pushChanges`);
|
|
493
|
+
if (this.On !== this.accessory.context.On) {
|
|
494
|
+
let command = '';
|
|
495
|
+
if (this.On) {
|
|
496
|
+
command = 'turnOn';
|
|
497
|
+
} else {
|
|
498
|
+
command = 'turnOff';
|
|
499
|
+
}
|
|
500
|
+
const body = JSON.stringify({
|
|
501
|
+
'command': `${command}`,
|
|
502
|
+
'parameter': 'default',
|
|
503
|
+
'commandType': 'command',
|
|
504
|
+
});
|
|
505
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
|
|
506
|
+
try {
|
|
507
|
+
const { body, statusCode, headers } = await request(`${Devices}/${this.device.deviceId}/commands`, {
|
|
553
508
|
method: 'POST',
|
|
554
|
-
headers:
|
|
555
|
-
'Authorization': this.platform.config.credentials?.token,
|
|
556
|
-
'sign': sign,
|
|
557
|
-
'nonce': nonce,
|
|
558
|
-
't': t,
|
|
559
|
-
'Content-Type': 'application/json',
|
|
560
|
-
'Content-Length': body.length,
|
|
561
|
-
},
|
|
562
|
-
};
|
|
563
|
-
const req = https.request(options, res => {
|
|
564
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges statusCode: ${res.statusCode}`);
|
|
565
|
-
this.statusCode({ res });
|
|
566
|
-
res.on('data', d => {
|
|
567
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
|
|
568
|
-
});
|
|
509
|
+
headers: this.platform.generateHeaders(),
|
|
569
510
|
});
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
this.
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
`${this.device.deviceType}: ${this.accessory.displayName} No openAPIpushChanges.` + `On: ${this.On}, `
|
|
579
|
-
+`OnCached: ${this.accessory.context.On}`,
|
|
511
|
+
const deviceStatus = await body.json();
|
|
512
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Devices: ${JSON.stringify(deviceStatus.body)}`);
|
|
513
|
+
this.statusCode(statusCode);
|
|
514
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Headers: ${JSON.stringify(headers)}`);
|
|
515
|
+
} catch (e: any) {
|
|
516
|
+
this.apiError(e);
|
|
517
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
|
|
518
|
+
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`,
|
|
580
519
|
);
|
|
581
520
|
}
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
this.
|
|
597
|
-
+ ` Connection, Error Message: ${superStringify(e.message)}`,
|
|
598
|
-
);
|
|
521
|
+
} else {
|
|
522
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No openAPIpushChanges.` + `On: ${this.On}, `
|
|
523
|
+
+ `OnCached: ${this.accessory.context.On}`);
|
|
524
|
+
}
|
|
525
|
+
// Push Hue & Saturation Update
|
|
526
|
+
if (this.On) {
|
|
527
|
+
await this.pushHueSaturationChanges();
|
|
528
|
+
}
|
|
529
|
+
// Push ColorTemperature Update
|
|
530
|
+
if (this.On) {
|
|
531
|
+
await this.pushColorTemperatureChanges();
|
|
532
|
+
}
|
|
533
|
+
// Push Brightness Update
|
|
534
|
+
if (this.On) {
|
|
535
|
+
await this.pushBrightnessChanges();
|
|
599
536
|
}
|
|
600
537
|
}
|
|
601
538
|
|
|
602
539
|
async pushHueSaturationChanges(): Promise<void> {
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
.digest();
|
|
618
|
-
const sign = signTerm.toString('base64');
|
|
619
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
|
|
620
|
-
const body = superStringify({
|
|
621
|
-
'command': 'setColor',
|
|
622
|
-
'parameter': `${red}:${green}:${blue}`,
|
|
623
|
-
'commandType': 'command',
|
|
624
|
-
});
|
|
625
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
|
|
626
|
-
const options = {
|
|
627
|
-
hostname: HostDomain,
|
|
628
|
-
port: 443,
|
|
629
|
-
path: `${DevicePath}/${this.device.deviceId}/commands`,
|
|
540
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushHueSaturationChanges`);
|
|
541
|
+
if (this.Hue !== this.accessory.context.Hue || this.Saturation !== this.accessory.context.Saturation) {
|
|
542
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${JSON.stringify(this.Hue)}`);
|
|
543
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${JSON.stringify(this.Saturation)}`);
|
|
544
|
+
const [red, green, blue] = hs2rgb(Number(this.Hue), Number(this.Saturation));
|
|
545
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} rgb: ${JSON.stringify([red, green, blue])}`);
|
|
546
|
+
const body = JSON.stringify({
|
|
547
|
+
'command': 'setColor',
|
|
548
|
+
'parameter': `${red}:${green}:${blue}`,
|
|
549
|
+
'commandType': 'command',
|
|
550
|
+
});
|
|
551
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
|
|
552
|
+
try {
|
|
553
|
+
const { body, statusCode, headers } = await request(`${Devices}/${this.device.deviceId}/commands`, {
|
|
630
554
|
method: 'POST',
|
|
631
|
-
headers:
|
|
632
|
-
'Authorization': this.platform.config.credentials?.token,
|
|
633
|
-
'sign': sign,
|
|
634
|
-
'nonce': nonce,
|
|
635
|
-
't': t,
|
|
636
|
-
'Content-Type': 'application/json',
|
|
637
|
-
'Content-Length': body.length,
|
|
638
|
-
},
|
|
639
|
-
};
|
|
640
|
-
const req = https.request(options, res => {
|
|
641
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushHueSaturationChanges statusCode: ${res.statusCode}`);
|
|
642
|
-
res.on('data', d => {
|
|
643
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
|
|
644
|
-
});
|
|
645
|
-
});
|
|
646
|
-
req.on('error', (e: any) => {
|
|
647
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
|
|
555
|
+
headers: this.platform.generateHeaders(),
|
|
648
556
|
});
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
this.
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
557
|
+
const deviceStatus = await body.json();
|
|
558
|
+
this.debugLog(`Devices: ${JSON.stringify(deviceStatus.body)}`);
|
|
559
|
+
this.statusCode(statusCode);
|
|
560
|
+
this.debugLog(`Headers: ${JSON.stringify(headers)}`);
|
|
561
|
+
} catch (e: any) {
|
|
562
|
+
this.apiError(e);
|
|
563
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushHueSaturationChanges with ${this.device.connectionType}`
|
|
564
|
+
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`,
|
|
565
|
+
);
|
|
655
566
|
}
|
|
656
|
-
}
|
|
657
|
-
this.
|
|
658
|
-
|
|
659
|
-
+ ` Connection, Error Message: ${superStringify(e.message)}`);
|
|
567
|
+
} else {
|
|
568
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushHueSaturationChanges. Hue: ${this.Hue}, `
|
|
569
|
+
+ `HueCached: ${this.accessory.context.Hue}, Saturation: ${this.Saturation}, SaturationCached: ${this.accessory.context.Saturation}`);
|
|
660
570
|
}
|
|
661
571
|
}
|
|
662
572
|
|
|
663
573
|
async pushColorTemperatureChanges(): Promise<void> {
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
.digest();
|
|
677
|
-
const sign = signTerm.toString('base64');
|
|
678
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
|
|
679
|
-
const body = superStringify({
|
|
680
|
-
'command': 'setColorTemperature',
|
|
681
|
-
'parameter': `${kelvin}`,
|
|
682
|
-
'commandType': 'command',
|
|
683
|
-
});
|
|
684
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
|
|
685
|
-
const options = {
|
|
686
|
-
hostname: HostDomain,
|
|
687
|
-
port: 443,
|
|
688
|
-
path: `${DevicePath}/${this.device.deviceId}/commands`,
|
|
574
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushColorTemperatureChanges`);
|
|
575
|
+
if (this.ColorTemperature !== this.accessory.context.ColorTemperature) {
|
|
576
|
+
const kelvin = Math.round(1000000 / Number(this.ColorTemperature));
|
|
577
|
+
this.cacheKelvin = kelvin;
|
|
578
|
+
const body = JSON.stringify({
|
|
579
|
+
'command': 'setColorTemperature',
|
|
580
|
+
'parameter': `${kelvin}`,
|
|
581
|
+
'commandType': 'command',
|
|
582
|
+
});
|
|
583
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
|
|
584
|
+
try {
|
|
585
|
+
const { body, statusCode, headers } = await request(`${Devices}/${this.device.deviceId}/commands`, {
|
|
689
586
|
method: 'POST',
|
|
690
|
-
headers:
|
|
691
|
-
'Authorization': this.platform.config.credentials?.token,
|
|
692
|
-
'sign': sign,
|
|
693
|
-
'nonce': nonce,
|
|
694
|
-
't': t,
|
|
695
|
-
'Content-Type': 'application/json',
|
|
696
|
-
'Content-Length': body.length,
|
|
697
|
-
},
|
|
698
|
-
};
|
|
699
|
-
const req = https.request(options, res => {
|
|
700
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
|
|
701
|
-
res.on('data', d => {
|
|
702
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
|
|
703
|
-
});
|
|
704
|
-
});
|
|
705
|
-
req.on('error', (e: any) => {
|
|
706
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
|
|
587
|
+
headers: this.platform.generateHeaders(),
|
|
707
588
|
});
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
this.
|
|
711
|
-
this.
|
|
712
|
-
}
|
|
713
|
-
this.
|
|
714
|
-
|
|
589
|
+
const deviceStatus = await body.json();
|
|
590
|
+
this.debugLog(`Devices: ${JSON.stringify(deviceStatus.body)}`);
|
|
591
|
+
this.statusCode(statusCode);
|
|
592
|
+
this.debugLog(`Headers: ${JSON.stringify(headers)}`);
|
|
593
|
+
} catch (e: any) {
|
|
594
|
+
this.apiError(e);
|
|
595
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushColorTemperatureChanges with ${this.device.connectionType}`
|
|
596
|
+
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`,
|
|
597
|
+
);
|
|
715
598
|
}
|
|
716
|
-
}
|
|
717
|
-
this.
|
|
718
|
-
|
|
719
|
-
+ ` Connection, Error Message: ${superStringify(e.message)}`);
|
|
599
|
+
} else {
|
|
600
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushColorTemperatureChanges.` +
|
|
601
|
+
`ColorTemperature: ${this.ColorTemperature}, ColorTemperatureCached: ${this.accessory.context.ColorTemperature}`);
|
|
720
602
|
}
|
|
721
603
|
}
|
|
722
604
|
|
|
723
605
|
async pushBrightnessChanges(): Promise<void> {
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
const sign = signTerm.toString('base64');
|
|
735
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
|
|
736
|
-
const body = superStringify({
|
|
737
|
-
'command': 'setBrightness',
|
|
738
|
-
'parameter': `${this.Brightness}`,
|
|
739
|
-
'commandType': 'command',
|
|
740
|
-
});
|
|
741
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
|
|
742
|
-
const options = {
|
|
743
|
-
hostname: HostDomain,
|
|
744
|
-
port: 443,
|
|
745
|
-
path: `${DevicePath}/${this.device.deviceId}/commands`,
|
|
606
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushBrightnessChanges`);
|
|
607
|
+
if (this.Brightness !== this.accessory.context.Brightness) {
|
|
608
|
+
const body = JSON.stringify({
|
|
609
|
+
'command': 'setBrightness',
|
|
610
|
+
'parameter': `${this.Brightness}`,
|
|
611
|
+
'commandType': 'command',
|
|
612
|
+
});
|
|
613
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
|
|
614
|
+
try {
|
|
615
|
+
const { body, statusCode, headers } = await request(`${Devices}/${this.device.deviceId}/commands`, {
|
|
746
616
|
method: 'POST',
|
|
747
|
-
headers:
|
|
748
|
-
'Authorization': this.platform.config.credentials?.token,
|
|
749
|
-
'sign': sign,
|
|
750
|
-
'nonce': nonce,
|
|
751
|
-
't': t,
|
|
752
|
-
'Content-Type': 'application/json',
|
|
753
|
-
'Content-Length': body.length,
|
|
754
|
-
},
|
|
755
|
-
};
|
|
756
|
-
const req = https.request(options, res => {
|
|
757
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushBrightnessChanges statusCode: ${res.statusCode}`);
|
|
758
|
-
res.on('data', d => {
|
|
759
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
|
|
760
|
-
});
|
|
761
|
-
});
|
|
762
|
-
req.on('error', (e: any) => {
|
|
763
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
|
|
617
|
+
headers: this.platform.generateHeaders(),
|
|
764
618
|
});
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
this.
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
619
|
+
const deviceStatus = await body.json();
|
|
620
|
+
this.debugLog(`Devices: ${JSON.stringify(deviceStatus.body)}`);
|
|
621
|
+
this.statusCode(statusCode);
|
|
622
|
+
this.debugLog(`Headers: ${JSON.stringify(headers)}`);
|
|
623
|
+
} catch (e: any) {
|
|
624
|
+
this.apiError(e);
|
|
625
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushBrightnessChanges with ${this.device.connectionType}`
|
|
626
|
+
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`,
|
|
627
|
+
);
|
|
771
628
|
}
|
|
772
|
-
}
|
|
773
|
-
this.
|
|
774
|
-
|
|
775
|
-
+ ` Connection, Error Message: ${superStringify(e.message)}`);
|
|
629
|
+
} else {
|
|
630
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushBrightnessChanges.` + `Brightness: ${this.Brightness}, `
|
|
631
|
+
+ `BrightnessCached: ${this.accessory.context.Brightness}`);
|
|
776
632
|
}
|
|
777
633
|
}
|
|
778
634
|
|
|
@@ -941,7 +797,7 @@ export class CeilingLight {
|
|
|
941
797
|
});
|
|
942
798
|
// Set an event handler
|
|
943
799
|
switchbot.onadvertisement = (ad: any) => {
|
|
944
|
-
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${
|
|
800
|
+
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${JSON.stringify(ad, null, ' ')}`);
|
|
945
801
|
};
|
|
946
802
|
await sleep(10000);
|
|
947
803
|
// Stop to monitor
|
|
@@ -1008,40 +864,41 @@ export class CeilingLight {
|
|
|
1008
864
|
}
|
|
1009
865
|
}
|
|
1010
866
|
|
|
1011
|
-
async statusCode(
|
|
1012
|
-
switch (
|
|
867
|
+
async statusCode(statusCode: number): Promise<void> {
|
|
868
|
+
switch (statusCode) {
|
|
1013
869
|
case 151:
|
|
1014
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command not supported by this
|
|
870
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command not supported by this deviceType, statusCode: ${statusCode}`);
|
|
1015
871
|
break;
|
|
1016
872
|
case 152:
|
|
1017
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device not found
|
|
873
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device not found, statusCode: ${statusCode}`);
|
|
1018
874
|
break;
|
|
1019
875
|
case 160:
|
|
1020
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command is not supported
|
|
876
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command is not supported, statusCode: ${statusCode}`);
|
|
1021
877
|
break;
|
|
1022
878
|
case 161:
|
|
1023
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device is offline
|
|
1024
|
-
|
|
879
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device is offline, statusCode: ${statusCode}`);
|
|
880
|
+
this.offlineOff();
|
|
1025
881
|
break;
|
|
1026
882
|
case 171:
|
|
1027
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} is offline
|
|
1028
|
-
|
|
883
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Hub Device is offline, statusCode: ${statusCode}. `
|
|
884
|
+
+ `Hub: ${this.device.hubDeviceId}`);
|
|
885
|
+
this.offlineOff();
|
|
1029
886
|
break;
|
|
1030
887
|
case 190:
|
|
1031
888
|
this.errorLog(
|
|
1032
889
|
`${this.device.deviceType}: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,` +
|
|
1033
|
-
|
|
890
|
+
` Or command format is invalid, statusCode: ${statusCode}`,
|
|
1034
891
|
);
|
|
1035
892
|
break;
|
|
1036
893
|
case 100:
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
894
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Command successfully sent, statusCode: ${statusCode}`);
|
|
895
|
+
break;
|
|
896
|
+
case 200:
|
|
897
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Request successful, statusCode: ${statusCode}`);
|
|
1040
898
|
break;
|
|
1041
899
|
default:
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
}
|
|
900
|
+
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode: `
|
|
901
|
+
+ `${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`);
|
|
1045
902
|
}
|
|
1046
903
|
}
|
|
1047
904
|
|
|
@@ -1063,7 +920,7 @@ export class CeilingLight {
|
|
|
1063
920
|
FirmwareRevision(accessory: PlatformAccessory<Context>, device: device & devicesConfig): CharacteristicValue {
|
|
1064
921
|
let FirmwareRevision: string;
|
|
1065
922
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
|
|
1066
|
-
|
|
923
|
+
+ ` accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`);
|
|
1067
924
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} device.firmware: ${device.firmware}`);
|
|
1068
925
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} this.platform.version: ${this.platform.version}`);
|
|
1069
926
|
if (accessory.context.FirmwareRevision) {
|
|
@@ -1143,7 +1000,7 @@ export class CeilingLight {
|
|
|
1143
1000
|
config['maxRetry'] = device.maxRetry;
|
|
1144
1001
|
}
|
|
1145
1002
|
if (Object.entries(config).length !== 0) {
|
|
1146
|
-
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${
|
|
1003
|
+
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
|
|
1147
1004
|
}
|
|
1148
1005
|
}
|
|
1149
1006
|
|