diff --git a/index.js b/index.js index efaf19f0..d83d0fd7 100644 --- a/index.js +++ b/index.js @@ -21,6 +21,7 @@ const SimpleFanAccessory = require('./lib/SimpleFanAccessory'); const SimpleFanLightAccessory = require('./lib/SimpleFanLightAccessory'); const ValveAccessory = require('./lib/ValveAccessory'); const OilDiffuserAccessory = require('./lib/OilDiffuserAccessory'); +const EcoFanLightAccessory = require('./lib/EcoFanLightAccessory'); const PLUGIN_NAME = 'homebridge-tuya-lan'; const PLATFORM_NAME = 'TuyaLan'; @@ -45,7 +46,8 @@ const CLASS_DEF = { fan: SimpleFanAccessory, fanlight: SimpleFanLightAccessory, watervalve: ValveAccessory, - oildiffuser: OilDiffuserAccessory + oildiffuser: OilDiffuserAccessory, + ecofanlight: EcoFanLightAccessory }; let Characteristic, PlatformAccessory, Service, Categories, UUID; diff --git a/lib/EcoFanLightAccessory.js b/lib/EcoFanLightAccessory.js new file mode 100644 index 00000000..ab07f0c6 --- /dev/null +++ b/lib/EcoFanLightAccessory.js @@ -0,0 +1,165 @@ +const BaseAccessory = require('./BaseAccessory'); + +class EcoFanLightAccessory extends BaseAccessory { + static getCategory(Categories) { + return Categories.FANLIGHT; + } + + constructor(...props) { + super(...props); + } + + _registerPlatformAccessory() { + const {Service} = this.hap; + + this.accessory.addService(Service.Fan, this.device.context.name); + this.accessory.addService(Service.Lightbulb, this.device.context.name + " Light"); + + super._registerPlatformAccessory(); + } + + _registerCharacteristics(dps) { + const {Service, Characteristic} = this.hap; + const serviceFan = this.accessory.getService(Service.Fan); + const serviceLightbulb = this.accessory.getService(Service.Lightbulb); + this._checkServiceName(serviceFan, this.device.context.name); + this._checkServiceName(serviceLightbulb, this.device.context.name + " Light"); + + this.dpActive = this._getCustomDP(this.device.context.dpActive) || '1'; + this.dpRotationSpeed = this._getCustomDP(this.device.context.RotationSpeed) || '3'; + this.dpLightOn = this._getCustomDP(this.device.context.dpLightOn) || '9'; + this.dpBrightness = this._getCustomDP(this.device.context.dpBrightness) || '10'; + this.dpUseLight = this._getCustomDP(this.device.context.dpUseLight) || true; + + const characteristicActive = serviceFan.getCharacteristic(Characteristic.On) + .updateValue(this._getActive(dps[this.dpActive])) + .on('get', this.getActive.bind(this)) + .on('set', this.setActive.bind(this)); + + const characteristicRotationSpeed = serviceFan.getCharacteristic(Characteristic.RotationSpeed) + .setProps({ + minValue: 0, + maxValue: 6, + minStep: 1 + }) + .updateValue(this._getSpeed(dps[this.dpRotationSpeed])) + .on('get', this.getSpeed.bind(this)) + .on('set', this.setSpeed.bind(this)); + + if (this.dpUseLight === true) { + const characterLightOn = serviceLightbulb.getCharacteristic(Characteristic.On) + .updateValue(this._getLightOn(dps[this.dpLightOn])) + .on('get', this.getLightOn.bind(this)) + .on('set', this.setLightOn.bind(this)); + + const characteristicBrightness = serviceLightbulb.getCharacteristic(Characteristic.Brightness) + .setProps({ + minValue: 0, + maxValue: 100, + minStep: 2 + }) + .updateValue(this.convertBrightnessFromTuyaToHomeKit(dps[this.dpBrightness])) + .on('get', this.getBrightness.bind(this)) + .on('set', this.setBrightness.bind(this)); + } + } + +/*************************** FAN ***************************/ +// Fan 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(); + } + +// 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; +// 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(); + } + +/*************************** LIGHT ***************************/ +// Lightbulb State + getLightOn(callback) { + this.getState(this.dpLightOn, (err, dp) => { + if (err) return callback(err); + + callback(null, this._getLightOn(dp)); + }); + } + + _getLightOn(dp) { + const {Characteristic} = this.hap; + + return dp; + } + + setLightOn(value, callback) { + const {Characteristic} = this.hap; + + return this.setState(this.dpLightOn, value, callback); + + callback(); + } + +// Lightbulb Brightness + getBrightness(callback) { + this.getState(this.dpBrightness, (err, dp) => { + if (err) return callback(err); + + callback(null, this._getBrightness(dp)); + }); + } + + _getBrightness(dp) { + const {Characteristic} = this.hap; +// console.log("_getBrightness = " + dp); + return dp; + } + + setBrightness(value, callback) { + const {Characteristic} = this.hap; +// console.log("setBrightness - Raw value = " + value); + return this.setState(this.dpBrightness, value, callback); + + callback(); + } +} + +module.exports = EcoFanLightAccessory; diff --git a/lib/TuyaAccessory.js b/lib/TuyaAccessory.js index fecbc7dd..964b6013 100644 --- a/lib/TuyaAccessory.js +++ b/lib/TuyaAccessory.js @@ -314,7 +314,7 @@ class TuyaAccessory extends EventEmitter { case 10: if (data) { if (data.dps) { - console.log(`[TuyaAccessory] Heard back from ${this.context.name} with command ${cmd}`); + console.log(`[TuyaAccessory] Heard back from ${this.context.name} with command ${cmd}`, JSON.stringify(data.dps)); this._change(data.dps); } else { console.log(`[TuyaAccessory] Malformed message from ${this.context.name} with command ${cmd}:`, decryptedMsg); @@ -499,4 +499,4 @@ const getCRC32 = buffer => { }; -module.exports = TuyaAccessory; \ No newline at end of file +module.exports = TuyaAccessory; diff --git a/package-lock.json b/package-lock.json index 431d5027..24b26c4d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { - "name": "homebridge-tuya", - "version": "1.0.0", + "name": "@newbishme/homebridge-tuya", + "version": "1.1.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 462c89a2..e03beee2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "homebridge-tuya", - "version": "1.1.0", + "name": "@newbishme/homebridge-tuya", + "version": "1.1.4", "description": "🏠 Offical Homebridge plugin for TuyAPI ", "main": "index.js", "scripts": { @@ -13,14 +13,14 @@ }, "repository": { "type": "git", - "url": "git+https://github.com/iRayanKhan/homebridge-tuya.git" + "url": "git+https://github.com/newbishme/homebridge-tuya.git" }, - "author": "Rayan Khan", + "author": "Newbishme", "license": "MIT", "bugs": { - "url": "https://github.com/iRayanKhan/homebridge-tuya/issues" + "url": "https://github.com/newbishme/homebridge-tuya/issues" }, - "homepage": "https://github.com/iRayanKhan/homebridge-tuya#readme", + "homepage": "https://github.com/newbishme/homebridge-tuya#readme", "dependencies": { "async": "^3.1.0", "commander": "^3.0.1",