From aeaacc7c718515aed2563a4a178fba055c18a025 Mon Sep 17 00:00:00 2001 From: Donavan Becker Date: Mon, 30 Sep 2024 15:04:22 -0500 Subject: [PATCH] "node-switchbot": "2.5.0-beta.28" --- package-lock.json | 62 ++++++++++++++++++++-------------------- package.json | 2 +- src/device/device.ts | 10 ++++--- src/irdevice/irdevice.ts | 5 ++-- src/platform.ts | 46 ++++++++++++++--------------- 5 files changed, 64 insertions(+), 61 deletions(-) diff --git a/package-lock.json b/package-lock.json index 8499865c..1910e378 100644 --- a/package-lock.json +++ b/package-lock.json @@ -23,7 +23,7 @@ "async-mqtt": "^2.6.3", "fakegato-history": "^0.6.5", "homebridge-lib": "^7.0.8", - "node-switchbot": "2.5.0-beta.25", + "node-switchbot": "2.5.0-beta.28", "rxjs": "^7.8.1" }, "devDependencies": { @@ -3381,47 +3381,47 @@ "optional": true }, "node_modules/@shikijs/core": { - "version": "1.20.0", - "resolved": "https://registry.npmjs.org/@shikijs/core/-/core-1.20.0.tgz", - "integrity": "sha512-KlO3iE0THzSdYkzDFugt8SHe6FR3qNYTkmpbdW1d6xo8juQkMjybxAw/cBi2npL2eb2F4PbbnSs5Z9tDusfvyg==", + "version": "1.21.0", + "resolved": "https://registry.npmjs.org/@shikijs/core/-/core-1.21.0.tgz", + "integrity": "sha512-zAPMJdiGuqXpZQ+pWNezQAk5xhzRXBNiECFPcJLtUdsFM3f//G95Z15EHTnHchYycU8kIIysqGgxp8OVSj1SPQ==", "dev": true, "license": "MIT", "dependencies": { - "@shikijs/engine-javascript": "1.20.0", - "@shikijs/engine-oniguruma": "1.20.0", - "@shikijs/types": "1.20.0", + "@shikijs/engine-javascript": "1.21.0", + "@shikijs/engine-oniguruma": "1.21.0", + "@shikijs/types": "1.21.0", "@shikijs/vscode-textmate": "^9.2.2", "@types/hast": "^3.0.4", "hast-util-to-html": "^9.0.3" } }, "node_modules/@shikijs/engine-javascript": { - "version": "1.20.0", - "resolved": "https://registry.npmjs.org/@shikijs/engine-javascript/-/engine-javascript-1.20.0.tgz", - "integrity": "sha512-ZUMo758uduM0Tfgzi/kd+0IKMbNdumCxxWjY36uf1DIs2Qyg9HIq3vA1Wfa/vc6HE7tHWFpANRi3mv7UzJ68MQ==", + "version": "1.21.0", + "resolved": "https://registry.npmjs.org/@shikijs/engine-javascript/-/engine-javascript-1.21.0.tgz", + "integrity": "sha512-jxQHNtVP17edFW4/0vICqAVLDAxmyV31MQJL4U/Kg+heQALeKYVOWo0sMmEZ18FqBt+9UCdyqGKYE7bLRtk9mg==", "dev": true, "license": "MIT", "dependencies": { - "@shikijs/types": "1.20.0", + "@shikijs/types": "1.21.0", "@shikijs/vscode-textmate": "^9.2.2", "oniguruma-to-js": "0.4.3" } }, "node_modules/@shikijs/engine-oniguruma": { - "version": "1.20.0", - "resolved": "https://registry.npmjs.org/@shikijs/engine-oniguruma/-/engine-oniguruma-1.20.0.tgz", - "integrity": "sha512-MQ40WkVTZk7by33ces4PGK6XNFSo6PYvKTSAr2kTWdRNhFmOcnaX+1XzvFwB26eySXR7U74t91czZ1qJkEgxTA==", + "version": "1.21.0", + "resolved": "https://registry.npmjs.org/@shikijs/engine-oniguruma/-/engine-oniguruma-1.21.0.tgz", + "integrity": "sha512-AIZ76XocENCrtYzVU7S4GY/HL+tgHGbVU+qhiDyNw1qgCA5OSi4B4+HY4BtAoJSMGuD/L5hfTzoRVbzEm2WTvg==", "dev": true, "license": "MIT", "dependencies": { - "@shikijs/types": "1.20.0", + "@shikijs/types": "1.21.0", "@shikijs/vscode-textmate": "^9.2.2" } }, "node_modules/@shikijs/types": { - "version": "1.20.0", - "resolved": "https://registry.npmjs.org/@shikijs/types/-/types-1.20.0.tgz", - "integrity": "sha512-y+EaDvU2K6/GaXOKXxJaGnr1XtmZMF7MfS0pSEDdxEq66gCtKsLwQvVwoQFdp7R7dLlNAro3ijEE19sMZ0pzqg==", + "version": "1.21.0", + "resolved": "https://registry.npmjs.org/@shikijs/types/-/types-1.21.0.tgz", + "integrity": "sha512-tzndANDhi5DUndBtpojEq/42+dpUF2wS7wdCDQaFtIXm3Rd1QkrcVgSSRLOvEwexekihOXfbYJINW37g96tJRw==", "dev": true, "license": "MIT", "dependencies": { @@ -6672,9 +6672,9 @@ } }, "node_modules/eslint-plugin-import-x": { - "version": "4.3.0", - "resolved": "https://registry.npmjs.org/eslint-plugin-import-x/-/eslint-plugin-import-x-4.3.0.tgz", - "integrity": "sha512-PxGzP7gAjF2DLeRnQtbYkkgZDg1intFyYr/XS1LgTYXUDrSXMHGkXx8++6i2eDv2jMs0jfeO6G6ykyeWxiFX7w==", + "version": "4.3.1", + "resolved": "https://registry.npmjs.org/eslint-plugin-import-x/-/eslint-plugin-import-x-4.3.1.tgz", + "integrity": "sha512-5TriWkXulDl486XnYYRgsL+VQoS/7mhN/2ci02iLCuL7gdhbiWxnsuL/NTcaKY9fpMgsMFjWZBtIGW7pb+RX0g==", "dev": true, "license": "MIT", "dependencies": { @@ -11956,9 +11956,9 @@ } }, "node_modules/node-switchbot": { - "version": "2.5.0-beta.25", - "resolved": "https://registry.npmjs.org/node-switchbot/-/node-switchbot-2.5.0-beta.25.tgz", - "integrity": "sha512-v/K7FdQDVObLmx2lkGoLZFee2gOPME0aOHJH5ZgBSap33gXOcyEAO0cCii0lu7fWzOz8sJ/ja6Lj3KwHMLcp5g==", + "version": "2.5.0-beta.28", + "resolved": "https://registry.npmjs.org/node-switchbot/-/node-switchbot-2.5.0-beta.28.tgz", + "integrity": "sha512-asiY7OF9WG3cFU9OVclxDiWifOGPysLdKWIuyegEEd6xyqyXOBhsgKGepS8FSoadjY1uCeE+dLH6y68mV7TYew==", "license": "MIT", "dependencies": { "@stoprocent/noble": "^1.15.0", @@ -14065,16 +14065,16 @@ } }, "node_modules/shiki": { - "version": "1.20.0", - "resolved": "https://registry.npmjs.org/shiki/-/shiki-1.20.0.tgz", - "integrity": "sha512-MZJJ1PCFsQB1Piq+25wiz0a75yUv8Q3/fzy7SzRx5ONdjdtGdyiKwYn8vb/FnK5kjS0voWGnPpjG16POauUR+g==", + "version": "1.21.0", + "resolved": "https://registry.npmjs.org/shiki/-/shiki-1.21.0.tgz", + "integrity": "sha512-apCH5BoWTrmHDPGgg3RF8+HAAbEL/CdbYr8rMw7eIrdhCkZHdVGat5mMNlRtd1erNG01VPMIKHNQ0Pj2HMAiog==", "dev": true, "license": "MIT", "dependencies": { - "@shikijs/core": "1.20.0", - "@shikijs/engine-javascript": "1.20.0", - "@shikijs/engine-oniguruma": "1.20.0", - "@shikijs/types": "1.20.0", + "@shikijs/core": "1.21.0", + "@shikijs/engine-javascript": "1.21.0", + "@shikijs/engine-oniguruma": "1.21.0", + "@shikijs/types": "1.21.0", "@shikijs/vscode-textmate": "^9.2.2", "@types/hast": "^3.0.4" } diff --git a/package.json b/package.json index 1e73ef82..657fbab7 100644 --- a/package.json +++ b/package.json @@ -81,7 +81,7 @@ "async-mqtt": "^2.6.3", "fakegato-history": "^0.6.5", "homebridge-lib": "^7.0.8", - "node-switchbot": "2.5.0-beta.25", + "node-switchbot": "2.5.0-beta.28", "rxjs": "^7.8.1" }, "devDependencies": { diff --git a/src/device/device.ts b/src/device/device.ts index 5222aa07..769d9540 100644 --- a/src/device/device.ts +++ b/src/device/device.ts @@ -344,12 +344,14 @@ export abstract class deviceBase { } } - async pushChangeRequest(bodyChange: bodyChange): Promise<{ body: any }> { - return this.platform.switchBotAPI.controlDevice(this.device.deviceId, bodyChange.command, bodyChange.parameter, bodyChange.commandType) + async pushChangeRequest(bodyChange: bodyChange): Promise<{ body: any, statusCode: number }> { + const { response, statusCode } = await this.platform.switchBotAPI.controlDevice(this.device.deviceId, bodyChange.command, bodyChange.parameter, bodyChange.commandType) + return { body: response, statusCode } } - async deviceRefreshStatus(): Promise<{ body: any }> { - return await this.platform.retryRequest(this.device.deviceId, this.deviceMaxRetries, this.deviceDelayBetweenRetries) + async deviceRefreshStatus(): Promise<{ body: any, statusCode: number }> { + const { response, statusCode } = await this.platform.retryRequest(this.device.deviceId, this.deviceMaxRetries, this.deviceDelayBetweenRetries) + return { body: response, statusCode } } async successfulStatusCodes(deviceStatus: any) { diff --git a/src/irdevice/irdevice.ts b/src/irdevice/irdevice.ts index 2a8a435e..bf6f59f8 100644 --- a/src/irdevice/irdevice.ts +++ b/src/irdevice/irdevice.ts @@ -125,8 +125,9 @@ export abstract class irdeviceBase { this.debugSuccessLog(`version: ${accessory.context.version}`) } - async pushChangeRequest(bodyChange: bodyChange): Promise<{ body: any }> { - return this.platform.switchBotAPI.controlDevice(this.device.deviceId, bodyChange.command, bodyChange.parameter, bodyChange.commandType) + async pushChangeRequest(bodyChange: bodyChange): Promise<{ body: any, statusCode: number }> { + const { response, statusCode } = await this.platform.switchBotAPI.controlDevice(this.device.deviceId, bodyChange.command, bodyChange.parameter, bodyChange.commandType) + return { body: response, statusCode } } async successfulStatusCodes(deviceStatus: any) { diff --git a/src/platform.ts b/src/platform.ts index d3085d47..bbbea88f 100644 --- a/src/platform.ts +++ b/src/platform.ts @@ -225,24 +225,17 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin { this.switchBotAPI.setupWebhook(url) // Listen for webhook events this.switchBotAPI.on('webhookEvent', (body) => { + if (this.config.options?.mqttURL) { + const mac = body.context.deviceMac?.toLowerCase().match(/[\s\S]{1,2}/g)?.join(':') + const options = this.config.options?.mqttPubOptions || {} + this.mqttClient?.publish(`homebridge-switchbot/webhook/${mac}`, `${JSON.stringify(body.context)}`, options) + } this.webhookEventHandler[body.context.deviceMac]?.(body.context) }) } catch (e: any) { await this.errorLog(`Failed to setup webhook. Error:${e.message}`) } - try { - this.switchBotAPI.updateWebhook(url) - } catch (e: any) { - await this.errorLog(`Failed to update webhook. Error:${e.message}`) - } - - try { - this.switchBotAPI.queryWebhook() - } catch (e: any) { - await this.errorLog(`Failed to query webhook. Error:${e.message}`) - } - this.api.on('shutdown', async () => { try { this.switchBotAPI.deleteWebhook(url) @@ -473,14 +466,22 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin { while (retryCount < maxRetries) { try { - const { response } = await this.switchBotAPI.getDevices() - await this.warnLog(`response: ${JSON.stringify(response)}`) - if (this.isSuccessfulResponse(response.statusCode)) { - await this.handleDevices(response.body.deviceList) - await this.handleIRDevices(response.body.infraredRemoteList) + const { response, statusCode } = await this.switchBotAPI.getDevices() + await this.debugLog(`response: ${JSON.stringify(response)}`) + if (this.isSuccessfulResponse(statusCode)) { + if (Array.isArray(response.deviceList)) { + await this.handleDevices(response.deviceList) + } else { + await this.errorLog('deviceList is not an array') + } + if (Array.isArray(response.infraredRemoteList)) { + await this.handleIRDevices(response.infraredRemoteList) + } else { + await this.errorLog('infraredRemoteList is not an array') + } break } else { - await this.handleErrorResponse(response.statusCode, retryCount, maxRetries, delayBetweenRetries) + await this.handleErrorResponse(statusCode, retryCount, maxRetries, delayBetweenRetries) retryCount++ } } catch (e: any) { @@ -2543,16 +2544,15 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin { } } - async retryRequest(deviceId: string, deviceMaxRetries: number, deviceDelayBetweenRetries: number): Promise<{ body: any }> { + async retryRequest(deviceId: string, deviceMaxRetries: number, deviceDelayBetweenRetries: number): Promise<{ response: any, statusCode: number }> { let retryCount = 0 const maxRetries = deviceMaxRetries const delayBetweenRetries = deviceDelayBetweenRetries while (retryCount < maxRetries) { try { - const { response } = await this.switchBotAPI.getDeviceStatus(deviceId) + const { response, statusCode } = await this.switchBotAPI.getDeviceStatus(deviceId) await this.warnLog(`response: ${JSON.stringify(response)}`) - const body = response - return body + return { response, statusCode } } catch (error: any) { await this.errorLog(`Error making request: ${error.message}`) } @@ -2560,7 +2560,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin { await this.debugLog(`Retry attempt ${retryCount} of ${maxRetries}`) await sleep(delayBetweenRetries) } - return { body: null } + return { response: null, statusCode: 500 } } // BLE Connection