@switchbot/homebridge-switchbot 3.8.3-beta.2 → 3.8.3-beta.21
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/workflows/beta-release.yml +2 -1
- package/CHANGELOG.md +8 -0
- package/dist/device/blindtilt.d.ts +1 -4
- package/dist/device/blindtilt.d.ts.map +1 -1
- package/dist/device/blindtilt.js +18 -19
- package/dist/device/blindtilt.js.map +1 -1
- package/dist/device/bot.d.ts +1 -4
- package/dist/device/bot.d.ts.map +1 -1
- package/dist/device/bot.js +15 -16
- package/dist/device/bot.js.map +1 -1
- package/dist/device/ceilinglight.d.ts +1 -4
- package/dist/device/ceilinglight.d.ts.map +1 -1
- package/dist/device/ceilinglight.js +35 -39
- package/dist/device/ceilinglight.js.map +1 -1
- package/dist/device/colorbulb.d.ts +1 -4
- package/dist/device/colorbulb.d.ts.map +1 -1
- package/dist/device/colorbulb.js +35 -39
- package/dist/device/colorbulb.js.map +1 -1
- package/dist/device/contact.d.ts +1 -4
- package/dist/device/contact.d.ts.map +1 -1
- package/dist/device/contact.js +7 -7
- package/dist/device/contact.js.map +1 -1
- package/dist/device/curtain.d.ts +1 -4
- package/dist/device/curtain.d.ts.map +1 -1
- package/dist/device/curtain.js +16 -17
- package/dist/device/curtain.js.map +1 -1
- package/dist/device/device.d.ts +6 -6
- package/dist/device/device.d.ts.map +1 -1
- package/dist/device/device.js +30 -14
- package/dist/device/device.js.map +1 -1
- package/dist/device/fan.d.ts +1 -4
- package/dist/device/fan.d.ts.map +1 -1
- package/dist/device/fan.js +28 -31
- package/dist/device/fan.js.map +1 -1
- package/dist/device/hub.d.ts +1 -4
- package/dist/device/hub.d.ts.map +1 -1
- package/dist/device/hub.js +7 -7
- package/dist/device/hub.js.map +1 -1
- package/dist/device/humidifier.d.ts +1 -4
- package/dist/device/humidifier.d.ts.map +1 -1
- package/dist/device/humidifier.js +28 -31
- package/dist/device/humidifier.js.map +1 -1
- package/dist/device/iosensor.d.ts +1 -4
- package/dist/device/iosensor.d.ts.map +1 -1
- package/dist/device/iosensor.js +7 -7
- package/dist/device/iosensor.js.map +1 -1
- package/dist/device/lightstrip.d.ts +1 -4
- package/dist/device/lightstrip.d.ts.map +1 -1
- package/dist/device/lightstrip.js +28 -31
- package/dist/device/lightstrip.js.map +1 -1
- package/dist/device/lock.d.ts +1 -4
- package/dist/device/lock.d.ts.map +1 -1
- package/dist/device/lock.js +15 -16
- package/dist/device/lock.js.map +1 -1
- package/dist/device/meter.d.ts +1 -4
- package/dist/device/meter.d.ts.map +1 -1
- package/dist/device/meter.js +7 -7
- package/dist/device/meter.js.map +1 -1
- package/dist/device/meterplus.d.ts +1 -4
- package/dist/device/meterplus.d.ts.map +1 -1
- package/dist/device/meterplus.js +7 -7
- package/dist/device/meterplus.js.map +1 -1
- package/dist/device/motion.d.ts +1 -4
- package/dist/device/motion.d.ts.map +1 -1
- package/dist/device/motion.js +7 -7
- package/dist/device/motion.js.map +1 -1
- package/dist/device/plug.d.ts +1 -4
- package/dist/device/plug.d.ts.map +1 -1
- package/dist/device/plug.js +14 -15
- package/dist/device/plug.js.map +1 -1
- package/dist/device/robotvacuumcleaner.d.ts +1 -4
- package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
- package/dist/device/robotvacuumcleaner.js +21 -23
- package/dist/device/robotvacuumcleaner.js.map +1 -1
- package/dist/device/waterdetector.d.ts +1 -4
- package/dist/device/waterdetector.d.ts.map +1 -1
- package/dist/device/waterdetector.js +7 -7
- package/dist/device/waterdetector.js.map +1 -1
- package/dist/irdevice/airconditioner.d.ts +1 -1
- package/dist/irdevice/airconditioner.d.ts.map +1 -1
- package/dist/irdevice/airconditioner.js +11 -12
- package/dist/irdevice/airconditioner.js.map +1 -1
- package/dist/irdevice/airpurifier.d.ts +1 -1
- package/dist/irdevice/airpurifier.d.ts.map +1 -1
- package/dist/irdevice/airpurifier.js +11 -12
- package/dist/irdevice/airpurifier.js.map +1 -1
- package/dist/irdevice/camera.d.ts +1 -1
- package/dist/irdevice/camera.d.ts.map +1 -1
- package/dist/irdevice/camera.js +9 -10
- package/dist/irdevice/camera.js.map +1 -1
- package/dist/irdevice/fan.d.ts +1 -1
- package/dist/irdevice/fan.d.ts.map +1 -1
- package/dist/irdevice/fan.js +15 -16
- package/dist/irdevice/fan.js.map +1 -1
- package/dist/irdevice/irdevice.d.ts +6 -6
- package/dist/irdevice/irdevice.d.ts.map +1 -1
- package/dist/irdevice/irdevice.js +7 -13
- package/dist/irdevice/irdevice.js.map +1 -1
- package/dist/irdevice/light.d.ts +1 -1
- package/dist/irdevice/light.d.ts.map +1 -1
- package/dist/irdevice/light.js +9 -10
- package/dist/irdevice/light.js.map +1 -1
- package/dist/irdevice/other.d.ts +1 -1
- package/dist/irdevice/other.d.ts.map +1 -1
- package/dist/irdevice/other.js +9 -10
- package/dist/irdevice/other.js.map +1 -1
- package/dist/irdevice/tv.d.ts +1 -1
- package/dist/irdevice/tv.d.ts.map +1 -1
- package/dist/irdevice/tv.js +27 -28
- package/dist/irdevice/tv.js.map +1 -1
- package/dist/irdevice/vacuumcleaner.d.ts +1 -1
- package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
- package/dist/irdevice/vacuumcleaner.js +9 -10
- package/dist/irdevice/vacuumcleaner.js.map +1 -1
- package/dist/irdevice/waterheater.d.ts +1 -1
- package/dist/irdevice/waterheater.d.ts.map +1 -1
- package/dist/irdevice/waterheater.js +9 -10
- package/dist/irdevice/waterheater.js.map +1 -1
- package/dist/platform.d.ts +5 -14
- package/dist/platform.d.ts.map +1 -1
- package/dist/platform.js +53 -161
- package/dist/platform.js.map +1 -1
- package/dist/settings.d.ts +1 -3
- package/dist/settings.d.ts.map +1 -1
- package/dist/settings.js.map +1 -1
- package/dist/utils.d.ts +1 -1
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js.map +1 -1
- package/package.json +9 -10
- package/src/device/blindtilt.ts +20 -24
- package/src/device/bot.ts +16 -20
- package/src/device/ceilinglight.ts +36 -43
- package/src/device/colorbulb.ts +36 -43
- package/src/device/contact.ts +8 -11
- package/src/device/curtain.ts +18 -22
- package/src/device/device.ts +29 -18
- package/src/device/fan.ts +29 -35
- package/src/device/hub.ts +8 -11
- package/src/device/humidifier.ts +29 -35
- package/src/device/iosensor.ts +8 -11
- package/src/device/lightstrip.ts +29 -35
- package/src/device/lock.ts +16 -20
- package/src/device/meter.ts +8 -11
- package/src/device/meterplus.ts +8 -11
- package/src/device/motion.ts +8 -11
- package/src/device/plug.ts +15 -19
- package/src/device/robotvacuumcleaner.ts +22 -31
- package/src/device/waterdetector.ts +8 -11
- package/src/irdevice/airconditioner.ts +12 -14
- package/src/irdevice/airpurifier.ts +12 -13
- package/src/irdevice/camera.ts +10 -11
- package/src/irdevice/fan.ts +16 -17
- package/src/irdevice/irdevice.ts +9 -17
- package/src/irdevice/light.ts +10 -11
- package/src/irdevice/other.ts +10 -11
- package/src/irdevice/tv.ts +28 -29
- package/src/irdevice/vacuumcleaner.ts +10 -11
- package/src/irdevice/waterheater.ts +10 -11
- package/src/platform.ts +55 -161
- package/src/settings.ts +1 -4
- package/src/utils.ts +2 -1
- package/dist/types/bledevicestatus.d.ts +0 -281
- package/dist/types/bledevicestatus.d.ts.map +0 -1
- package/dist/types/bledevicestatus.js +0 -2
- package/dist/types/bledevicestatus.js.map +0 -1
- package/dist/types/devicelist.d.ts +0 -89
- package/dist/types/devicelist.d.ts.map +0 -1
- package/dist/types/devicelist.js +0 -6
- package/dist/types/devicelist.js.map +0 -1
- package/dist/types/deviceresponse.d.ts +0 -13
- package/dist/types/deviceresponse.d.ts.map +0 -1
- package/dist/types/deviceresponse.js +0 -2
- package/dist/types/deviceresponse.js.map +0 -1
- package/dist/types/devicestatus.d.ts +0 -149
- package/dist/types/devicestatus.d.ts.map +0 -1
- package/dist/types/devicestatus.js +0 -3
- package/dist/types/devicestatus.js.map +0 -1
- package/dist/types/devicewebhookstatus.d.ts +0 -156
- package/dist/types/devicewebhookstatus.d.ts.map +0 -1
- package/dist/types/devicewebhookstatus.js +0 -2
- package/dist/types/devicewebhookstatus.js.map +0 -1
- package/dist/types/irdevicelist.d.ts +0 -10
- package/dist/types/irdevicelist.d.ts.map +0 -1
- package/dist/types/irdevicelist.js +0 -2
- package/dist/types/irdevicelist.js.map +0 -1
- package/dist/types/pushbody.d.ts +0 -6
- package/dist/types/pushbody.d.ts.map +0 -1
- package/dist/types/pushbody.js +0 -2
- package/dist/types/pushbody.js.map +0 -1
- package/src/types/bledevicestatus.ts +0 -303
- package/src/types/devicelist.ts +0 -124
- package/src/types/deviceresponse.ts +0 -14
- package/src/types/devicestatus.ts +0 -178
- package/src/types/devicewebhookstatus.ts +0 -191
- package/src/types/irdevicelist.ts +0 -15
- package/src/types/pushbody.ts +0 -9
package/dist/platform.d.ts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import type { Server } from 'node:http';
|
|
2
|
-
import type { UrlObject } from 'node:url';
|
|
3
2
|
import type { API, DynamicPlatformPlugin, Logging, PlatformAccessory } from 'homebridge';
|
|
4
3
|
import type { MqttClient } from 'mqtt';
|
|
5
|
-
import type { Dispatcher } from 'undici';
|
|
6
4
|
import type { devicesConfig, irDevicesConfig, SwitchBotPlatformConfig } from './settings.js';
|
|
7
|
-
import type { blindTilt, curtain, curtain3, device } from '
|
|
5
|
+
import type { blindTilt, curtain, curtain3, device } from 'node-switchbot';
|
|
6
|
+
import { SwitchBotOpenAPI } from 'node-switchbot';
|
|
8
7
|
/**
|
|
9
8
|
* HomebridgePlatform
|
|
10
9
|
* This class is the main constructor for your plugin, this is where you should
|
|
@@ -32,6 +31,7 @@ export declare class SwitchBotPlatform implements DynamicPlatformPlugin {
|
|
|
32
31
|
readonly bleEventHandler: {
|
|
33
32
|
[x: string]: (context: any) => void;
|
|
34
33
|
};
|
|
34
|
+
switchBotAPI: SwitchBotOpenAPI;
|
|
35
35
|
constructor(log: Logging, config: SwitchBotPlatformConfig, api: API);
|
|
36
36
|
setupMqtt(): Promise<void>;
|
|
37
37
|
setupwebhook(): Promise<void>;
|
|
@@ -50,13 +50,6 @@ export declare class SwitchBotPlatform implements DynamicPlatformPlugin {
|
|
|
50
50
|
* This method saves the openToken as the token in the config.json file
|
|
51
51
|
*/
|
|
52
52
|
updateToken(): Promise<void>;
|
|
53
|
-
generateHeaders: () => {
|
|
54
|
-
Authorization: any;
|
|
55
|
-
sign: string;
|
|
56
|
-
nonce: `${string}-${string}-${string}-${string}-${string}`;
|
|
57
|
-
t: string;
|
|
58
|
-
'Content-Type': string;
|
|
59
|
-
};
|
|
60
53
|
discoverDevices(): Promise<void>;
|
|
61
54
|
private handleManualConfig;
|
|
62
55
|
private isSuccessfulResponse;
|
|
@@ -107,10 +100,8 @@ export declare class SwitchBotPlatform implements DynamicPlatformPlugin {
|
|
|
107
100
|
* @returns A promise that resolves when the logging is complete.
|
|
108
101
|
*/
|
|
109
102
|
statusCode(statusCode: number): Promise<void>;
|
|
110
|
-
retryRequest(deviceMaxRetries: number, deviceDelayBetweenRetries: number
|
|
111
|
-
|
|
112
|
-
} & Omit<Dispatcher.RequestOptions, 'origin' | 'path' | 'method'> & Partial<Pick<Dispatcher.RequestOptions, 'method'>>): Promise<{
|
|
113
|
-
body: any;
|
|
103
|
+
retryRequest(deviceId: string, deviceMaxRetries: number, deviceDelayBetweenRetries: number): Promise<{
|
|
104
|
+
response: any;
|
|
114
105
|
statusCode: number;
|
|
115
106
|
}>;
|
|
116
107
|
connectBLE(accessory: PlatformAccessory, device: device & devicesConfig): Promise<any>;
|
package/dist/platform.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"platform.d.ts","sourceRoot":"","sources":["../src/platform.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"platform.d.ts","sourceRoot":"","sources":["../src/platform.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,WAAW,CAAA;AAE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AAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAMtC,aAAa,CAAC,GAAG,GAAG,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAQ3C,QAAQ,CAAC,GAAG,GAAG,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAUtC,cAAc,IAAI,OAAO,CAAC,OAAO,CAAC;IAIlC,uBAAuB,IAAI,OAAO,CAAC,OAAO,CAAC;CAGlD"}
|
package/dist/platform.js
CHANGED
|
@@ -1,18 +1,10 @@
|
|
|
1
|
-
import { Buffer } from 'node:buffer';
|
|
2
|
-
import crypto, { randomUUID } from 'node:crypto';
|
|
3
1
|
import { readFileSync, writeFileSync } from 'node:fs';
|
|
4
|
-
import { createServer } from 'node:http';
|
|
5
2
|
import process from 'node:process';
|
|
6
3
|
import asyncmqtt from 'async-mqtt';
|
|
7
4
|
import fakegato from 'fakegato-history';
|
|
8
5
|
import { EveHomeKitTypes } from 'homebridge-lib/EveHomeKitTypes';
|
|
9
|
-
|
|
10
|
-
* For Testing Locally:
|
|
11
|
-
* import { SwitchBotModel } from '/Users/Shared/GitHub/OpenWonderLabs/node-switchbot/dist/index.js';
|
|
12
|
-
*/
|
|
13
|
-
import { SwitchBot, SwitchBotModel } from 'node-switchbot';
|
|
6
|
+
import { LogLevel, SwitchBot, SwitchBotModel, SwitchBotOpenAPI } from 'node-switchbot';
|
|
14
7
|
import { queueScheduler } from 'rxjs';
|
|
15
|
-
import { request } from 'undici';
|
|
16
8
|
import { BlindTilt } from './device/blindtilt.js';
|
|
17
9
|
import { Bot } from './device/bot.js';
|
|
18
10
|
import { CeilingLight } from './device/ceilinglight.js';
|
|
@@ -40,7 +32,7 @@ import { Others } from './irdevice/other.js';
|
|
|
40
32
|
import { TV } from './irdevice/tv.js';
|
|
41
33
|
import { VacuumCleaner } from './irdevice/vacuumcleaner.js';
|
|
42
34
|
import { WaterHeater } from './irdevice/waterheater.js';
|
|
43
|
-
import {
|
|
35
|
+
import { PLATFORM_NAME, PLUGIN_NAME } from './settings.js';
|
|
44
36
|
import { formatDeviceIdAsMac, isBlindTiltDevice, isCurtainDevice, sleep } from './utils.js';
|
|
45
37
|
/**
|
|
46
38
|
* HomebridgePlatform
|
|
@@ -70,6 +62,7 @@ export class SwitchBotPlatform {
|
|
|
70
62
|
// Event Handlers
|
|
71
63
|
webhookEventHandler = {};
|
|
72
64
|
bleEventHandler = {};
|
|
65
|
+
switchBotAPI;
|
|
73
66
|
constructor(log, config, api) {
|
|
74
67
|
this.api = api;
|
|
75
68
|
this.log = log;
|
|
@@ -100,6 +93,24 @@ export class SwitchBotPlatform {
|
|
|
100
93
|
this.debugErrorLog(`Verify Config, Error: ${e}`);
|
|
101
94
|
return;
|
|
102
95
|
}
|
|
96
|
+
// SwitchBot OpenAPI
|
|
97
|
+
this.switchBotAPI = new SwitchBotOpenAPI(this.config.credentials?.token, this.config.credentials?.secret);
|
|
98
|
+
// Listen for log events
|
|
99
|
+
this.switchBotAPI.on('log', (log) => {
|
|
100
|
+
switch (log.level) {
|
|
101
|
+
case LogLevel.ERROR:
|
|
102
|
+
this.errorLog(log.message);
|
|
103
|
+
break;
|
|
104
|
+
case LogLevel.WARN:
|
|
105
|
+
this.warnLog(log.message);
|
|
106
|
+
break;
|
|
107
|
+
case LogLevel.DEBUG:
|
|
108
|
+
this.debugLog(log.message);
|
|
109
|
+
break;
|
|
110
|
+
default:
|
|
111
|
+
this.infoLog(log.message);
|
|
112
|
+
}
|
|
113
|
+
});
|
|
103
114
|
// import fakegato-history module and EVE characteristics
|
|
104
115
|
this.fakegatoAPI = fakegato(api);
|
|
105
116
|
this.eve = new EveHomeKitTypes(api);
|
|
@@ -181,123 +192,23 @@ export class SwitchBotPlatform {
|
|
|
181
192
|
if (this.config.options?.webhookURL) {
|
|
182
193
|
const url = this.config.options?.webhookURL;
|
|
183
194
|
try {
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
try {
|
|
192
|
-
const body = JSON.parse(data);
|
|
193
|
-
await this.debugLog(`Received Webhook: ${JSON.stringify(body)}`);
|
|
194
|
-
if (this.config.options?.mqttURL) {
|
|
195
|
-
const mac = body.context.deviceMac?.toLowerCase().match(/[\s\S]{1,2}/g)?.join(':');
|
|
196
|
-
const options = this.config.options?.mqttPubOptions || {};
|
|
197
|
-
this.mqttClient?.publish(`homebridge-switchbot/webhook/${mac}`, `${JSON.stringify(body.context)}`, options);
|
|
198
|
-
}
|
|
199
|
-
this.webhookEventHandler[body.context.deviceMac]?.(body.context);
|
|
200
|
-
}
|
|
201
|
-
catch (e) {
|
|
202
|
-
await this.errorLog(`Failed to handle webhook event. Error:${e}`);
|
|
203
|
-
}
|
|
204
|
-
});
|
|
205
|
-
response.writeHead(200, { 'Content-Type': 'text/plain' });
|
|
206
|
-
response.end('OK');
|
|
207
|
-
}
|
|
208
|
-
// else {
|
|
209
|
-
// response.writeHead(403, {'Content-Type': 'text/plain'});
|
|
210
|
-
// response.end(`NG`);
|
|
211
|
-
// }
|
|
195
|
+
this.switchBotAPI.setupWebhook(url);
|
|
196
|
+
// Listen for webhook events
|
|
197
|
+
this.switchBotAPI.on('webhookEvent', (body) => {
|
|
198
|
+
if (this.config.options?.mqttURL) {
|
|
199
|
+
const mac = body.context.deviceMac?.toLowerCase().match(/[\s\S]{1,2}/g)?.join(':');
|
|
200
|
+
const options = this.config.options?.mqttPubOptions || {};
|
|
201
|
+
this.mqttClient?.publish(`homebridge-switchbot/webhook/${mac}`, `${JSON.stringify(body.context)}`, options);
|
|
212
202
|
}
|
|
213
|
-
|
|
214
|
-
this.errorLog(`Failed to handle webhook event. Error:${e}`);
|
|
215
|
-
}
|
|
216
|
-
}).listen(port || 80);
|
|
217
|
-
}
|
|
218
|
-
catch (e) {
|
|
219
|
-
await this.errorLog(`Failed to create webhook listener. Error:${e.message}`);
|
|
220
|
-
return;
|
|
221
|
-
}
|
|
222
|
-
try {
|
|
223
|
-
const { body, statusCode } = await request(setupWebhook, {
|
|
224
|
-
method: 'POST',
|
|
225
|
-
headers: this.generateHeaders(),
|
|
226
|
-
body: JSON.stringify({
|
|
227
|
-
action: 'setupWebhook',
|
|
228
|
-
url,
|
|
229
|
-
deviceList: 'ALL',
|
|
230
|
-
}),
|
|
231
|
-
});
|
|
232
|
-
const response = await body.json();
|
|
233
|
-
await this.debugLog(`setupWebhook: url:${url}, body:${JSON.stringify(response)}, statusCode:${statusCode}`);
|
|
234
|
-
if (statusCode !== 200 || response?.statusCode !== 100) {
|
|
235
|
-
await this.errorLog(`Failed to configure webhook. Existing webhook well be overridden. HTTP:${statusCode} API:${response?.statusCode} message:${response?.message}`);
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
catch (e) {
|
|
239
|
-
await this.errorLog(`Failed to configure webhook. Error: ${e.message}`);
|
|
240
|
-
}
|
|
241
|
-
try {
|
|
242
|
-
const { body, statusCode } = await request(updateWebhook, {
|
|
243
|
-
method: 'POST',
|
|
244
|
-
headers: this.generateHeaders(),
|
|
245
|
-
body: JSON.stringify({
|
|
246
|
-
action: 'updateWebhook',
|
|
247
|
-
config: {
|
|
248
|
-
url,
|
|
249
|
-
enable: true,
|
|
250
|
-
},
|
|
251
|
-
}),
|
|
252
|
-
});
|
|
253
|
-
const response = await body.json();
|
|
254
|
-
await this.debugLog(`updateWebhook: url:${url}, body:${JSON.stringify(response)}, statusCode:${statusCode}`);
|
|
255
|
-
if (statusCode !== 200 || response?.statusCode !== 100) {
|
|
256
|
-
await this.errorLog(`Failed to update webhook. HTTP:${statusCode} API:${response?.statusCode} message:${response?.message}`);
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
catch (e) {
|
|
260
|
-
await this.errorLog(`Failed to update webhook. Error:${e.message}`);
|
|
261
|
-
}
|
|
262
|
-
try {
|
|
263
|
-
const { body, statusCode } = await request(queryWebhook, {
|
|
264
|
-
method: 'POST',
|
|
265
|
-
headers: this.generateHeaders(),
|
|
266
|
-
body: JSON.stringify({
|
|
267
|
-
action: 'queryUrl',
|
|
268
|
-
}),
|
|
203
|
+
this.webhookEventHandler[body.context.deviceMac]?.(body.context);
|
|
269
204
|
});
|
|
270
|
-
const response = await body.json();
|
|
271
|
-
await this.debugLog(`queryWebhook: body:${JSON.stringify(response)}`);
|
|
272
|
-
await this.debugLog(`queryWebhook: statusCode:${statusCode}`);
|
|
273
|
-
if (statusCode !== 200 || response?.statusCode !== 100) {
|
|
274
|
-
await this.errorLog(`Failed to query webhook. HTTP:${statusCode} API:${response?.statusCode} message:${response?.message}`);
|
|
275
|
-
}
|
|
276
|
-
else {
|
|
277
|
-
await this.infoLog(`Listening webhook on ${response?.body?.urls[0]}`);
|
|
278
|
-
}
|
|
279
205
|
}
|
|
280
206
|
catch (e) {
|
|
281
|
-
await this.errorLog(`Failed to
|
|
207
|
+
await this.errorLog(`Failed to setup webhook. Error:${e.message}`);
|
|
282
208
|
}
|
|
283
209
|
this.api.on('shutdown', async () => {
|
|
284
210
|
try {
|
|
285
|
-
|
|
286
|
-
method: 'POST',
|
|
287
|
-
headers: this.generateHeaders(),
|
|
288
|
-
body: JSON.stringify({
|
|
289
|
-
action: 'deleteWebhook',
|
|
290
|
-
url,
|
|
291
|
-
}),
|
|
292
|
-
});
|
|
293
|
-
const response = await body.json();
|
|
294
|
-
await this.debugLog(`deleteWebhook: url:${url}, body:${JSON.stringify(response)}, statusCode:${statusCode}`);
|
|
295
|
-
if (statusCode !== 200 || response?.statusCode !== 100) {
|
|
296
|
-
await this.errorLog(`Failed to delete webhook. HTTP:${statusCode} API:${response?.statusCode} message:${response?.message}`);
|
|
297
|
-
}
|
|
298
|
-
else {
|
|
299
|
-
await this.infoLog('Unregistered webhook to close listening.');
|
|
300
|
-
}
|
|
211
|
+
this.switchBotAPI.deleteWebhook(url);
|
|
301
212
|
}
|
|
302
213
|
catch (e) {
|
|
303
214
|
await this.errorLog(`Failed to delete webhook. Error:${e.message}`);
|
|
@@ -317,7 +228,12 @@ export class SwitchBotPlatform {
|
|
|
317
228
|
(async () => {
|
|
318
229
|
// Start to monitor advertisement packets
|
|
319
230
|
await this.debugLog('Scanning for BLE SwitchBot devices...');
|
|
320
|
-
|
|
231
|
+
try {
|
|
232
|
+
await switchbot.startScan();
|
|
233
|
+
}
|
|
234
|
+
catch (e) {
|
|
235
|
+
await this.errorLog(`Failed to start BLE scanning. Error:${e}`);
|
|
236
|
+
}
|
|
321
237
|
// Set an event handler to monitor advertisement packets
|
|
322
238
|
switchbot.onadvertisement = async (ad) => {
|
|
323
239
|
try {
|
|
@@ -492,23 +408,6 @@ export class SwitchBotPlatform {
|
|
|
492
408
|
await this.errorLog(`Update Token: ${e}`);
|
|
493
409
|
}
|
|
494
410
|
}
|
|
495
|
-
generateHeaders = () => {
|
|
496
|
-
const t = `${Date.now()}`;
|
|
497
|
-
const nonce = randomUUID();
|
|
498
|
-
const data = this.config.credentials?.token + t + nonce;
|
|
499
|
-
const signTerm = crypto
|
|
500
|
-
.createHmac('sha256', this.config.credentials?.secret)
|
|
501
|
-
.update(Buffer.from(data, 'utf-8'))
|
|
502
|
-
.digest();
|
|
503
|
-
const sign = signTerm.toString('base64');
|
|
504
|
-
return {
|
|
505
|
-
'Authorization': this.config.credentials?.token,
|
|
506
|
-
'sign': sign,
|
|
507
|
-
'nonce': nonce,
|
|
508
|
-
't': t,
|
|
509
|
-
'Content-Type': 'application/json',
|
|
510
|
-
};
|
|
511
|
-
};
|
|
512
411
|
async discoverDevices() {
|
|
513
412
|
if (!this.config.credentials?.token) {
|
|
514
413
|
return this.handleManualConfig();
|
|
@@ -521,17 +420,15 @@ export class SwitchBotPlatform {
|
|
|
521
420
|
await this.debugWarnLog(`Delay Between Retries: ${delayBetweenRetries}`);
|
|
522
421
|
while (retryCount < maxRetries) {
|
|
523
422
|
try {
|
|
524
|
-
const {
|
|
525
|
-
await this.
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
await this.handleDevices(devicesAPI.body.deviceList);
|
|
530
|
-
await this.handleIRDevices(devicesAPI.body.infraredRemoteList);
|
|
423
|
+
const { response, statusCode } = await this.switchBotAPI.getDevices();
|
|
424
|
+
await this.debugLog(`response: ${JSON.stringify(response)}`);
|
|
425
|
+
if (this.isSuccessfulResponse(statusCode)) {
|
|
426
|
+
await this.handleDevices(Array.isArray(response.body.deviceList) ? response.body.deviceList : []);
|
|
427
|
+
await this.handleIRDevices(Array.isArray(response.body.infraredRemoteList) ? response.body.infraredRemoteList : []);
|
|
531
428
|
break;
|
|
532
429
|
}
|
|
533
430
|
else {
|
|
534
|
-
await this.handleErrorResponse(statusCode,
|
|
431
|
+
await this.handleErrorResponse(statusCode, retryCount, maxRetries, delayBetweenRetries);
|
|
535
432
|
retryCount++;
|
|
536
433
|
}
|
|
537
434
|
}
|
|
@@ -565,8 +462,8 @@ export class SwitchBotPlatform {
|
|
|
565
462
|
await this.errorLog('Neither SwitchBot Token or Device Config are set.');
|
|
566
463
|
}
|
|
567
464
|
}
|
|
568
|
-
isSuccessfulResponse(
|
|
569
|
-
return (
|
|
465
|
+
isSuccessfulResponse(apiStatusCode) {
|
|
466
|
+
return (apiStatusCode === 200 || apiStatusCode === 100);
|
|
570
467
|
}
|
|
571
468
|
async handleDevices(deviceLists) {
|
|
572
469
|
if (!this.config.options?.devices) {
|
|
@@ -631,11 +528,10 @@ export class SwitchBotPlatform {
|
|
|
631
528
|
return { ...matchingItem, ...itm };
|
|
632
529
|
});
|
|
633
530
|
}
|
|
634
|
-
async handleErrorResponse(
|
|
635
|
-
await this.statusCode(statusCode);
|
|
531
|
+
async handleErrorResponse(apiStatusCode, retryCount, maxRetries, delayBetweenRetries) {
|
|
636
532
|
await this.statusCode(apiStatusCode);
|
|
637
|
-
if (
|
|
638
|
-
this.infoLog(`statusCode: ${
|
|
533
|
+
if (apiStatusCode === 500) {
|
|
534
|
+
this.infoLog(`statusCode: ${apiStatusCode} Attempt ${retryCount + 1} of ${maxRetries}`);
|
|
639
535
|
await sleep(delayBetweenRetries);
|
|
640
536
|
}
|
|
641
537
|
}
|
|
@@ -2531,19 +2427,15 @@ export class SwitchBotPlatform {
|
|
|
2531
2427
|
await this.errorLog(message);
|
|
2532
2428
|
}
|
|
2533
2429
|
}
|
|
2534
|
-
async retryRequest(deviceMaxRetries, deviceDelayBetweenRetries
|
|
2430
|
+
async retryRequest(deviceId, deviceMaxRetries, deviceDelayBetweenRetries) {
|
|
2535
2431
|
let retryCount = 0;
|
|
2536
2432
|
const maxRetries = deviceMaxRetries;
|
|
2537
2433
|
const delayBetweenRetries = deviceDelayBetweenRetries;
|
|
2538
2434
|
while (retryCount < maxRetries) {
|
|
2539
2435
|
try {
|
|
2540
|
-
const {
|
|
2541
|
-
|
|
2542
|
-
|
|
2543
|
-
}
|
|
2544
|
-
else {
|
|
2545
|
-
await this.debugLog(`Received status code: ${statusCode}`);
|
|
2546
|
-
}
|
|
2436
|
+
const { response, statusCode } = await this.switchBotAPI.getDeviceStatus(deviceId);
|
|
2437
|
+
await this.warnLog(`response: ${JSON.stringify(response)}`);
|
|
2438
|
+
return { response, statusCode };
|
|
2547
2439
|
}
|
|
2548
2440
|
catch (error) {
|
|
2549
2441
|
await this.errorLog(`Error making request: ${error.message}`);
|
|
@@ -2552,7 +2444,7 @@ export class SwitchBotPlatform {
|
|
|
2552
2444
|
await this.debugLog(`Retry attempt ${retryCount} of ${maxRetries}`);
|
|
2553
2445
|
await sleep(delayBetweenRetries);
|
|
2554
2446
|
}
|
|
2555
|
-
return {
|
|
2447
|
+
return { response: null, statusCode: 500 };
|
|
2556
2448
|
}
|
|
2557
2449
|
// BLE Connection
|
|
2558
2450
|
async connectBLE(accessory, device) {
|