Skip to content

Commit

Permalink
1.fix "fskg" speed issue
Browse files Browse the repository at this point in the history
2.fix "dd" set color issue
3.fix Smart Air issue
4.fix curtain "cl" issue
5.support category "tgkg"
  • Loading branch information
Hanh94 committed Aug 4, 2021
1 parent 049828d commit 50ae20a
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 33 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
30 changes: 17 additions & 13 deletions lib/fanv2_accessory.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,14 @@ 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);
}

//init Or refresh AccessoryService
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)
Expand All @@ -60,15 +53,26 @@ 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)
}

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') {
Expand Down Expand Up @@ -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;
Expand Down
46 changes: 29 additions & 17 deletions lib/light_accessory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -68,29 +68,38 @@ 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)

}
}
}

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 => {
Expand Down Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -180,7 +189,7 @@ getAccessoryCharacteristic(name) {
}

// deviceConfig.functions is null, return defaultdpRange
getDefaultDPRange(){
getDefaultDPRange() {
let defaultBrightRange
let defaultTempRange
let defaultSaturationRange
Expand All @@ -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;
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion lib/tuyaopenapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`);
Expand Down
2 changes: 1 addition & 1 deletion lib/tuyashopenapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down

0 comments on commit 50ae20a

Please sign in to comment.