Skip to content

Commit

Permalink
"node-switchbot": "2.5.0-beta.28"
Browse files Browse the repository at this point in the history
  • Loading branch information
donavanbecker committed Sep 30, 2024
1 parent 08f980b commit aeaacc7
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 61 deletions.
62 changes: 31 additions & 31 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
10 changes: 6 additions & 4 deletions src/device/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 3 additions & 2 deletions src/irdevice/irdevice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
46 changes: 23 additions & 23 deletions src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -2543,24 +2544,23 @@ 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}`)
}
retryCount++
await this.debugLog(`Retry attempt ${retryCount} of ${maxRetries}`)
await sleep(delayBetweenRetries)
}
return { body: null }
return { response: null, statusCode: 500 }
}

// BLE Connection
Expand Down

0 comments on commit aeaacc7

Please sign in to comment.