Skip to content

Commit

Permalink
fix maxRetry on other deviceTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
donavanbecker committed Dec 27, 2022
1 parent b7a18ae commit 3b47951
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 110 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ All notable changes to this project will be documented in this file. This projec
## What's Changed

- Added a new `Bot` Device Type `Multi-Press`, Thanks [@alvst](https://github.com/alvst). [#628](https://github.com/OpenWonderLabs/homebridge-switchbot/issues/628)
- Added `maxRetry` option for `Curtain`, `Celing Light`, `Celing Light Pro`, `Plug`, `Plug Mini (US)`, `Plug Mini (JP)`, `Robot Vacuum Cleaner S1 Plus`, `Robot Vacuum Cleaner S1`, `Color Bulb`, and `Strip Light` Device Type, Thanks [@dnicolson](https://github.com/dnicolson). [#631](https://github.com/OpenWonderLabs/homebridge-switchbot/issues/631)
- Fix max retry option for `Bot` Device Type, Thanks [@dnicolson](https://github.com/dnicolson). [#630](https://github.com/OpenWonderLabs/homebridge-switchbot/issues/628)
- Added `maxRetry` option for `Curtain`, `Celing Light`, `Celing Light Pro`, `Plug`, `Plug Mini (US)`, `Plug Mini (JP)`, `Robot Vacuum Cleaner S1 Plus`, `Robot Vacuum Cleaner S1`, `Color Bulb`, and `Strip Light` Device Types, Thanks [@dnicolson](https://github.com/dnicolson). [#631](https://github.com/OpenWonderLabs/homebridge-switchbot/issues/631)
- Fix max retry option for `Bot`, `Curtain`, `Celing Light`, `Celing Light Pro`, `Plug`, `Plug Mini (US)`, `Plug Mini (JP)`, `Robot Vacuum Cleaner S1 Plus`, `Robot Vacuum Cleaner S1`, `Color Bulb`, and `Strip Light` Device Types, Thanks [@dnicolson](https://github.com/dnicolson). [#630](https://github.com/OpenWonderLabs/homebridge-switchbot/issues/628)
- Moved `maxRetry` option from `Bot` level to overall `configDeviceType` level.
- **If you had this set for your `Bot` you will have to update this config.**
- Housekeeping and updated dependencies.
Expand Down
33 changes: 15 additions & 18 deletions src/device/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -655,9 +655,19 @@ export class Bot {
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Press Mode: ${this.botMode}`);
switchbot
.discover({ model: 'H', quick: true, id: this.device.bleMac })
.then((device_list: any) => {
.then(async (device_list: any) => {
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
return this.turnOnOff(device_list);
return await this.retry({
max: await this.maxRetry(),
switchbot,
fn: () => {
if (this.On) {
return device_list[0].turnOn({ id: this.device.bleMac });
} else {
return device_list[0].turnOff({ id: this.device.bleMac });
}
},
});
})
.then(() => {
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
Expand Down Expand Up @@ -1162,28 +1172,15 @@ export class Bot {
}
}

async turnOnOff(device_list: any): Promise<any> {
return await this.retry({
max: await this.maxRetry(),
fn: () => {
if (this.On) {
return device_list[0].turnOn({ id: this.device.bleMac });
} else {
return device_list[0].turnOff({ id: this.device.bleMac });
}
},
});
}

async retry({ max, fn }: { max: number; fn: { (): any; (): Promise<any> } }): Promise<null> {
async retry({ max, switchbot, fn }: { max: number; switchbot: any, fn: { (): any; (): Promise<any> } }): Promise<null> {
return fn().catch(async (err: any) => {
if (max === 0) {
throw err;
}
this.infoLog(err);
this.infoLog('Retrying');
await this.switchbot.wait(1000);
return this.retry({ max: max - 1, fn });
await switchbot.wait(1000);
return this.retry({ max: max - 1, switchbot, fn });
});
}

Expand Down
33 changes: 15 additions & 18 deletions src/device/ceilinglight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,9 +488,19 @@ export class CeilingLight {
model: 'u',
id: this.device.bleMac,
})
.then((device_list: any) => {
.then(async (device_list: any) => {
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
return this.turnOnOff(device_list);
return await this.retry({
max: await this.maxRetry(),
switchbot,
fn: () => {
if (this.On) {
return device_list[0].turnOn({ id: this.device.bleMac });
} else {
return device_list[0].turnOff({ id: this.device.bleMac });
}
},
});
})
.then(() => {
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
Expand Down Expand Up @@ -955,28 +965,15 @@ export class CeilingLight {
}
}

async turnOnOff(device_list: any): Promise<any> {
return await this.retry({
max: await this.maxRetry(),
fn: () => {
if (this.On) {
return device_list[0].turnOn({ id: this.device.bleMac });
} else {
return device_list[0].turnOff({ id: this.device.bleMac });
}
},
});
}

async retry({ max, fn }: { max: number; fn: { (): any; (): Promise<any> } }): Promise<null> {
async retry({ max, switchbot, fn }: { max: number; switchbot: any, fn: { (): any; (): Promise<any> } }): Promise<null> {
return fn().catch(async (err: any) => {
if (max === 0) {
throw err;
}
this.infoLog(err);
this.infoLog('Retrying');
await this.switchbot.wait(1000);
return this.retry({ max: max - 1, fn });
await switchbot.wait(1000);
return this.retry({ max: max - 1, switchbot, fn });
});
}

Expand Down
33 changes: 15 additions & 18 deletions src/device/colorbulb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,9 +523,19 @@ export class ColorBulb {
model: 'u',
id: this.device.bleMac,
})
.then((device_list: any) => {
.then(async (device_list: any) => {
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
return this.turnOnOff(device_list);
return await this.retry({
max: await this.maxRetry(),
switchbot,
fn: () => {
if (this.On) {
return device_list[0].turnOn({ id: this.device.bleMac });
} else {
return device_list[0].turnOff({ id: this.device.bleMac });
}
},
});
})
.then(() => {
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
Expand Down Expand Up @@ -1112,28 +1122,15 @@ export class ColorBulb {
}
}

async turnOnOff(device_list: any): Promise<any> {
return await this.retry({
max: await this.maxRetry(),
fn: () => {
if (this.On) {
return device_list[0].turnOn({ id: this.device.bleMac });
} else {
return device_list[0].turnOff({ id: this.device.bleMac });
}
},
});
}

async retry({ max, fn }: { max: number; fn: { (): any; (): Promise<any> } }): Promise<null> {
async retry({ max, switchbot, fn }: { max: number; switchbot: any, fn: { (): any; (): Promise<any> } }): Promise<null> {
return fn().catch(async (err: any) => {
if (max === 0) {
throw err;
}
this.infoLog(err);
this.infoLog('Retrying');
await this.switchbot.wait(1000);
return this.retry({ max: max - 1, fn });
await switchbot.wait(1000);
return this.retry({ max: max - 1, switchbot, fn });
});
}

Expand Down
33 changes: 15 additions & 18 deletions src/device/plug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,19 @@ export class Plug {
model: this.BLEmodel(),
id: this.device.bleMac,
})
.then((device_list: any) => {
.then(async (device_list: any) => {
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
return this.turnOnOff(device_list);
return await this.retry({
max: await this.maxRetry(),
switchbot,
fn: () => {
if (this.On) {
return device_list[0].turnOn({ id: this.device.bleMac });
} else {
return device_list[0].turnOff({ id: this.device.bleMac });
}
},
});
})
.then(() => {
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
Expand Down Expand Up @@ -511,28 +521,15 @@ export class Plug {
}
}

async turnOnOff(device_list: any): Promise<any> {
return await this.retry({
max: await this.maxRetry(),
fn: () => {
if (this.On) {
return device_list[0].turnOn({ id: this.device.bleMac });
} else {
return device_list[0].turnOff({ id: this.device.bleMac });
}
},
});
}

async retry({ max, fn }: { max: number; fn: { (): any; (): Promise<any> } }): Promise<null> {
async retry({ max, switchbot, fn }: { max: number; switchbot: any, fn: { (): any; (): Promise<any> } }): Promise<null> {
return fn().catch(async (err: any) => {
if (max === 0) {
throw err;
}
this.infoLog(err);
this.infoLog('Retrying');
await this.switchbot.wait(1000);
return this.retry({ max: max - 1, fn });
await switchbot.wait(1000);
return this.retry({ max: max - 1, switchbot, fn });
});
}

Expand Down
33 changes: 15 additions & 18 deletions src/device/robotvacuumcleaner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,19 @@ export class RobotVacuumCleaner {
model: this.BLEmodel(),
id: this.device.bleMac,
})
.then((device_list: any) => {
.then(async (device_list: any) => {
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
return this.turnOnOff(device_list);
return await this.retry({
max: await this.maxRetry(),
switchbot,
fn: () => {
if (this.On) {
return device_list[0].turnOn({ id: this.device.bleMac });
} else {
return device_list[0].turnOff({ id: this.device.bleMac });
}
},
});
})
.then(() => {
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
Expand Down Expand Up @@ -635,28 +645,15 @@ export class RobotVacuumCleaner {
}
}

async turnOnOff(device_list: any): Promise<any> {
return await this.retry({
max: await this.maxRetry(),
fn: () => {
if (this.On) {
return device_list[0].turnOn({ id: this.device.bleMac });
} else {
return device_list[0].turnOff({ id: this.device.bleMac });
}
},
});
}

async retry({ max, fn }: { max: number; fn: { (): any; (): Promise<any> } }): Promise<null> {
async retry({ max, switchbot, fn }: { max: number; switchbot: any, fn: { (): any; (): Promise<any> } }): Promise<null> {
return fn().catch(async (err: any) => {
if (max === 0) {
throw err;
}
this.infoLog(err);
this.infoLog('Retrying');
await this.switchbot.wait(1000);
return this.retry({ max: max - 1, fn });
await switchbot.wait(1000);
return this.retry({ max: max - 1, switchbot, fn });
});
}

Expand Down
33 changes: 15 additions & 18 deletions src/device/striplight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -463,9 +463,19 @@ export class StripLight {
model: 'r',
id: this.device.bleMac,
})
.then((device_list: any) => {
.then(async (device_list: any) => {
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
return this.turnOnOff(device_list);
return await this.retry({
max: await this.maxRetry(),
switchbot,
fn: () => {
if (this.On) {
return device_list[0].turnOn({ id: this.device.bleMac });
} else {
return device_list[0].turnOff({ id: this.device.bleMac });
}
},
});
})
.then(() => {
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
Expand Down Expand Up @@ -876,28 +886,15 @@ export class StripLight {
}
}

async turnOnOff(device_list: any): Promise<any> {
return await this.retry({
max: await this.maxRetry(),
fn: () => {
if (this.On) {
return device_list[0].turnOn({ id: this.device.bleMac });
} else {
return device_list[0].turnOff({ id: this.device.bleMac });
}
},
});
}

async retry({ max, fn }: { max: number; fn: { (): any; (): Promise<any> } }): Promise<null> {
async retry({ max, switchbot, fn }: { max: number; switchbot: any, fn: { (): any; (): Promise<any> } }): Promise<null> {
return fn().catch(async (err: any) => {
if (max === 0) {
throw err;
}
this.infoLog(err);
this.infoLog('Retrying');
await this.switchbot.wait(1000);
return this.retry({ max: max - 1, fn });
await switchbot.wait(1000);
return this.retry({ max: max - 1, switchbot, fn });
});
}

Expand Down

0 comments on commit 3b47951

Please sign in to comment.