Skip to content

Commit

Permalink
fixed curtain some issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Hanh94 committed Sep 8, 2021
1 parent a77115d commit 68684e3
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 50 deletions.
129 changes: 79 additions & 50 deletions lib/window_covering_accessory.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,43 @@ class WindowCoveringAccessory extends BaseAccessory {
refreshAccessoryServiceIfNeed(stateArr, isRefresh) {
this.isRefresh = isRefresh;
for (const statusMap of stateArr) {

//Check whether 100% is fully on or fully off. If there is no dp point, 100% is fully off by default
if (statusMap.code === 'situation_set') {
this.fullySituationMap = statusMap
}

// Characteristic.TargetPosition
if (statusMap.code === 'percent_control') {
this.percentControlMap = statusMap
this.normalAsync(Characteristic.TargetPosition, this._getReversedPercent(this.percentControlMap.value));
this.normalAsync(Characteristic.TargetPosition, this._getCorrectPercent(this.percentControlMap.value));

if (!this._isHaveDPCodeOfPercentState()) {
// Characteristic.CurrentPosition
this.normalAsync(Characteristic.CurrentPosition, this._getCorrectPercent(this.percentControlMap.value));
}

}

if (statusMap.code === 'position') {
this.percentControlMap = statusMap
const percent = this._getReversedPercent(parseInt(this.percentControlMap.value))
this.normalAsync(Characteristic.TargetPosition, percent, {
minStep: 5
});
const percent = this._getCorrectPercent(parseInt(this.percentControlMap.value))
this.normalAsync(Characteristic.TargetPosition, percent);

if (!this._isHaveDPCodeOfPercentState()) {
// Characteristic.CurrentPosition
this.normalAsync(Characteristic.CurrentPosition, percent);
}
}

if (statusMap.code === 'percent_state') {
// Characteristic.CurrentPosition
this.positionMap = statusMap
this.normalAsync(Characteristic.CurrentPosition, this._getReversedPercent(this.positionMap.value));
this.normalAsync(Characteristic.CurrentPosition, this._getCorrectPercent(this.positionMap.value));

// Characteristic.PositionState
let hbValue = this.getHomeBridgeParam(Characteristic.PositionState, this._getReversedPercent(this.positionMap.value));
this.normalAsync(Characteristic.PositionState, hbValue);
// let hbValue = this.getHomeBridgeParam(Characteristic.PositionState, this._getCorrectPercent(this.positionMap.value));
// this.normalAsync(Characteristic.PositionState, hbValue);
}
}
}
Expand All @@ -65,13 +80,13 @@ class WindowCoveringAccessory extends BaseAccessory {
}
})
.on('set', (hbValue, callback) => {
let percentValue = this._getReversedPercent(hbValue)
let percentValue = this._getCorrectPercent(hbValue)
let tuyaParam = this.getTuyaParam(name, percentValue);
this.platform.tuyaOpenApi.sendCommand(this.deviceId, tuyaParam).then(() => {
//store homebridge value
this.setCachedState(name, percentValue);
//store targetPosition value
this.targetPosition = percentValue;
this.setCachedState(name, hbValue);
// //store targetPosition value
// this.targetPosition = percentValue;
callback();
}).catch((error) => {
this.log.error('[SET][%s] Characteristic Error: %s', this.homebridgeAccessory.displayName, error);
Expand Down Expand Up @@ -109,21 +124,21 @@ class WindowCoveringAccessory extends BaseAccessory {
/**
* get HomeBridge param from tuya param
*/
getHomeBridgeParam(name, tuyaParam) {
if (Characteristic.PositionState === name) {
if (this.targetPosition) {
if (this.targetPosition > tuyaParam) {
return Characteristic.PositionState.INCREASING;
} else if (this.targetPosition < tuyaParam) {
return Characteristic.PositionState.DECREASING;
} else {
return Characteristic.PositionState.STOPPED;
}
} else {
return Characteristic.PositionState.STOPPED;
}
}
}
// getHomeBridgeParam(name, tuyaParam) {
// if (Characteristic.PositionState === name) {
// if (this.targetPosition) {
// if (this.targetPosition > tuyaParam) {
// return Characteristic.PositionState.INCREASING;
// } else if (this.targetPosition < tuyaParam) {
// return Characteristic.PositionState.DECREASING;
// } else {
// return Characteristic.PositionState.STOPPED;
// }
// } else {
// return Characteristic.PositionState.STOPPED;
// }
// }
// }

/**
* update HomeBridge state
Expand All @@ -142,37 +157,51 @@ class WindowCoveringAccessory extends BaseAccessory {
}
}

_getReversedPercent(value) {
_getCorrectPercent(value) {
var percent = value;
if (this._isMotorReversed()) {
if (this.fullySituationMap && this.fullySituationMap.value === 'fully_open') {
return percent
} else {
percent = 100 - percent;
return percent
}
return percent
}

//Check Motor Reversed
_isMotorReversed() {
let isMotorReversed
for (const statusMap of this.statusArr) {
switch (statusMap.code) {
case 'control_back_mode':
if (statusMap.value === 'forward') {
isMotorReversed = false;
} else {
isMotorReversed = true;
}
break;
case 'opposite':
case 'control_back':
isMotorReversed = statusMap.value;
break;
default:
break;
}

//Check whether the device supports percent_state dp code
_isHaveDPCodeOfPercentState() {
const percentStateDic = this.statusArr.find((item, index) => { return item.code.indexOf("percent_state") != -1 });
if (percentStateDic) {
return true;
} else {
return false;
}
return isMotorReversed;
}


//Check Motor Reversed
// _isMotorReversed() {
// let isMotorReversed
// for (const statusMap of this.statusArr) {
// switch (statusMap.code) {
// case 'control_back_mode':
// if (statusMap.value === 'forward') {
// isMotorReversed = false;
// } else {
// isMotorReversed = true;
// }
// break;
// case 'opposite':
// case 'control_back':
// isMotorReversed = statusMap.value;
// break;
// default:
// break;
// }
// }
// return isMotorReversed;
// }

/**
* Tuya MQTT update device status
*/
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"@clubedaentrega/cipher": "^1.0.0",
"aes-decrypter": "^3.1.2",
"axios": "^0.21.1",
"chai": "^4.3.4",
"crypto-js": "^4.0.0",
"js-base64": "^3.6.1",
"mqtt": "^4.2.6",
Expand Down
10 changes: 10 additions & 0 deletions test/datautil.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const expect = require('chai').expect;
const DataUtil = require('../util/datautil')


describe('DataUtil', function () {
it('getSubService() is not empty ', function () {
const device = new DataUtil().getSubService([{"code":"switch"},{"code":"switch2"},{"code":"switch_led"}]);
expect(device).to.not.be.empty;
});
});
23 changes: 23 additions & 0 deletions test/tuyashopenapi.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const expect = require('chai').expect;
const TuyaSHOpenAPI = require("../lib/tuyashopenapi");
const LogUtil = require('../util/logutil')

var api = new TuyaSHOpenAPI(
"xxxxxxxxxxxxxxxxxx",
"xxxxxxxxxxxxxxxxxx",
"xxxxxxxxxxxxxxxxxx",
"xxxxxxxxxxxxxxxxxx",
86,
"tuyaSmart",
new LogUtil(
false,
),
);

describe('TuyaSHOpenAPI', function () {
it('getDevices() is not empty ', async function () {
this.timeout(5000)
const device = await api.getDevices();
expect(device).to.not.be.empty;
});
});

0 comments on commit 68684e3

Please sign in to comment.