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.`) }