Skip to content

Commit

Permalink
Respond to power state change action in maximum set time #382
Browse files Browse the repository at this point in the history
  • Loading branch information
Octavian Ruda committed Oct 4, 2021
1 parent ae11fc9 commit b5bd9c4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/options/switch.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports = function(service, accessory) {
await Remote.setPower(true);
MainAccessory.services.main.updateValue(true);

await new Promise(resolve => setTimeout(resolve, 1500));
await new Promise(resolve => setTimeout(resolve, 1000));
}
},

Expand Down
15 changes: 14 additions & 1 deletion lib/remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const {
GetApplicationsFailedError
} = require('./errors');

const RACE_TIMEOUT = 1500;
const TURNING_TIMEOUT = 1000 * 5;
const STANDBY_TIMEOUT = 1000 * 17;

Expand Down Expand Up @@ -120,7 +121,7 @@ module.exports = class Remote {
}

async setPower(value) {
return value ? await this._turnOn() : await this._turnOff();
return await this._race(value ? this._turnOn() : this._turnOff());
}

async getArtPower() {
Expand Down Expand Up @@ -392,4 +393,16 @@ module.exports = class Remote {
this.device.log.debug(error.stack);
});
}

/**
* Promise with a timeout
* @param {Promise} promise
* @param {number} timeout
* @return {Promise}
*/
_race(promise, timeout = RACE_TIMEOUT) {
const race = new Promise((resolve) => setTimeout(resolve, timeout, true));

return Promise.race([promise, race]);
}
}

0 comments on commit b5bd9c4

Please sign in to comment.