Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a fix to set blinds to correct open/close state #363

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 51 additions & 53 deletions lib/window_covering_accessory.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ class WindowCoveringAccessory extends BaseAccessory {
);
this.statusArr = deviceConfig.status;

this.movingStartTime = null;
this.movingDuration = null;
this.updateTimer = null;
this.cachedHbValue = 0;
this.totalOpenDuration = 30;

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

Expand All @@ -35,18 +41,21 @@ class WindowCoveringAccessory extends BaseAccessory {
// Characteristic.TargetPosition
if (statusMap.code === 'percent_control') {
this.percentControlMap = statusMap
this.normalAsync(Characteristic.TargetPosition, this._getCorrectPercent(this.percentControlMap.value));
const percent = this._getCorrectPercent(this.percentControlMap.value);
this.cachedHbValue = percent;
this.normalAsync(Characteristic.TargetPosition, percent);

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

}

if (statusMap.code === 'position') {
this.percentControlMap = statusMap
const percent = this._getCorrectPercent(parseInt(this.percentControlMap.value))
this.cachedHbValue = percent;
this.normalAsync(Characteristic.TargetPosition, percent);

if (!this._isHaveDPCodeOfPercentState()) {
Expand All @@ -60,9 +69,7 @@ class WindowCoveringAccessory extends BaseAccessory {
this.positionMap = statusMap
this.normalAsync(Characteristic.CurrentPosition, this._getCorrectPercent(this.positionMap.value));

// Characteristic.PositionState
// let hbValue = this.getHomeBridgeParam(Characteristic.PositionState, this._getCorrectPercent(this.positionMap.value));
// this.normalAsync(Characteristic.PositionState, hbValue);
this.normalAsync(Characteristic.PositionState, Characteristic.PositionState.STOPPED);
}
}
}
Expand All @@ -82,11 +89,17 @@ class WindowCoveringAccessory extends BaseAccessory {
.on('set', (hbValue, callback) => {
let percentValue = this._getCorrectPercent(hbValue)
let tuyaParam = this.getTuyaParam(name, percentValue);

this.platform.tuyaOpenApi.sendCommand(this.deviceId, tuyaParam).then(() => {
//store homebridge value
// this.log.debug("set blinds from: " + this.cachedHbValue + "; to: " + hbValue)
this.setCachedState(name, hbValue);
// //store targetPosition value
// this.targetPosition = percentValue;

this.movingStartTime = new Date().getTime();
let movingDuration = Math.abs(hbValue - this.cachedHbValue) * (this.totalOpenDuration / 100 * 1000);
this.cachedHbValue = hbValue;

this.startUpdateTimer(hbValue, movingDuration);
callback();
}).catch((error) => {
this.log.error('[SET][%s] Characteristic Error: %s', this.homebridgeAccessory.displayName, error);
Expand All @@ -96,8 +109,6 @@ class WindowCoveringAccessory extends BaseAccessory {
});
}



/**
* get Tuya param from HomeBridge param
*/
Expand All @@ -121,25 +132,6 @@ 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;
// }
// }
// }

/**
* update HomeBridge state
* @param {*} name HomeBridge Name
Expand All @@ -160,14 +152,13 @@ class WindowCoveringAccessory extends BaseAccessory {
_getCorrectPercent(value) {
var percent = value;
if (this.fullySituationMap && this.fullySituationMap.value === 'fully_open') {
return percent
return percent;
} else {
percent = 100 - percent;
return percent
return percent;
}
}


//Check whether the device supports percent_state dp code
_isHaveDPCodeOfPercentState() {
const percentStateDic = this.statusArr.find((item, index) => { return item.code.indexOf("percent_state") != -1 });
Expand All @@ -178,29 +169,36 @@ class WindowCoveringAccessory extends BaseAccessory {
}
}

startUpdateTimer(targetValue, movingDuration) {
this.stopUpdateTimer();
let hbValue;
if (this._getCorrectPercent(targetValue) > this._getCorrectPercent(this.cachedHbValue)) {
hbValue = Characteristic.PositionState.INCREASING;
} else if (this._getCorrectPercent(targetValue) < this._getCorrectPercent(this.cachedHbValue)) {
hbValue = Characteristic.PositionState.DECREASING;
}
this.normalAsync(Characteristic.PositionState, hbValue);
this.updateTimer = setTimeout(this.estimatedMovementStop.bind(this), movingDuration);
}

//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;
// }
stopUpdateTimer() {
if (this.updateTimer) {
clearTimeout(this.updateTimer);
this.updateTimer = null;
}
}

estimatedMovementStop() {
let refresh = this.isRefresh;
this.isRefresh = true;
this.normalAsync(Characteristic.TargetPosition, this.cachedHbValue);
this.normalAsync(Characteristic.CurrentPosition, this.cachedHbValue);
this.isRefresh = refresh;

this.movingStartTime = null;
this.movingDuration = null;
this.stopUpdateTimer();
}

/**
* Tuya MQTT update device status
Expand Down