@switchbot/homebridge-switchbot 3.8.3-beta.10 → 3.8.3-beta.12
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/dist/device/blindtilt.d.ts +1 -4
- package/dist/device/blindtilt.d.ts.map +1 -1
- package/dist/device/blindtilt.js +6 -6
- 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 +3 -3
- 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 +8 -8
- 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 +8 -8
- 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.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 +4 -4
- package/dist/device/curtain.js.map +1 -1
- package/dist/device/device.d.ts +3 -3
- package/dist/device/device.d.ts.map +1 -1
- package/dist/device/device.js +26 -12
- 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 +6 -6
- 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.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 +6 -6
- 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.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 +6 -6
- 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 +3 -3
- 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.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.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.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 +2 -2
- 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 +4 -4
- 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.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 +6 -6
- 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 +6 -6
- 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 +4 -4
- 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 +10 -10
- package/dist/irdevice/fan.js.map +1 -1
- package/dist/irdevice/irdevice.d.ts +2 -2
- package/dist/irdevice/irdevice.d.ts.map +1 -1
- package/dist/irdevice/irdevice.js +1 -7
- 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 +4 -4
- 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 +4 -4
- 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 +22 -22
- 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 +4 -4
- 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 +4 -4
- package/dist/irdevice/waterheater.js.map +1 -1
- package/dist/platform.d.ts +4 -13
- package/dist/platform.d.ts.map +1 -1
- package/dist/platform.js +25 -133
- 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 +3 -4
- package/src/device/blindtilt.ts +8 -11
- package/src/device/bot.ts +4 -7
- package/src/device/ceilinglight.ts +9 -12
- package/src/device/colorbulb.ts +9 -12
- package/src/device/contact.ts +1 -4
- package/src/device/curtain.ts +6 -9
- package/src/device/device.ts +24 -15
- package/src/device/fan.ts +7 -10
- package/src/device/hub.ts +1 -4
- package/src/device/humidifier.ts +7 -10
- package/src/device/iosensor.ts +1 -4
- package/src/device/lightstrip.ts +7 -10
- package/src/device/lock.ts +4 -7
- package/src/device/meter.ts +1 -4
- package/src/device/meterplus.ts +1 -4
- package/src/device/motion.ts +1 -4
- package/src/device/plug.ts +3 -6
- package/src/device/robotvacuumcleaner.ts +5 -12
- package/src/device/waterdetector.ts +1 -4
- package/src/irdevice/airconditioner.ts +7 -8
- package/src/irdevice/airpurifier.ts +7 -7
- package/src/irdevice/camera.ts +5 -5
- package/src/irdevice/fan.ts +11 -11
- package/src/irdevice/irdevice.ts +3 -11
- package/src/irdevice/light.ts +5 -5
- package/src/irdevice/other.ts +5 -5
- package/src/irdevice/tv.ts +23 -23
- package/src/irdevice/vacuumcleaner.ts +5 -5
- package/src/irdevice/waterheater.ts +5 -5
- package/src/platform.ts +25 -126
- 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,9 +100,7 @@ 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
|
-
dispatcher?: Dispatcher;
|
|
112
|
-
} & Omit<Dispatcher.RequestOptions, 'origin' | 'path' | 'method'> & Partial<Pick<Dispatcher.RequestOptions, 'method'>>): Promise<{
|
|
103
|
+
retryRequest(deviceId: string, deviceMaxRetries: number, deviceDelayBetweenRetries: number): Promise<{
|
|
113
104
|
body: any;
|
|
114
105
|
statusCode: number;
|
|
115
106
|
}>;
|
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,EAAmB,MAAM,EAAkB,MAAM,WAAW,CAAA;
|
|
1
|
+
{"version":3,"file":"platform.d.ts","sourceRoot":"","sources":["../src/platform.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAmB,MAAM,EAAkB,MAAM,WAAW,CAAA;AAGxE,OAAO,KAAK,EAAE,GAAG,EAAE,qBAAqB,EAAE,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AACxF,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,MAAM,CAAA;AAGtC,OAAO,KAAK,EAAE,aAAa,EAAE,eAAe,EAAW,uBAAuB,EAAE,MAAM,eAAe,CAA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O,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,9 +62,13 @@ 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;
|
|
69
|
+
this.switchBotAPI = new SwitchBotOpenAPI(this.config.credentials?.token, this.config.credentials?.secret);
|
|
70
|
+
// Set the log level
|
|
71
|
+
this.switchBotAPI.setLogLevel(LogLevel.DEBUG);
|
|
76
72
|
// only load if configured
|
|
77
73
|
if (!config) {
|
|
78
74
|
return;
|
|
@@ -181,123 +177,31 @@ export class SwitchBotPlatform {
|
|
|
181
177
|
if (this.config.options?.webhookURL) {
|
|
182
178
|
const url = this.config.options?.webhookURL;
|
|
183
179
|
try {
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
if (request.url === path && request.method === 'POST') {
|
|
190
|
-
request.on('data', async (data) => {
|
|
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
|
-
// }
|
|
212
|
-
}
|
|
213
|
-
catch (e) {
|
|
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
|
-
}),
|
|
180
|
+
this.switchBotAPI.setupWebhook(url);
|
|
181
|
+
// Listen for webhook events
|
|
182
|
+
this.switchBotAPI.on('webhookEvent', (body) => {
|
|
183
|
+
this.debugLog('Received webhook event:', body);
|
|
184
|
+
this.webhookEventHandler[body.context.deviceMac]?.(body.context);
|
|
231
185
|
});
|
|
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
186
|
}
|
|
238
187
|
catch (e) {
|
|
239
|
-
await this.errorLog(`Failed to
|
|
188
|
+
await this.errorLog(`Failed to setup webhook. Error:${e.message}`);
|
|
240
189
|
}
|
|
241
190
|
try {
|
|
242
|
-
|
|
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
|
-
}
|
|
191
|
+
this.switchBotAPI.updateWebhook(url);
|
|
258
192
|
}
|
|
259
193
|
catch (e) {
|
|
260
194
|
await this.errorLog(`Failed to update webhook. Error:${e.message}`);
|
|
261
195
|
}
|
|
262
196
|
try {
|
|
263
|
-
|
|
264
|
-
method: 'POST',
|
|
265
|
-
headers: this.generateHeaders(),
|
|
266
|
-
body: JSON.stringify({
|
|
267
|
-
action: 'queryUrl',
|
|
268
|
-
}),
|
|
269
|
-
});
|
|
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
|
-
}
|
|
197
|
+
this.switchBotAPI.queryWebhook();
|
|
279
198
|
}
|
|
280
199
|
catch (e) {
|
|
281
|
-
await this.errorLog(`Failed to query webhook. Error:${e}`);
|
|
200
|
+
await this.errorLog(`Failed to query webhook. Error:${e.message}`);
|
|
282
201
|
}
|
|
283
202
|
this.api.on('shutdown', async () => {
|
|
284
203
|
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
|
-
}
|
|
204
|
+
this.switchBotAPI.deleteWebhook(url);
|
|
301
205
|
}
|
|
302
206
|
catch (e) {
|
|
303
207
|
await this.errorLog(`Failed to delete webhook. Error:${e.message}`);
|
|
@@ -317,7 +221,12 @@ export class SwitchBotPlatform {
|
|
|
317
221
|
(async () => {
|
|
318
222
|
// Start to monitor advertisement packets
|
|
319
223
|
await this.debugLog('Scanning for BLE SwitchBot devices...');
|
|
320
|
-
|
|
224
|
+
try {
|
|
225
|
+
await switchbot.startScan();
|
|
226
|
+
}
|
|
227
|
+
catch (e) {
|
|
228
|
+
await this.errorLog(`Failed to start BLE scanning. Error:${e}`);
|
|
229
|
+
}
|
|
321
230
|
// Set an event handler to monitor advertisement packets
|
|
322
231
|
switchbot.onadvertisement = async (ad) => {
|
|
323
232
|
try {
|
|
@@ -492,23 +401,6 @@ export class SwitchBotPlatform {
|
|
|
492
401
|
await this.errorLog(`Update Token: ${e}`);
|
|
493
402
|
}
|
|
494
403
|
}
|
|
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
404
|
async discoverDevices() {
|
|
513
405
|
if (!this.config.credentials?.token) {
|
|
514
406
|
return this.handleManualConfig();
|
|
@@ -521,7 +413,7 @@ export class SwitchBotPlatform {
|
|
|
521
413
|
await this.debugWarnLog(`Delay Between Retries: ${delayBetweenRetries}`);
|
|
522
414
|
while (retryCount < maxRetries) {
|
|
523
415
|
try {
|
|
524
|
-
const { body, statusCode } = await
|
|
416
|
+
const { body, statusCode } = await this.switchBotAPI.getDevices();
|
|
525
417
|
await this.debugWarnLog(`statusCode: ${statusCode}`);
|
|
526
418
|
const devicesAPI = await body.json();
|
|
527
419
|
await this.debugWarnLog(`devicesAPI: ${JSON.stringify(devicesAPI)}`);
|
|
@@ -2531,13 +2423,13 @@ export class SwitchBotPlatform {
|
|
|
2531
2423
|
await this.errorLog(message);
|
|
2532
2424
|
}
|
|
2533
2425
|
}
|
|
2534
|
-
async retryRequest(deviceMaxRetries, deviceDelayBetweenRetries
|
|
2426
|
+
async retryRequest(deviceId, deviceMaxRetries, deviceDelayBetweenRetries) {
|
|
2535
2427
|
let retryCount = 0;
|
|
2536
2428
|
const maxRetries = deviceMaxRetries;
|
|
2537
2429
|
const delayBetweenRetries = deviceDelayBetweenRetries;
|
|
2538
2430
|
while (retryCount < maxRetries) {
|
|
2539
2431
|
try {
|
|
2540
|
-
const { body, statusCode } = await
|
|
2432
|
+
const { body, statusCode } = await this.switchBotAPI.getDeviceStatus(deviceId);
|
|
2541
2433
|
if (statusCode === 200 || statusCode === 100) {
|
|
2542
2434
|
return { body, statusCode };
|
|
2543
2435
|
}
|