Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add max retry option for curtain #631

Merged
merged 3 commits into from
Dec 27, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,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);"
donavanbecker marked this conversation as resolved.
Show resolved Hide resolved
"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
4 changes: 2 additions & 2 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
@@ -1,7 +1,7 @@
{
"displayName": "Homebridge SwitchBot",
"name": "@switchbot/homebridge-switchbot",
"version": "2.3.1",
"version": "2.3.2",
"description": "The [Homebridge](https://homebridge.io) SwitchBot plugin allows you to access your [SwitchBot](https://www.switch-bot.com) device(s) from HomeKit.",
"author": "SwitchBot <support@wondertechlabs.com> (https://github.com/SwitchBot)",
"license": "ISC",
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 @@ -100,6 +100,7 @@ export type curtain = {
set_minStep?: number;
setCloseMode?: string;
setOpenMode?: string;
maxRetry?: number;
};

export type contact = {
Expand Down