From c71ab75136df5f48e49b75dd35645815112fe15b Mon Sep 17 00:00:00 2001 From: Dave Nicolson Date: Sun, 12 Mar 2023 04:14:32 +0100 Subject: [PATCH] Fix async method calls (#690) * Fix async method calls * Use consistent error variable --- src/device/blindtilt.ts | 10 +++++----- src/device/bot.ts | 16 ++++++++-------- src/device/ceilinglight.ts | 12 ++++++------ src/device/colorbulb.ts | 24 ++++++++++++------------ src/device/curtain.ts | 10 +++++----- src/device/humidifier.ts | 4 ++-- src/device/motion.ts | 6 +++--- src/device/plug.ts | 12 ++++++------ src/device/robotvacuumcleaner.ts | 12 ++++++------ src/device/striplight.ts | 20 ++++++++++---------- 10 files changed, 63 insertions(+), 63 deletions(-) diff --git a/src/device/blindtilt.ts b/src/device/blindtilt.ts index 06196066..900b08a1 100644 --- a/src/device/blindtilt.ts +++ b/src/device/blindtilt.ts @@ -614,8 +614,8 @@ export class BlindTilt { this.infoLog(`${this.accessory.displayName} Target Position: ${this.TargetPosition}`); return await this.retry({ max: await this.maxRetry(), - fn: () => { - return device_list[0].runToPos(100 - Number(this.TargetPosition), adjustedMode); + fn: async () => { + return await device_list[0].runToPos(100 - Number(this.TargetPosition), adjustedMode); }, }); }) @@ -640,11 +640,11 @@ export class BlindTilt { } async retry({ max, fn }: { max: number; fn: { (): any; (): Promise } }): Promise { - return fn().catch(async (err: any) => { + return fn().catch(async (e: any) => { if (max === 0) { - throw err; + throw e; } - this.infoLog(err); + this.infoLog(e); this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Retrying`); await sleep(1000); return this.retry({ max: max - 1, fn }); diff --git a/src/device/bot.ts b/src/device/bot.ts index 2c9b47a7..e49dd9a5 100644 --- a/src/device/bot.ts +++ b/src/device/bot.ts @@ -630,9 +630,9 @@ export class Bot { this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Bot Mode: ${this.botMode}`); switchbot .discover({ model: 'H', quick: true, id: this.device.bleMac }) - .then((device_list: { press: (arg0: { id: string | undefined }) => any }[]) => { + .then(async (device_list: { press: (arg0: { id: string | undefined }) => any }[]) => { this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`); - return device_list[0].press({ id: this.device.bleMac }); + return await device_list[0].press({ id: this.device.bleMac }); }) .then(() => { this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`); @@ -660,11 +660,11 @@ export class Bot { this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`); return await this.retry({ max: await this.maxRetry(), - fn: () => { + fn: async () => { if (this.On) { - return device_list[0].turnOn({ id: this.device.bleMac }); + return await device_list[0].turnOn({ id: this.device.bleMac }); } else { - return device_list[0].turnOff({ id: this.device.bleMac }); + return await device_list[0].turnOff({ id: this.device.bleMac }); } }, }); @@ -1173,11 +1173,11 @@ export class Bot { } async retry({ max, fn }: { max: number; fn: { (): any; (): Promise } }): Promise { - return fn().catch(async (err: any) => { + return fn().catch(async (e: any) => { if (max === 0) { - throw err; + throw e; } - this.infoLog(err); + this.infoLog(e); this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Retrying`); await sleep(1000); return this.retry({ max: max - 1, fn }); diff --git a/src/device/ceilinglight.ts b/src/device/ceilinglight.ts index a51b5db2..a2fbd730 100644 --- a/src/device/ceilinglight.ts +++ b/src/device/ceilinglight.ts @@ -493,11 +493,11 @@ export class CeilingLight { this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`); return await this.retry({ max: await this.maxRetry(), - fn: () => { + fn: async () => { if (this.On) { - return device_list[0].turnOn({ id: this.device.bleMac }); + return await device_list[0].turnOn({ id: this.device.bleMac }); } else { - return device_list[0].turnOff({ id: this.device.bleMac }); + return await device_list[0].turnOff({ id: this.device.bleMac }); } }, }); @@ -966,11 +966,11 @@ export class CeilingLight { } async retry({ max, fn }: { max: number; fn: { (): any; (): Promise } }): Promise { - return fn().catch(async (err: any) => { + return fn().catch(async (e: any) => { if (max === 0) { - throw err; + throw e; } - this.infoLog(err); + this.infoLog(e); this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Retrying`); await sleep(1000); return this.retry({ max: max - 1, fn }); diff --git a/src/device/colorbulb.ts b/src/device/colorbulb.ts index 21a8c8cc..1670a784 100644 --- a/src/device/colorbulb.ts +++ b/src/device/colorbulb.ts @@ -528,11 +528,11 @@ export class ColorBulb { this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`); return await this.retry({ max: await this.maxRetry(), - fn: () => { + fn: async () => { if (this.On) { - return device_list[0].turnOn({ id: this.device.bleMac }); + return await device_list[0].turnOn({ id: this.device.bleMac }); } else { - return device_list[0].turnOff({ id: this.device.bleMac }); + return await device_list[0].turnOff({ id: this.device.bleMac }); } }, }); @@ -580,9 +580,9 @@ export class ColorBulb { model: 'u', id: this.device.bleMac, }) - .then((device_list: any) => { + .then(async (device_list: any) => { this.infoLog(`${this.accessory.displayName} Target Brightness: ${this.Brightness}`); - return device_list[0].setBrightness(this.Brightness); + return await device_list[0].setBrightness(this.Brightness); }) .then(() => { this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`); @@ -615,9 +615,9 @@ export class ColorBulb { model: 'u', id: this.device.bleMac, }) - .then((device_list: any) => { + .then(async (device_list: any) => { this.infoLog(`${this.accessory.displayName} Target ColorTemperature: ${this.ColorTemperature}`); - return device_list[0].setColorTemperature(this.ColorTemperature); + return await device_list[0].setColorTemperature(this.ColorTemperature); }) .then(() => { this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`); @@ -657,9 +657,9 @@ export class ColorBulb { model: 'u', id: this.device.bleMac, }) - .then((device_list: any) => { + .then(async (device_list: any) => { this.infoLog(`${this.accessory.displayName} Target RGB: ${this.Brightness, red, green, blue}`); - return device_list[0].setRGB(this.Brightness, red, green, blue); + return await device_list[0].setRGB(this.Brightness, red, green, blue); }) .then(() => { this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`); @@ -1123,11 +1123,11 @@ export class ColorBulb { } async retry({ max, fn }: { max: number; fn: { (): any; (): Promise } }): Promise { - return fn().catch(async (err: any) => { + return fn().catch(async (e: any) => { if (max === 0) { - throw err; + throw e; } - this.infoLog(err); + this.infoLog(e); this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Retrying`); await sleep(1000); return this.retry({ max: max - 1, fn }); diff --git a/src/device/curtain.ts b/src/device/curtain.ts index 24dde162..f448c750 100644 --- a/src/device/curtain.ts +++ b/src/device/curtain.ts @@ -575,8 +575,8 @@ export class Curtain { this.infoLog(`${this.accessory.displayName} Target Position: ${this.TargetPosition}`); return await this.retry({ max: await this.maxRetry(), - fn: () => { - return device_list[0].runToPos(100 - Number(this.TargetPosition), adjustedMode); + fn: async () => { + return await device_list[0].runToPos(100 - Number(this.TargetPosition), adjustedMode); }, }); }) @@ -601,11 +601,11 @@ export class Curtain { } async retry({ max, fn }: { max: number; fn: { (): any; (): Promise } }): Promise { - return fn().catch(async (err: any) => { + return fn().catch(async (e: any) => { if (max === 0) { - throw err; + throw e; } - this.infoLog(err); + this.infoLog(e); this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Retrying`); await sleep(1000); return this.retry({ max: max - 1, fn }); diff --git a/src/device/humidifier.ts b/src/device/humidifier.ts index 43aeca88..dd27b3a6 100644 --- a/src/device/humidifier.ts +++ b/src/device/humidifier.ts @@ -455,9 +455,9 @@ export class Humidifier { quick: true, id: this.device.bleMac, }) - .then((device_list: any) => { + .then(async (device_list: any) => { this.infoLog(`${this.accessory.displayName} Target Position: ${this.Active}`); - return device_list[0].percentage(this.RelativeHumidityHumidifierThreshold); + return await device_list[0].percentage(this.RelativeHumidityHumidifierThreshold); }) .then(() => { this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`); diff --git a/src/device/motion.ts b/src/device/motion.ts index 02df067c..7ea8d00a 100644 --- a/src/device/motion.ts +++ b/src/device/motion.ts @@ -456,11 +456,11 @@ export class Motion { } async retry({ max, fn }: { max: number; fn: { (): any; (): Promise } }): Promise { - return fn().catch(async (err: any) => { + return fn().catch(async (e: any) => { if (max === 0) { - throw err; + throw e; } - this.infoLog(err); + this.infoLog(e); this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Retrying`); await sleep(1000); return this.retry({ max: max - 1, fn }); diff --git a/src/device/plug.ts b/src/device/plug.ts index 1472e696..4158dc04 100644 --- a/src/device/plug.ts +++ b/src/device/plug.ts @@ -352,11 +352,11 @@ export class Plug { this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`); return await this.retry({ max: await this.maxRetry(), - fn: () => { + fn: async () => { if (this.On) { - return device_list[0].turnOn({ id: this.device.bleMac }); + return await device_list[0].turnOn({ id: this.device.bleMac }); } else { - return device_list[0].turnOff({ id: this.device.bleMac }); + return await device_list[0].turnOff({ id: this.device.bleMac }); } }, }); @@ -522,11 +522,11 @@ export class Plug { } async retry({ max, fn }: { max: number; fn: { (): any; (): Promise } }): Promise { - return fn().catch(async (err: any) => { + return fn().catch(async (e: any) => { if (max === 0) { - throw err; + throw e; } - this.infoLog(err); + this.infoLog(e); this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Retrying`); await sleep(1000); return this.retry({ max: max - 1, fn }); diff --git a/src/device/robotvacuumcleaner.ts b/src/device/robotvacuumcleaner.ts index 0e341cd0..68595663 100644 --- a/src/device/robotvacuumcleaner.ts +++ b/src/device/robotvacuumcleaner.ts @@ -363,11 +363,11 @@ export class RobotVacuumCleaner { this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`); return await this.retry({ max: await this.maxRetry(), - fn: () => { + fn: async () => { if (this.On) { - return device_list[0].turnOn({ id: this.device.bleMac }); + return await device_list[0].turnOn({ id: this.device.bleMac }); } else { - return device_list[0].turnOff({ id: this.device.bleMac }); + return await device_list[0].turnOff({ id: this.device.bleMac }); } }, }); @@ -646,11 +646,11 @@ export class RobotVacuumCleaner { } async retry({ max, fn }: { max: number; fn: { (): any; (): Promise } }): Promise { - return fn().catch(async (err: any) => { + return fn().catch(async (e: any) => { if (max === 0) { - throw err; + throw e; } - this.infoLog(err); + this.infoLog(e); this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Retrying`); await sleep(1000); return this.retry({ max: max - 1, fn }); diff --git a/src/device/striplight.ts b/src/device/striplight.ts index 3e912c91..eb8c020d 100644 --- a/src/device/striplight.ts +++ b/src/device/striplight.ts @@ -468,11 +468,11 @@ export class StripLight { this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`); return await this.retry({ max: await this.maxRetry(), - fn: () => { + fn: async () => { if (this.On) { - return device_list[0].turnOn({ id: this.device.bleMac }); + return await device_list[0].turnOn({ id: this.device.bleMac }); } else { - return device_list[0].turnOff({ id: this.device.bleMac }); + return await device_list[0].turnOff({ id: this.device.bleMac }); } }, }); @@ -516,9 +516,9 @@ export class StripLight { model: 'u', id: this.device.bleMac, }) - .then((device_list: any) => { + .then(async (device_list: any) => { this.infoLog(`${this.accessory.displayName} Target Brightness: ${this.Brightness}`); - return device_list[0].setBrightness(this.Brightness); + return await device_list[0].setBrightness(this.Brightness); }) .then(() => { this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`); @@ -557,9 +557,9 @@ export class StripLight { model: 'u', id: this.device.bleMac, }) - .then((device_list: any) => { + .then(async (device_list: any) => { this.infoLog(`${this.accessory.displayName} Target RGB: ${this.Brightness, red, green, blue}`); - return device_list[0].setRGB(this.Brightness, red, green, blue); + return await device_list[0].setRGB(this.Brightness, red, green, blue); }) .then(() => { this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`); @@ -887,11 +887,11 @@ export class StripLight { } async retry({ max, fn }: { max: number; fn: { (): any; (): Promise } }): Promise { - return fn().catch(async (err: any) => { + return fn().catch(async (e: any) => { if (max === 0) { - throw err; + throw e; } - this.infoLog(err); + this.infoLog(e); this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Retrying`); await sleep(1000); return this.retry({ max: max - 1, fn });