diff --git a/lib/options/switch.js b/lib/options/switch.js index 0ba5294..86d824f 100644 --- a/lib/options/switch.js +++ b/lib/options/switch.js @@ -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)); } }, diff --git a/lib/remote.js b/lib/remote.js index 519ba31..5bb3d74 100644 --- a/lib/remote.js +++ b/lib/remote.js @@ -20,6 +20,7 @@ const { GetApplicationsFailedError } = require('./errors'); +const RACE_TIMEOUT = 1500; const TURNING_TIMEOUT = 1000 * 5; const STANDBY_TIMEOUT = 1000 * 17; @@ -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() { @@ -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]); + } } \ No newline at end of file