From 024d34103852768d7ec849371a4646ecf5f2276d Mon Sep 17 00:00:00 2001 From: Luke Rhodes Date: Wed, 1 Jan 2020 12:37:31 +1100 Subject: [PATCH] Support for resetting to a default fan speed each time a fan is turned on (#503) "alwaysResetToDefaults": true, "defaultFanSpeed": 30, --- accessories/fan.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/accessories/fan.js b/accessories/fan.js index 054692c0..92d1e7ea 100644 --- a/accessories/fan.js +++ b/accessories/fan.js @@ -5,13 +5,33 @@ const SwitchAccessory = require('./switch'); class FanAccessory extends SwitchAccessory { async setSwitchState (hexData, previousValue) { + const { config, state, serviceManager } = this; + if (!this.state.switchState) { this.lastFanSpeed = undefined; } + // Reset the fan speed back to the default speed when turned off + if (this.state.switchState === false && config && config.alwaysResetToDefaults) { + this.setDefaults(); + serviceManager.setCharacteristic(Characteristic.RotationSpeed, state.fanSpeed); + } + super.setSwitchState(hexData, previousValue); } + setDefaults () { + super.setDefaults(); + + let { config, state } = this; + + // Reset the fan speed back to the default speed when turned off + // This will also be called whenever homebridge is restarted + if (config && config.alwaysResetToDefaults) { + state.fanSpeed = (config.defaultFanSpeed !== undefined) ? config.defaultFanSpeed : 100; + } + } + async setFanSpeed (hexData) { const { data, host, log, state, name, debug} = this; @@ -30,7 +50,6 @@ class FanAccessory extends SwitchAccessory { }) if (foundSpeeds.length === 0) { - return log(`${name} setFanSpeed: No fan speed hex codes provided.`) }