Skip to content

Commit

Permalink
Support for older fan category "fan_speed"
Browse files Browse the repository at this point in the history
  • Loading branch information
Hanh94 committed Jun 21, 2021
1 parent 6287e96 commit fd1364f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 10 deletions.
42 changes: 36 additions & 6 deletions lib/fanv2_accessory.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ let Service;
let Characteristic;
let UUIDGen;

const DEFAULT_SPEED_COUNT = 3;
class Fanv2Accessory extends BaseAccessory {
constructor(platform, homebridgeAccessory, deviceConfig) {

Expand All @@ -25,7 +26,14 @@ class Fanv2Accessory extends BaseAccessory {
} else {
this.speed_range = { 'min': 1, 'max': 100 }
}


if (this.functionArr.length != 0) {
this.speed_count = this.getSpeedFunctionLevel("fan_speed")
} else {
this.speed_count = DEFAULT_SPEED_COUNT
}
this.speed_coefficient = 100 / this.speed_count

this.refreshAccessoryServiceIfNeed(this.statusArr, false);
}

Expand Down Expand Up @@ -91,6 +99,12 @@ class Fanv2Accessory extends BaseAccessory {
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 (statusMap.code === 'switch_vertical') {
this.swingMap = statusMap
var rawStatus = this.swingMap.value
Expand Down Expand Up @@ -181,10 +195,15 @@ class Fanv2Accessory extends BaseAccessory {
value = direction;
break;
case Characteristic.RotationSpeed:
var percentage;
percentage = Math.floor((value * this.speed_range.max - value * this.speed_range.min + 100 * this.speed_range.min) / 100); //1~100
const speed = percentage;
code = "fan_speed_percent";
let speed
if (this.speedMap.code === 'fan_speed_percent') {
speed = Math.floor((value * this.speed_range.max - value * this.speed_range.min + 100 * this.speed_range.min) / 100); //1~100
} else {
let level = Math.floor(value / this.speed_coefficient) + 1
level = level > this.speed_count ? this.speed_count : level;
speed = level;
}
code = this.speedMap.code;
value = speed;
break;
case Characteristic.SwingMode:
Expand Down Expand Up @@ -216,11 +235,22 @@ class Fanv2Accessory extends BaseAccessory {
let valueRange = JSON.parse(funcDic.values)
let isnull = (JSON.stringify(valueRange) == "{}")
return isnull ? { 'min': 1, 'max': 100 } : { 'min': parseInt(valueRange.min), 'max': parseInt(valueRange.max) };
}else{
} else {
return { 'min': 1, 'max': 100 }
}
}

getSpeedFunctionLevel(code) {
var funcDic = this.functionArr.find((item, index) => { return item.code == code })
if (funcDic) {
let value = JSON.parse(funcDic.values)
let isnull = (JSON.stringify(value) == "{}")
return isnull ? DEFAULT_SPEED_COUNT : value.range.length;
} else {
return DEFAULT_SPEED_COUNT;
}
}

//update device status
updateState(data) {
this.refreshAccessoryServiceIfNeed(data.status, true);
Expand Down
2 changes: 1 addition & 1 deletion lib/light_accessory.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ getAccessoryCharacteristic(name) {
this.setCachedState(name, value);
callback();
}).catch((error) => {
this.log.error('[SET][%s] Characteristic.Brightness Error: %s', this.homebridgeAccessory.displayName, error);
this.log.error('[SET][%s] Characteristic Error: %s', this.homebridgeAccessory.displayName, error);
this.invalidateCache();
callback(error);
});
Expand Down
2 changes: 1 addition & 1 deletion lib/tuyaopenapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ class TuyaOpenAPI {
'lang': this.lang,
'dev_lang': 'javascript',
'dev_channel': 'homebridge',
'devVersion': '1.1.9',
'devVersion': '1.2.0',

};
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 @@ -136,7 +136,7 @@ class TuyaSHOpenAPI {
'lang': this.lang,
'dev_lang': 'javascript',
'dev_channel': 'homebridge',
'devVersion': '1.1.9',
'devVersion': '1.2.0',

};
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.1.9",
"version": "1.2.0",
"description": "Official Homebridge plugin for Tuya Open API, maintained by the Tuya Developer Team.",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit fd1364f

Please sign in to comment.