Skip to content

Commit

Permalink
Merge pull request #223 from NebzHB/beta
Browse files Browse the repository at this point in the history
Fix range to percentage issue
  • Loading branch information
NebzHB authored May 19, 2024
2 parents 7ce77a3 + f4a1899 commit 1d99d9e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
30 changes: 20 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3276,7 +3276,7 @@ JeedomPlatform.prototype.setAccessoryValue = function(value, characteristic, ser
action = 'setValue';
const oldValue = value;
value = 100 - value;// invert percentage
value = Math.round(((value / 100)*(service.maxValue-service.minValue))+service.minValue); // transform from percentage to scale
value = percentageToRange(value, service.minValue, service.maxValue); // transform from percentage to scale
if(value > service.infos.state.currentValue) {
service.Moving=Characteristic.PositionState.DECREASING;
} else if (value != service.infos.state.currentValue) {
Expand Down Expand Up @@ -3304,7 +3304,7 @@ JeedomPlatform.prototype.setAccessoryValue = function(value, characteristic, ser
action = 'setValue';
const oldValue = value;
value = 100 - value;// invert percentage
value = Math.round(((value / 100)*(service.maxValue-service.minValue))+service.minValue); // transform from percentage to scale
value = percentageToRange(value, service.minValue, service.maxValue); // transform from percentage to scale
if(value > service.infos.state.currentValue) {
service.Moving=Characteristic.PositionState.DECREASING;
} else if (value != service.infos.state.currentValue) {
Expand All @@ -3331,7 +3331,7 @@ JeedomPlatform.prototype.setAccessoryValue = function(value, characteristic, ser
} else {
action = 'setValue';
const oldValue = value;
value = Math.round(((value / 100)*(service.maxValue-service.minValue))+service.minValue);// transform from percentage to scale
value = percentageToRange(value, service.minValue, service.maxValue); // transform from percentage to scale
if(value > service.infos.state.currentValue) {
service.Moving=Characteristic.PositionState.INCREASING;
} else if (value != service.infos.state.currentValue) {
Expand All @@ -3358,7 +3358,7 @@ JeedomPlatform.prototype.setAccessoryValue = function(value, characteristic, ser
else if (service.actions.slider) {
action = 'setValue';
const oldValue = value;
value = Math.round(((value / 100)*(service.maxValue-service.minValue))+service.minValue);// transform from percentage to scale
value = percentageToRange(value, service.minValue, service.maxValue); // transform from percentage to scale
if(value > service.infos.state.currentValue) {
service.Moving=Characteristic.PositionState.INCREASING;
} else if (value != service.infos.state.currentValue) {
Expand Down Expand Up @@ -3390,7 +3390,7 @@ JeedomPlatform.prototype.setAccessoryValue = function(value, characteristic, ser
} else {
action = 'setValue';
const oldValue = value;
value = Math.round(((value / 100)*(service.maxValue-service.minValue))+service.minValue);
value = percentageToRange(value, service.minValue, service.maxValue); // transform from percentage to scale
if(value > service.infos.state) {
service.Moving=Characteristic.PositionState.INCREASING;
} else if (value != service.infos.state) {
Expand All @@ -3415,7 +3415,7 @@ JeedomPlatform.prototype.setAccessoryValue = function(value, characteristic, ser
else if (service.actions.slider) {
action = 'setValue';
const oldValue = value;
value = Math.round(((value / 100)*(service.maxValue-service.minValue))+service.minValue);
value = percentageToRange(value, service.minValue, service.maxValue); // transform from percentage to scale
if(value > service.infos.state) {
service.Moving=Characteristic.PositionState.INCREASING;
} else if (value != service.infos.state) {
Expand Down Expand Up @@ -4612,7 +4612,7 @@ JeedomPlatform.prototype.getAccessoryValue = function(characteristic, service, i
for (const cmd of cmdList) {
if (cmd.generic_type == 'FLAP_STATE' && cmd.id == service.cmd_id) {
returnValue = parseInt(cmd.currentValue);
returnValue = Math.round(((returnValue-service.minValue) / (service.maxValue-service.minValue))*100);
returnValue = rangeToPercentage(returnValue, service.minValue, service.maxValue);

if(service.maxValue == 100) {
returnValue = returnValue > (service.maxValue-5) ? service.maxValue : returnValue; // >95% is 100% in home (flaps need yearly tunning)
Expand All @@ -4625,7 +4625,7 @@ JeedomPlatform.prototype.getAccessoryValue = function(characteristic, service, i
}
if (cmd.generic_type == 'FLAP_STATE_CLOSING' && cmd.id == service.cmd_id) {
returnValue = parseInt(cmd.currentValue);
returnValue = Math.round(((returnValue-service.minValue) / (service.maxValue-service.minValue))*100);
returnValue = rangeToPercentage(returnValue, service.minValue, service.maxValue);

if(service.maxValue == 100) {
returnValue = returnValue > (service.maxValue-5) ? service.maxValue : returnValue; // >95% is 100% in home (flaps need yearly tunning)
Expand All @@ -4639,7 +4639,7 @@ JeedomPlatform.prototype.getAccessoryValue = function(characteristic, service, i
}
if (cmd.generic_type == 'WINDOW_STATE' && cmd.id == service.cmd_id) {
returnValue = parseInt(cmd.currentValue);
returnValue = Math.round(((returnValue-service.minValue) / (service.maxValue-service.minValue))*100);
returnValue = rangeToPercentage(returnValue, service.minValue, service.maxValue);

if(returnValue === service.TargetValue) {service.Moving=Characteristic.PositionState.STOPPED;}
else if (service.TargetValue !== undefined && service.Moving===Characteristic.PositionState.STOPPED) {service.TargetValue=undefined;}
Expand All @@ -4651,7 +4651,7 @@ JeedomPlatform.prototype.getAccessoryValue = function(characteristic, service, i
case Characteristic.TargetPosition.UUID :
if(service.TargetValue === undefined) {
returnValue = parseInt(service.infos.state.currentValue);
returnValue = Math.round(((returnValue-service.minValue) / (service.maxValue-service.minValue))*100);
returnValue = rangeToPercentage(returnValue, service.minValue, service.maxValue);

if(service.maxValue == 100) {
returnValue = returnValue > (service.maxValue-5) ? service.maxValue : returnValue; // >95% is 100% in home (flaps need yearly tunning)
Expand Down Expand Up @@ -6435,6 +6435,16 @@ JeedomBridgedAccessory.prototype.delServices = function(accessory) {
}
};

// convert range to percentage
function rangeToPercentage(value, min, max) {
return Math.floor(((value - min) / (max - min)) * 100);
}

// convert percentage to range
function percentageToRange(percentage, min, max) {
return Math.ceil((percentage / 100) * (max - min) + min);
}

// -- hexToR
// -- Desc : take the R value in a html color string
// -- Params --
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@nebz/homebridge-jeedom",
"version": "v1.7.0",
"cust_serial": "0024",
"cust_serial": "0027",
"description": "Homebridge plugin for Jeedom ",
"main": "index.js",
"private":true,
Expand All @@ -26,14 +26,14 @@
],
"dependencies": {
"async": "3.2.5",
"axios": "1.6.7",
"express": "4.18.2",
"axios": "1.6.8",
"express": "4.19.2",
"fakegato-history": "NebzHB/fakegato-history#master"
},
"devDependencies": {
"@babel/core": ">=7.23.9",
"@babel/eslint-parser": "^7.23.10",
"eslint": "^8.56.0"
"@babel/core": ">=7.24.5",
"@babel/eslint-parser": "^7.24.5",
"eslint": "^8.57.0"
},
"author": "Nebz <[email protected]>",
"license": "GPL-2.0",
Expand Down

0 comments on commit 1d99d9e

Please sign in to comment.