diff --git a/index.js b/index.js index 04b40a63..ab8b0923 100644 --- a/index.js +++ b/index.js @@ -125,6 +125,7 @@ class TuyaPlatform { case 'tgq': case 'xdd': case 'dc': + case 'tgkg': deviceAccessory = new LightAccessory(this, homebridgeAccessory, device); this.accessories.set(uuid, deviceAccessory.homebridgeAccessory); this.deviceAccessories.set(uuid, deviceAccessory); diff --git a/lib/fanv2_accessory.js b/lib/fanv2_accessory.js index 5d91fe70..23339cc6 100644 --- a/lib/fanv2_accessory.js +++ b/lib/fanv2_accessory.js @@ -21,13 +21,6 @@ class Fanv2Accessory extends BaseAccessory { this.statusArr = deviceConfig.status ? deviceConfig.status : []; this.functionArr = deviceConfig.functions ? deviceConfig.functions : []; - //get speed percent dp range - this.speed_range = this.getSpeedFunctionRange('fan_speed_percent') - - //get speed level dp range - this.speed_count = this.getSpeedFunctionLevel("fan_speed") - this.speed_coefficient = 100 / this.speed_count - this.refreshAccessoryServiceIfNeed(this.statusArr, false); } @@ -35,7 +28,7 @@ class Fanv2Accessory extends BaseAccessory { refreshAccessoryServiceIfNeed(statusArr, isRefresh) { this.isRefresh = isRefresh for (const statusMap of statusArr) { - if (statusMap.code === 'switch' || statusMap.code === 'fan_switch') { + if (statusMap.code === 'switch' || statusMap.code === 'fan_switch' || statusMap.code === 'switch_fan') { this.switchMap = statusMap const hbSwitch = this.tuyaParamToHomeBridge(Characteristic.Active, this.switchMap.value); this.normalAsync(Characteristic.Active, hbSwitch, this.isRefresh) @@ -60,6 +53,7 @@ class Fanv2Accessory extends BaseAccessory { if (statusMap.code === 'fan_speed_percent') { this.speedMap = statusMap + this.speed_range = this.getSpeedFunctionRange(this.speedMap.code) const rawValue = this.speedMap.value // 1~12 const value = Math.floor((rawValue * 100 - 100 * this.speed_range.min) / (this.speed_range.max - this.speed_range.min)); // 0-100 this.normalAsync(Characteristic.RotationSpeed, value, this.isRefresh) @@ -67,8 +61,18 @@ class Fanv2Accessory extends BaseAccessory { if (statusMap.code === 'fan_speed') { this.speedMap = statusMap; - const hbSpeed = parseInt(this.speedMap.value * this.speed_coefficient); - this.normalAsync(Characteristic.RotationSpeed, hbSpeed, this.isRefresh) + if ((typeof this.speedMap.value == 'string') && this.speedMap.value.constructor == String) { + //get speed level dp range + this.speed_count = this.getSpeedFunctionLevel(this.speedMap.code) + this.speed_coefficient = 100 / this.speed_count + const hbSpeed = parseInt(this.speedMap.value * this.speed_coefficient); + this.normalAsync(Characteristic.RotationSpeed, hbSpeed, this.isRefresh) + }else{ + this.speed_range = this.getSpeedFunctionRange(this.speedMap.code) + const rawValue = this.speedMap.value // 1~12 + const value = Math.floor((rawValue * 100 - 100 * this.speed_range.min) / (this.speed_range.max - this.speed_range.min)); // 0-100 + this.normalAsync(Characteristic.RotationSpeed, value, this.isRefresh) + } } if (statusMap.code === 'switch_vertical') { @@ -140,12 +144,12 @@ class Fanv2Accessory extends BaseAccessory { break; case Characteristic.RotationSpeed: let speed - if (this.speedMap.code === 'fan_speed_percent') { - speed = Math.floor((hbValue * this.speed_range.max - hbValue * this.speed_range.min + 100 * this.speed_range.min) / 100); //1~100 - } else { + if ((typeof this.speedMap.value == 'string') && this.speedMap.value.constructor == String) { let level = Math.floor(hbValue / this.speed_coefficient) + 1 level = level > this.speed_count ? this.speed_count : level; speed = "" + level; + }else{ + speed = Math.floor((hbValue * this.speed_range.max - hbValue * this.speed_range.min + 100 * this.speed_range.min) / 100); //1~100 } code = this.speedMap.code; value = speed; diff --git a/lib/light_accessory.js b/lib/light_accessory.js index 34d2cf19..29250a7f 100644 --- a/lib/light_accessory.js +++ b/lib/light_accessory.js @@ -16,7 +16,7 @@ class LightAccessory extends BaseAccessory { Service.Lightbulb ); this.isRefesh = false; - this.statusArr = deviceConfig.status? deviceConfig.status : []; + this.statusArr = deviceConfig.status ? deviceConfig.status : []; this.functionArr = deviceConfig.functions ? deviceConfig.functions : []; //Distinguish Tuya different devices under the same HomeBridge Service this.deviceCategorie = deviceConfig.category; @@ -33,7 +33,7 @@ class LightAccessory extends BaseAccessory { this.refreshAccessoryServiceIfNeed(this.statusArr, false); } - //init Or refresh AccessoryService + //init Or refresh AccessoryService refreshAccessoryServiceIfNeed(statusArr, isRefesh) { this.isRefesh = isRefesh; for (var statusMap of statusArr) { @@ -68,13 +68,22 @@ class LightAccessory extends BaseAccessory { } if (statusMap.code === 'colour_data' || statusMap.code === 'colour_data_v2') { this.colourData = statusMap; - this.colourObj = this.colourData.value === "" ? {"h":100.0,"s":100.0,"v":100.0} : JSON.parse(this.colourData.value) + this.colourObj = this.colourData.value === "" ? { "h": 100.0, "s": 100.0, "v": 100.0 } : JSON.parse(this.colourData.value) + + if (!this.getCachedState(Characteristic.Brightness)) { + var percentage; + percentage = Math.floor((this.colourObj.v - this.function_dp_range.bright_range.min) * 100 / (this.function_dp_range.bright_range.max - this.function_dp_range.bright_range.min)); // $ + percentage = percentage > 100 ? 100 : percentage + this.setCachedState(Characteristic.Brightness, percentage); + } + const hue = this.colourObj.h; // 0-359 this.normalAsync(Characteristic.Hue, hue, this.isRefesh) var saturation; saturation = Math.floor((this.colourObj.s - this.function_dp_range.saturation_range.min) * 100 / (this.function_dp_range.saturation_range.max - this.function_dp_range.saturation_range.min)); // saturation 0-100 this.normalAsync(Characteristic.Saturation, saturation > 100 ? 100 : saturation, this.isRefesh) + } } } @@ -82,15 +91,15 @@ class LightAccessory extends BaseAccessory { normalAsync(name, hbValue, isRefresh) { this.setCachedState(name, hbValue); if (isRefresh) { - this.service - .getCharacteristic(name) - .updateValue(hbValue); + this.service + .getCharacteristic(name) + .updateValue(hbValue); } else { - this.getAccessoryCharacteristic(name); + this.getAccessoryCharacteristic(name); } -} + } -getAccessoryCharacteristic(name) { + getAccessoryCharacteristic(name) { //set Accessory service Characteristic this.service.getCharacteristic(name) .on('get', callback => { @@ -118,12 +127,12 @@ getAccessoryCharacteristic(name) { }); } -//get Command SendData + //get Command SendData getSendParam(name, value) { var code; var value; switch (name) { - case Characteristic.On: + case Characteristic.On: const isOn = value ? true : false; code = this.switchLed.code; value = isOn; @@ -138,7 +147,7 @@ getAccessoryCharacteristic(name) { { var percentage; percentage = Math.floor((this.function_dp_range.bright_range.max - this.function_dp_range.bright_range.min) * value / 100 + this.function_dp_range.bright_range.min); // value 0~100 - if (!this.workMode || this.workMode.value === 'white') { + if (!this.workMode || this.workMode.value === 'white' || this.workMode.value === 'light_white') { code = this.brightValue.code; value = percentage; } else { @@ -180,7 +189,7 @@ getAccessoryCharacteristic(name) { } // deviceConfig.functions is null, return defaultdpRange - getDefaultDPRange(){ + getDefaultDPRange() { let defaultBrightRange let defaultTempRange let defaultSaturationRange @@ -189,7 +198,7 @@ getAccessoryCharacteristic(name) { case 'bright_value': if (this.deviceCategorie == 'dj') { defaultBrightRange = { 'min': 25, 'max': 255 } - }else if (this.deviceCategorie == 'xdd' || this.deviceCategorie == 'fwd' || this.deviceCategorie == 'tgq' || this.deviceCategorie == 'dd' || this.deviceCategorie == 'dc') { + } else if (this.deviceCategorie == 'xdd' || this.deviceCategorie == 'fwd' || this.deviceCategorie == 'tgq' || this.deviceCategorie == 'dd' || this.deviceCategorie == 'dc' || this.deviceCategorie == 'tgkg') { defaultBrightRange = { 'min': 10, 'max': 1000 } } break; @@ -200,22 +209,25 @@ getAccessoryCharacteristic(name) { case 'temp_value': if (this.deviceCategorie == 'dj') { defaultTempRange = { 'min': 0, 'max': 255 } - }else if (this.deviceCategorie == 'xdd' || this.deviceCategorie == 'fwd' || this.deviceCategorie == 'dd' || this.deviceCategorie == 'dc') { + } else if (this.deviceCategorie == 'xdd' || this.deviceCategorie == 'fwd' || this.deviceCategorie == 'dd' || this.deviceCategorie == 'dc') { defaultTempRange = { 'min': 0, 'max': 1000 } } break; case 'temp_value_v2': - defaultTempRange = { 'min': 0, 'max': 1000 } + defaultTempRange = { 'min': 0, 'max': 1000 } break; case 'colour_data': if (this.deviceCategorie == 'dj') { defaultSaturationRange = { 'min': 0, 'max': 255 } - }else if (this.deviceCategorie == 'xdd' || this.deviceCategorie == 'fwd' || this.deviceCategorie == 'dd' || this.deviceCategorie == 'dc') { + defaultBrightRange = { 'min': 25, 'max': 255 } + } else if (this.deviceCategorie == 'xdd' || this.deviceCategorie == 'fwd' || this.deviceCategorie == 'dd' || this.deviceCategorie == 'dc') { defaultSaturationRange = { 'min': 0, 'max': 1000 } + defaultBrightRange = { 'min': 10, 'max': 1000 } } break; case 'colour_data_v2': defaultSaturationRange = { 'min': 0, 'max': 1000 } + defaultBrightRange = { 'min': 10, 'max': 1000 } break; default: break; diff --git a/lib/tuyaopenapi.js b/lib/tuyaopenapi.js index db5bd285..a5b82f5a 100644 --- a/lib/tuyaopenapi.js +++ b/lib/tuyaopenapi.js @@ -170,7 +170,7 @@ class TuyaOpenAPI { 'lang': this.lang, 'dev_lang': 'javascript', 'dev_channel': 'homebridge', - 'devVersion': '1.3.0', + 'devVersion': '1.3.1', }; this.log.log(`TuyaOpenAPI request: method = ${method}, endpoint = ${this.endpoint}, path = ${path}, params = ${JSON.stringify(params)}, body = ${JSON.stringify(body)}, headers = ${JSON.stringify(headers)}`); diff --git a/lib/tuyashopenapi.js b/lib/tuyashopenapi.js index df75200f..cfef83d0 100644 --- a/lib/tuyashopenapi.js +++ b/lib/tuyashopenapi.js @@ -164,7 +164,7 @@ class TuyaSHOpenAPI { 'lang': this.lang, 'dev_lang': 'javascript', 'dev_channel': 'homebridge', - 'devVersion': '1.3.0', + 'devVersion': '1.3.1', }; this.log.log(`TuyaOpenAPI request: method = ${method}, endpoint = ${this.endpoint}, path = ${path}, params = ${JSON.stringify(params)}, body = ${JSON.stringify(body)}, headers = ${JSON.stringify(headers)}`); diff --git a/package.json b/package.json index 2052c1df..070d48b7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "homebridge-tuya-platform", - "version": "1.3.0", + "version": "1.3.1", "description": "Official Homebridge plugin for Tuya Open API, maintained by the Tuya Developer Team.", "main": "index.js", "scripts": {