dxdc/homebridge-blinds

Add ability to call command-line scripts (vs. HTTP requests)

dxdc opened this issue · 26 comments

The change is pretty simple, but it's already changes the core purpose of this plugin.
Are you sure you want to go this direction and expand its functionality?

dxdc commented

I think it expands a lot of functionality, so I'm open for it. The only thing is how to handle the configuration.

I was thinking maybe the user could use file:///.... in the url config, and if the system detected this protocol, it will redirect to use something like:

const exec = require('child_process').exec;
const url = require('url');

...

exec(url, (err, stdout, stderr) => {
  if (err) {
    //some err occurred
  } else {
   console.log(`stdout: ${stdout}`);
   console.log(`stderr: ${stderr}`);
  }

Are there already any updates on changing/ adding command-line features to this plugin?
I use the forked version mentioned above of this plugin, but it would be great if the user chooses this plugin to choose between cmd or HTTP request.

dxdc commented

@hauni97 I just pushed some changes in a beta version, would appreciate if you can test them.

e.g.,
npm install -g homebridge-blinds@1.3.23-beta.0

It works as follows:

  • If file:// is added as prefix for URL, it will be processed as a command line script. For example, file://echo "this is a test". Note that this is just used as a prefix and should not be url encoded. The file:// prefix is just stripped off before it is run.

  • Else, normal HTTP processing will occur.

This is a bit of a patch for now, I'm working on additional changes which will be part of a future version, but don't want to break anything for now.

Thanks. I just tried using it, and it gives me the error no response in HK; the blinds are not moving.

This is the Homebridge log after using it and filling in, up, down, pause + time it takes to upen & close:

[12/27/2020, 17:10:47] TypeError: Cannot read property 'match' of undefined
    at BlindsHTTPAccessory.httpRequest (/usr/local/lib/node_modules/homebridge-blinds/index.js:637:34)
    at BlindsHTTPAccessory.setTargetPosition (/usr/local/lib/node_modules/homebridge-blinds/index.js:402:10)
    at TargetPosition.emit (events.js:315:20)
    at TargetPosition.EventEmitter.emit (/usr/local/lib/node_modules/homebridge/node_modules/hap-nodejs/src/lib/EventEmitter.ts:42:22)
    at TargetPosition.Characteristic._this.setValue (/usr/local/lib/node_modules/homebridge/node_modules/hap-nodejs/src/lib/Characteristic.ts:652:12)
    at /usr/local/lib/node_modules/homebridge/node_modules/hap-nodejs/src/lib/Accessory.ts:1439:24
    at Array.forEach (<anonymous>)
    at Bridge.Accessory._this._handleSetCharacteristics (/usr/local/lib/node_modules/homebridge/node_modules/hap-nodejs/src/lib/Accessory.ts:1281:10)
    at HAPServer.emit (events.js:315:20)
    at HAPServer.EventEmitter.emit (/usr/local/lib/node_modules/homebridge/node_modules/hap-nodejs/src/lib/EventEmitter.ts:42:22)
Got SIGTERM, shutting down Homebridge...

[12/27/2020, 17:11:00] [Blinds Command] Name, up, and down commands are required configuration parameters.

I inserted the following into the UP, DOWN, PAUSE Command Script:

file://python /var/lib/homebridge/Blinds_UP.py

My python script contains the following (based on MQQT, changes depending on up, down, and pause):

import paho.mqtt.client as mqtt

#Copyright

client = mqtt.Client()
client.username_pw_set(username=username”, password=password”)
client.connect(“broker”, port, no)
client.publish(“topic”, “command _to_close_blinds");
client.disconnect();
dxdc commented

Can you send me your full config @hauni97 ?

dxdc commented

Also @hauni97 , please try:

npm install -g homebridge-blinds@1.3.23-beta.1

Found a minor issue.

Sure this is the config of my accessories for this specific plug-in and the test blind:

},
    "accessories": [
        {
            "name": "TEST",
            "up_url": "file://python /var/lib/homebridge/Blinds_UP.py",
            "motion_up_time": 25000,
            "down_url": "file//python /var/lib/homebridge/Blinds_DOWN.py",
            "motion_down_time": 25000,
            "stop_url": "python /var/lib/homebridge/Blinds_Pause.py",
            "accessory": "BlindsHTTP"
        }
  ],

Please let me know if you require any further info.

dxdc commented

"down_url": "file//python /var/lib/homebridge/Blinds_DOWN.py",

This needs to be:

"down_url": "file://python /var/lib/homebridge/Blinds_DOWN.py",

"stop_url": "python /var/lib/homebridge/Blinds_Pause.py",

This needs to be:

"stop_url": "file://python /var/lib/homebridge/Blinds_Pause.py",

dxdc commented

Btw, please remember to restart homebridge after installing the update and making the config changes.

Works now without an error, blinds are moving.

[12/27/2020, 17:42:58] [TEST] Requested Move down (to 81%)
[12/27/2020, 17:42:58] [TEST] Requested Move down (to 72%)
[12/27/2020, 17:42:58] [TEST] Move request sent (276 ms), waiting 7s (+ 0s response lag)...
[12/27/2020, 17:42:58] [TEST] Move request sent (211 ms), waiting 7s (+ 0s response lag)...
[12/27/2020, 17:43:02] [TEST] End Move down to 72% (target 72%)
[12/27/2020, 17:43:05] [TEST] Requesting stop
[12/27/2020, 17:43:05] [TEST] Requesting stop
[12/27/2020, 17:43:05] [TEST] Stop request sent
[12/27/2020, 17:43:05] [TEST] Stop request sent

When moving down: It stopped at the desired position for a few seconds but then kept moving down to full without doing anything in HK.
When moving up (in the range of 50%): Some desired positions seem to work after a couple more tests, probably a timing issue.
I will try to troubleshoot systematically later today and keep you up to date.

dxdc commented

Thanks @hauni97. It could also depend on your set up and how your MQTT<-->blinds interface is working.

I would also encourage you to add the verbose: true keyword to the config while troubleshooting, could be useful.

The plugin with the command line addition works perfectly, just tried it with five different binds.

Thanks for adding that feature.

dxdc commented

thanks for testing @hauni97! I'll update the docs and push this out as an official release shortly.

dxdc commented

Btw, @hauni97 would you be willing to publish anonymized versions of your scripts? Could be included in the wiki... I imagine useful for other users.

(And, I guess you did not use the position_url feature?)

dxdc commented

Btw, published as a new (official) release, although no code-based changes from the one you have - just documentation.

dxdc commented

Thx! I would suggest just writing an entry on the Wiki directly, it's user editable (no PR needed). I don't want to clutter the README unnecessarily.

@dxdc I tried to edit the wiki, are you sure it is publically editable?

When hitting the wiki button it redirects me to the main repo (https://github.com/dxdc/homebridge-blinds), no button to edit the wiki is shown as in other repositories.

dxdc commented

@hauni97 can you try again? I didn't have an initial page set up, not sure if that was an issue.

@dxdc Yes, there is a Wiki now, but no rights to edit it.

Maybe this helps: https://docs.github.com/en/free-pro-team@latest/github/building-a-strong-community/changing-access-permissions-for-wikis

"Collaborators can edit a public repository's wiki by default, but you can allow anyone with a GitHub account to edit your wiki."

dxdc commented

Sorry... try again @hauni97

@dxdc no worries it works now. I Will work on the wiki in the coming days and add useful content.