Skip to content

Commit

Permalink
feat: Possibility to invert the position value.
Browse files Browse the repository at this point in the history
  • Loading branch information
QuickSander committed Jul 9, 2021
1 parent 0c82c7a commit aec6e41
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Features:
* Set target position (0-100).
* Position change updates: push (efficient / less network and Homebridge load) or pull (easy to configure).
* Retrieve current position.
* Send identify request (via Eve Home app) to locate your curtain
* Send identify request (via Eve Home app) to locate your curtain.

## Installation

Expand Down Expand Up @@ -68,7 +68,8 @@ Any `%d` format specifier will be replaced by the requested target position.
* `getTargetPosRegEx` \<string\> **optional**: A regular expression from which the first matched group determines the target position.
* `identifyUrl` \<string | [urlObject](#urlobject)\> **optional**: URL to call when the HomeKit identify action is requested.
* `pullInterval` \<integer\> **optional**: The property expects an interval in **milliseconds** in which the plugin
pulls updates from your http device. For more information read [pulling updates](#the-pull-way).
pulls updates from your http device. For more information read [pulling updates](#the-pull-way).
* `invertPosition` \<boolean\> **optional**: True: interpret 0% as fully open and 100% as fully closed.

Below is an example configuration. One URL is using a simple string URL and the other is using an urlObject.
Both configs can be used for a basic plugin configuration.
Expand Down
9 changes: 8 additions & 1 deletion config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@
"description": "Leave empty to update position using push method only. Interval in milliseconds to pull position data from the curtain periodically. ",
"required": false,
"minimum": 0
},
"invertPosition": {
"title": "Invert position value",
"type": "boolean",
"description": "When selected will interpret 0% as fully opened and 100% as fully closed.",
"required": false
}
}
},
Expand Down Expand Up @@ -264,7 +270,8 @@
"identifyUrl.method"
]
},
"pullInterval"
"pullInterval",
"invertPosition"

]
}
Expand Down
21 changes: 17 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ function HttpCurtain(log, config) {
this.pullTimer.start();
}

this.invertPosition = config.invertPosition || false

api.on('didFinishLaunching', function() {
// Check if notificationRegistration is set, if not 'notificationRegistration' is probably not installed on the system.
if (global.notificationRegistration && typeof global.notificationRegistration === "function") {
Expand Down Expand Up @@ -191,9 +193,12 @@ HttpCurtain.prototype = {
this.log.warn("Your CurrentPosRegEx regular expression: \"%s\" did not match any part of the returned body: \"%s\"", this.getCurrentPosRegEx, body);
}
}
const posValue = parseInt(body);
let posValue = parseInt(body);
this.log.info("Current position (retrieved via http): %s\%", posValue);

this.log.info("Current position: %s\%", posValue);
if (this.invertPosition) {
posValue = 100 - posValue;
}

callback(null, posValue);
}
Expand Down Expand Up @@ -231,6 +236,10 @@ HttpCurtain.prototype = {
setTargetPosition: function (value, callback) {
this.targetPosition = value;

if (this.invertPosition) {
value = 100 - value;
}

// Replace %d with target position.
let urlObj = {...this.setTargetPosUrl};
urlObj.url = urlObj.url.replace(/%d/g, value.toString());
Expand All @@ -246,7 +255,7 @@ HttpCurtain.prototype = {
callback(new Error("Got http error code " + response.statusCode));
}
else {
this.log.debug("Succesfully requested target position: %d\%", this.targetPosition);
this.log.debug("Succesfully requested target position: %d\%", value);

callback(null);
}
Expand Down Expand Up @@ -276,9 +285,13 @@ HttpCurtain.prototype = {
}
}

const targetPosition = parseInt(body);
let targetPosition = parseInt(body);
this.log.info("Target position (retrieved via http): %s\%", targetPosition);

if (this.invertPosition) {
targetPosition = 100 - targetPosition;
}

callback(null, targetPosition);
}
});
Expand Down

0 comments on commit aec6e41

Please sign in to comment.