-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #403 from 05TEVE/main
Updating FanController logic to merge the setSpeed and SetActive functions.
- Loading branch information
Showing
2 changed files
with
311 additions
and
282 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,95 +1,119 @@ | ||
const BaseAccessory = require('./BaseAccessory'); | ||
|
||
class SimpleFanAccessory extends BaseAccessory { | ||
static getCategory(Categories) { | ||
return Categories.FAN; | ||
} | ||
|
||
constructor(...props) { | ||
super(...props); | ||
} | ||
|
||
_registerPlatformAccessory() { | ||
const {Service} = this.hap; | ||
|
||
this.accessory.addService(Service.Fan, this.device.context.name); | ||
|
||
super._registerPlatformAccessory(); | ||
} | ||
|
||
_registerCharacteristics(dps) { | ||
const {Service, Characteristic} = this.hap; | ||
const service = this.accessory.getService(Service.Fan); | ||
this._checkServiceName(service, this.device.context.name); | ||
|
||
this.dpActive = this._getCustomDP(this.device.context.dpActive) || '1'; | ||
this.dpRotationSpeed = this._getCustomDP(this.device.context.dpRotationSpeed) || '3'; | ||
|
||
const characteristicActive = service.getCharacteristic(Characteristic.On) | ||
.updateValue(this._getActive(dps[this.dpActive])) | ||
.on('get', this.getActive.bind(this)) | ||
.on('set', this.setActive.bind(this)); | ||
|
||
const characteristicRotationSpeed = service.getCharacteristic(Characteristic.RotationSpeed) | ||
.setProps({ | ||
minValue: 0, | ||
maxValue: 3, | ||
minStep: 1 | ||
}) | ||
.updateValue(this._getSpeed(dps[this.dpRotationSpeed])) | ||
.on('get', this.getSpeed.bind(this)) | ||
.on('set', this.setSpeed.bind(this)); | ||
} | ||
|
||
// State | ||
getActive(callback) { | ||
this.getState(this.dpActive, (err, dp) => { | ||
if (err) return callback(err); | ||
|
||
callback(null, this._getActive(dp)); | ||
}); | ||
} | ||
|
||
_getActive(dp) { | ||
const {Characteristic} = this.hap; | ||
|
||
return dp; | ||
} | ||
|
||
setActive(value, callback) { | ||
const {Characteristic} = this.hap; | ||
|
||
return this.setState(this.dpActive, value, callback); | ||
|
||
callback(); | ||
} | ||
|
||
// Speed | ||
getSpeed(callback) { | ||
this.getState(this.dpRotationSpeed, (err, dp) => { | ||
if (err) return callback(err); | ||
|
||
callback(null, this._getSpeed(dp)); | ||
}); | ||
} | ||
|
||
_getSpeed(dp) { | ||
const {Characteristic} = this.hap; | ||
// console.log("_getSpeed = " + dp); | ||
return dp; | ||
} | ||
|
||
setSpeed(value, callback) { | ||
const {Characteristic} = this.hap; | ||
if (value == 0) { | ||
return this.setState(this.dpActive, false, callback); | ||
} else { | ||
return this.setState(this.dpRotationSpeed, value.toString(), callback); | ||
} | ||
|
||
callback(); | ||
} | ||
|
||
} | ||
|
||
module.exports = SimpleFanAccessory; | ||
const BaseAccessory = require('./BaseAccessory'); | ||
|
||
class SimpleFanAccessory extends BaseAccessory { | ||
static getCategory(Categories) { | ||
return Categories.FAN; | ||
} | ||
|
||
constructor(...props) { | ||
super(...props); | ||
} | ||
|
||
_registerPlatformAccessory() { | ||
const {Service} = this.hap; | ||
this.accessory.addService(Service.Fan, this.device.context.name); | ||
super._registerPlatformAccessory(); | ||
} | ||
|
||
_registerCharacteristics(dps) { | ||
const {Service, Characteristic} = this.hap; | ||
const serviceFan = this.accessory.getService(Service.Fan); | ||
this._checkServiceName(serviceFan, this.device.context.name); | ||
this.dpFanOn = this._getCustomDP(this.device.context.dpFanOn) || '1'; | ||
this.dpRotationSpeed = this._getCustomDP(this.device.context.RotationSpeed) || '3'; | ||
this.maxSpeed = parseInt(this.device.context.maxSpeed) || 3; | ||
// This variable is here so that we can set the fans to turn onto speed one instead of 3 on start. | ||
this.fanDefaultSpeed = parseInt(this.device.context.fanDefaultSpeed) || 1; | ||
// This variable is here as a workaround to allow for the on/off function to work. | ||
this.fanCurrentSpeed = 0; | ||
|
||
const characteristicFanOn = serviceFan.getCharacteristic(Characteristic.On) | ||
.updateValue(this._getFanOn(dps[this.dpFanOn])) | ||
.on('get', this.getFanOn.bind(this)) | ||
.on('set', this.setFanOn.bind(this)); | ||
|
||
const characteristicRotationSpeed = serviceFan.getCharacteristic(Characteristic.RotationSpeed) | ||
.setProps({ | ||
minValue: 0, | ||
maxValue: this.maxSpeed, | ||
minStep: 1 | ||
}) | ||
.updateValue(this._getSpeed(dps[this.dpRotationSpeed])) | ||
.on('get', this.getSpeed.bind(this)) | ||
.on('set', this.setSpeed.bind(this)); | ||
|
||
this.device.on('change', (changes, state) => { | ||
|
||
if (changes.hasOwnProperty(this.dpFanOn) && characteristicFanOn.value !== changes[this.dpFanOn]) | ||
characteristicFanOn.updateValue(changes[this.dpFanOn]); | ||
|
||
if (changes.hasOwnProperty(this.dpRotationSpeed) && characteristicRotationSpeed.value !== changes[this.dpRotationSpeed]) | ||
characteristicRotationSpeed.updateValue(changes[this.dpRotationSpeed]); | ||
|
||
console.log('[Tuya] SimpleFan changed: ' + JSON.stringify(state)); | ||
}); | ||
} | ||
|
||
/*************************** FAN ***************************/ | ||
// Get the Current Fan State | ||
getFanOn(callback) { | ||
this.getState(this.dpFanOn, (err, dp) => { | ||
if (err) return callback(err); | ||
callback(null, this._getFanOn(dp)); | ||
}); | ||
} | ||
|
||
_getFanOn(dp) { | ||
const {Characteristic} = this.hap; | ||
return dp; | ||
} | ||
|
||
setFanOn(value, callback) { | ||
const {Characteristic} = this.hap; | ||
// This uses the multistate set command to send the fan on and speed request in one call. | ||
if (value == false ) { | ||
this.fanCurrentSpeed = 0; | ||
// This will turn off the fan speed if it is set to be 0. | ||
return this.setState(this.dpFanOn, false, callback); | ||
} else { | ||
if (this.fanCurrentSpeed === 0) { | ||
// The current fanDefaultSpeed Variable is there to have the fan set to a sensible default if turned on. | ||
return this.setMultiState({[this.dpFanOn]: value, [this.dpRotationSpeed]: this.fanDefaultSpeed.toString()}, callback); | ||
} else { | ||
// The current fanCurrentSpeed Variable is there to ensure the fan speed doesn't change if the fan is already on. | ||
return this.setMultiState({[this.dpFanOn]: value, [this.dpRotationSpeed]: this.fanCurrentSpeed.toString()}, callback); | ||
} | ||
} | ||
callback(); | ||
} | ||
|
||
// Get the Current Fan Speed | ||
getSpeed(callback) { | ||
this.getState(this.dpRotationSpeed, (err, dp) => { | ||
if (err) return callback(err); | ||
callback(null, this._getSpeed(dp)); | ||
}); | ||
} | ||
|
||
_getSpeed(dp) { | ||
const {Characteristic} = this.hap; | ||
return dp; | ||
} | ||
|
||
// Set the new fan speed | ||
setSpeed(value, callback) { | ||
const {Characteristic} = this.hap; | ||
if (value === 0) { | ||
// This is to set the fan speed variable to be 1 when the fan is off. | ||
return this.setMultiState({[this.dpFanOn]: false, [this.dpRotationSpeed]: this.fanDefaultSpeed.toString()}, callback); | ||
} else { | ||
// This is to set the fan speed variable to match the current speed. | ||
this.fanCurrentSpeed = value; | ||
// This uses the multistate set command to send the fan on and speed request in one call. | ||
return this.setMultiState({[this.dpFanOn]: true, [this.dpRotationSpeed]: value.toString()}, callback); | ||
} | ||
callback(); | ||
} | ||
} | ||
|
||
module.exports = SimpleFanAccessory; |
Oops, something went wrong.