From 5fdb543a17f10a527fc19a5355fc0e1185d81796 Mon Sep 17 00:00:00 2001 From: Ming Yi Date: Mon, 9 Nov 2020 15:48:55 +0800 Subject: [PATCH 01/12] Create EcoFanLightAccessory.js This fan supports ECO PO fans which has 6 speeds. --- lib/EcoFanLightAccessory.js | 165 ++++++++++++++++++++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 lib/EcoFanLightAccessory.js diff --git a/lib/EcoFanLightAccessory.js b/lib/EcoFanLightAccessory.js new file mode 100644 index 0000000..7986b97 --- /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) || '5'; + 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: 1 + }) + .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; From bc44ffd92ec673933336c3bcba70ceb69ae56c48 Mon Sep 17 00:00:00 2001 From: Ming Yi Date: Mon, 9 Nov 2020 15:50:40 +0800 Subject: [PATCH 02/12] Update index.js Adds EcoFanLightAccessory to valid type. This fan light type has 6 speeds instead of 3 found in simplefanlight. --- index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index efaf19f..d83d0fd 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; From 44f7f8c81cc5e0c5bdaf11ad158cedd4b7b88928 Mon Sep 17 00:00:00 2001 From: Ming Yi Date: Mon, 9 Nov 2020 15:59:56 +0800 Subject: [PATCH 03/12] Update package.json --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 462c89a..18c6baf 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.1", "description": "🏠 Offical Homebridge plugin for TuyAPI ", "main": "index.js", "scripts": { From 4d7ed0e8f8747a2ae8fc21f8f9edfb409eabcd0b Mon Sep 17 00:00:00 2001 From: Ming Yi Date: Mon, 9 Nov 2020 16:00:14 +0800 Subject: [PATCH 04/12] Update package-lock.json --- package-lock.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 431d502..24b26c4 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": { From 6772532776870fbf820fe64f7c2434ad69d00b36 Mon Sep 17 00:00:00 2001 From: Ming Yi Date: Mon, 9 Nov 2020 16:03:21 +0800 Subject: [PATCH 05/12] Update package.json --- package.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 18c6baf..a32923d 100644 --- a/package.json +++ b/package.json @@ -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", From e0b2b17dcde0a8cbf7b1f3c0bbec719c8e302693 Mon Sep 17 00:00:00 2001 From: Ming Yi Date: Mon, 9 Nov 2020 16:21:04 +0800 Subject: [PATCH 06/12] Update TuyaAccessory.js Add logging --- lib/TuyaAccessory.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/TuyaAccessory.js b/lib/TuyaAccessory.js index fecbc7d..6503af8 100644 --- a/lib/TuyaAccessory.js +++ b/lib/TuyaAccessory.js @@ -314,7 +314,8 @@ 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} : ${data.dps}`); + console.log(`[TuyaAccessory] Raw message from ${this.context.name} (${this.context.version}) with command ${cmd}:`, task.msg.toString('hex')); this._change(data.dps); } else { console.log(`[TuyaAccessory] Malformed message from ${this.context.name} with command ${cmd}:`, decryptedMsg); @@ -499,4 +500,4 @@ const getCRC32 = buffer => { }; -module.exports = TuyaAccessory; \ No newline at end of file +module.exports = TuyaAccessory; From e97eeaa50f4cc2dbdf5e826faa6c2ae4525c0fd3 Mon Sep 17 00:00:00 2001 From: Ming Yi Date: Mon, 9 Nov 2020 16:21:58 +0800 Subject: [PATCH 07/12] Update package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index a32923d..9032d01 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@newbishme/homebridge-tuya", - "version": "1.1.1", + "version": "1.1.2", "description": "🏠 Offical Homebridge plugin for TuyAPI ", "main": "index.js", "scripts": { From 54a8b481d4a9f9f6d16a608438b895dd298da5c7 Mon Sep 17 00:00:00 2001 From: Ming Yi Date: Mon, 9 Nov 2020 16:28:47 +0800 Subject: [PATCH 08/12] Update EcoFanLightAccessory.js --- lib/EcoFanLightAccessory.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/EcoFanLightAccessory.js b/lib/EcoFanLightAccessory.js index 7986b97..b98b97c 100644 --- a/lib/EcoFanLightAccessory.js +++ b/lib/EcoFanLightAccessory.js @@ -26,7 +26,7 @@ class EcoFanLightAccessory extends BaseAccessory { 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) || '5'; + 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; From da223c0e4829f7f2607ec6a36fcba666c83e165a Mon Sep 17 00:00:00 2001 From: Ming Yi Date: Mon, 9 Nov 2020 16:30:15 +0800 Subject: [PATCH 09/12] Update TuyaAccessory.js Inspect datapoint --- lib/TuyaAccessory.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/TuyaAccessory.js b/lib/TuyaAccessory.js index 6503af8..964b601 100644 --- a/lib/TuyaAccessory.js +++ b/lib/TuyaAccessory.js @@ -314,8 +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} : ${data.dps}`); - console.log(`[TuyaAccessory] Raw message from ${this.context.name} (${this.context.version}) with command ${cmd}:`, task.msg.toString('hex')); + 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); From 8c2f28784b62035e051eb7887b6dd45c26cb2d1a Mon Sep 17 00:00:00 2001 From: Ming Yi Date: Mon, 9 Nov 2020 16:30:29 +0800 Subject: [PATCH 10/12] Update package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 9032d01..7c5a432 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@newbishme/homebridge-tuya", - "version": "1.1.2", + "version": "1.1.3", "description": "🏠 Offical Homebridge plugin for TuyAPI ", "main": "index.js", "scripts": { From 0c58e33be7f476c067b1236452d388b98cfbd164 Mon Sep 17 00:00:00 2001 From: Ming Yi Date: Mon, 9 Nov 2020 16:39:12 +0800 Subject: [PATCH 11/12] Update package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 7c5a432..e03beee 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@newbishme/homebridge-tuya", - "version": "1.1.3", + "version": "1.1.4", "description": "🏠 Offical Homebridge plugin for TuyAPI ", "main": "index.js", "scripts": { From 3ce5bf91807d826e42692dbda21182bcfb73f80f Mon Sep 17 00:00:00 2001 From: Ming Yi Date: Mon, 9 Nov 2020 16:39:37 +0800 Subject: [PATCH 12/12] Update EcoFanLightAccessory.js --- lib/EcoFanLightAccessory.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/EcoFanLightAccessory.js b/lib/EcoFanLightAccessory.js index b98b97c..ab07f0c 100644 --- a/lib/EcoFanLightAccessory.js +++ b/lib/EcoFanLightAccessory.js @@ -56,7 +56,7 @@ class EcoFanLightAccessory extends BaseAccessory { .setProps({ minValue: 0, maxValue: 100, - minStep: 1 + minStep: 2 }) .updateValue(this.convertBrightnessFromTuyaToHomeKit(dps[this.dpBrightness])) .on('get', this.getBrightness.bind(this))