Skip to content

Commit

Permalink
Add max retry option for curtain (#631)
Browse files Browse the repository at this point in the history
* v2.3.2

* Add max retry option for curtain

Co-authored-by: Donavan Becker <beckersmarthome@icloud.com>
  • Loading branch information
dnicolson and donavanbecker committed Dec 27, 2022
1 parent cfaf0c5 commit 1e259ce
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
2 changes: 1 addition & 1 deletion config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@
"type": "number",
"placeholder": "5",
"condition": {
"functionBody": "return (model.options && model.options.devices && !model.options.devices[arrayIndices].hide_device && model.options.devices[arrayIndices].configDeviceType === 'Bot' && model.options.devices[arrayIndices].deviceId && model.options.devices[arrayIndices].bot && model.options.devices[arrayIndices].bot.mode);"
"functionBody": "return (model.options && model.options.devices && !model.options.devices[arrayIndices].hide_device && (model.options.devices[arrayIndices].configDeviceType === 'Bot' && model.options.devices[arrayIndices].deviceId && model.options.devices[arrayIndices].bot && model.options.devices[arrayIndices].bot.mode) || model.options.devices[arrayIndices].configDeviceType === 'Curtain');"
}
}
}
Expand Down
32 changes: 30 additions & 2 deletions src/device/curtain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -568,9 +568,15 @@ export class Curtain {
if (switchbot !== false) {
switchbot
.discover({ model: 'c', quick: true, id: this.device.bleMac })
.then((device_list: any) => {
.then(async (device_list: any) => {
this.infoLog(`${this.accessory.displayName} Target Position: ${this.TargetPosition}`);
return device_list[0].runToPos(100 - Number(this.TargetPosition), adjustedMode);
return await this.retry({
max: await this.maxRetry(),
switchbot,
fn: () => {
return device_list[0].runToPos(100 - Number(this.TargetPosition), adjustedMode);
},
});
})
.then(() => {
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
Expand All @@ -592,6 +598,28 @@ export class Curtain {
}
}

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 switchbot.wait(1000);
return this.retry({ max: max - 1, switchbot, fn });
});
}

async maxRetry(): Promise<number> {
let maxRetry: number;
if (this.device.curtain?.maxRetry) {
maxRetry = this.device.curtain?.maxRetry;
} else {
maxRetry = 5;
}
return maxRetry;
}

async openAPIpushChanges(): Promise<void> {
try {
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} OpenAPI pushChanges`);
Expand Down
1 change: 1 addition & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export type curtain = {
set_minStep?: number;
setCloseMode?: string;
setOpenMode?: string;
maxRetry?: number;
};

export type contact = {
Expand Down

0 comments on commit 1e259ce

Please sign in to comment.