Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3d3da5e

Browse files
neptunixDmitry Brigadirov
authored andcommitted
Added cmd_timeout parameter (setting commands execution timeout value)
1 parent 9347b04 commit 3d3da5e

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

README.md

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ This step is not required. HomeBridge with API 2.0 can handle configurations in
5050
"state_cmd": "ps4-waker search | grep -i '200 Ok'",
5151
"polling": true,
5252
"interval": 5,
53+
"cmd_timeout": 2000,
5354
"manufacturer": "Sony Corporation",
5455
"model": "CUH-1001A",
5556
"serial": "XXXXXXXXXXX"
@@ -58,19 +59,22 @@ This step is not required. HomeBridge with API 2.0 can handle configurations in
5859
```
5960

6061

61-
| Fields | Description | Required |
62-
|------------------|-------------------------------------------------------|----------|
63-
| platform | Must always be `cmdSwitch2`. | Yes |
64-
| name | For logging purposes. | No |
65-
| switches | Array of switch config (multiple switches supported). | Yes |
66-
| \|- name\* | Name of your device. | Yes |
67-
| \|- on_cmd | Command to turn on your device. | No |
68-
| \|- off_cmd | Command to turn off your device. | No |
69-
| \|- state_cmd | Command to detect an ON state of your device. | No |
70-
| \|- polling | State polling (Default false). | No |
71-
| \|- interval | Polling interval in `s` (Default 1s). | No |
72-
| \|- manufacturer | Manufacturer of your device. | No |
73-
| \|- model | Model of your device. | No |
74-
| \|- serial | Serial number of your device. | No |
62+
| Fields | Description | Required |
63+
|--------------------|-------------------------------------------------------|----------|
64+
| platform | Must always be `cmdSwitch2`. | Yes |
65+
| name | For logging purposes. | No |
66+
| switches | Array of switch config (multiple switches supported). | Yes |
67+
| \|- name\* | Name of your device. | Yes |
68+
| \|- on_cmd | Command to turn on your device. | No |
69+
| \|- off_cmd | Command to turn off your device. | No |
70+
| \|- state_cmd | Command to detect an ON state of your device. | No |
71+
| \|- polling | State polling (Default false). | No |
72+
| \|- interval | Polling interval in `s` (Default 1s). | No |
73+
| \|- cmd_timeout\*\*| Commands execution timeout in `ms` (Default 1000ms). | No |
74+
| \|- manufacturer | Manufacturer of your device. | No |
75+
| \|- model | Model of your device. | No |
76+
| \|- serial | Serial number of your device. | No |
7577

7678
\*Changing the switch `name` in `config.json` will create a new switch instead of renaming the existing one in HomeKit. It's strongly recommended that you rename the switch using a HomeKit app only.
79+
80+
\*\*Command execution is assumed 'Successful' if timeout occures.

index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ cmdSwitchPlatform.prototype.addAccessory = function (data) {
7373
// Confirm variable type
7474
data.polling = data.polling === true;
7575
data.interval = parseInt(data.interval, 10) || 1;
76+
data.cmd_timeout = parseInt(data.cmd_timeout, 10) || 1000;
7677
if (data.manufacturer) data.manufacturer = data.manufacturer.toString();
7778
if (data.model) data.model = data.model.toString();
7879
if (data.serial) data.serial = data.serial.toString();
@@ -85,6 +86,7 @@ cmdSwitchPlatform.prototype.addAccessory = function (data) {
8586
cache.state_cmd = data.state_cmd;
8687
cache.polling = data.polling;
8788
cache.interval = data.interval;
89+
cache.cmd_timeout = data.cmd_timeout;
8890
cache.manufacturer = data.manufacturer;
8991
cache.model = data.model;
9092
cache.serial = data.serial;
@@ -239,9 +241,9 @@ cmdSwitchPlatform.prototype.setPowerState = function (thisSwitch, state, callbac
239241
// Allow 1s to set state but otherwise assumes success
240242
tout = setTimeout(function () {
241243
tout = null;
242-
self.log("Turning " + (state ? "on " : "off ") + thisSwitch.name + " took too long, assuming success." );
244+
self.log("Turning " + (state ? "on " : "off ") + thisSwitch.name + " took too long [" + thisSwitch.cmd_timeout + " ms], assuming success." );
243245
callback();
244-
}, 1000);
246+
}, thisSwitch.cmd_timeout);
245247
}
246248

247249
// Method to handle identify request
@@ -375,6 +377,11 @@ cmdSwitchPlatform.prototype.configurationRequestHandler = function (context, req
375377
"id": "interval",
376378
"title": "Polling Interval",
377379
"placeholder": context.operation ? "Leave blank if unchanged" : "1"
380+
},
381+
{
382+
"id": "cmd_timeout",
383+
"title": "On/Off command execution timeout (ms)",
384+
"placeholder": context.operation ? "Leave blank if unchanged" : "1000"
378385
}, {
379386
"id": "manufacturer",
380387
"title": "Manufacturer",
@@ -429,6 +436,7 @@ cmdSwitchPlatform.prototype.configurationRequestHandler = function (context, req
429436
newSwitch.polling = false;
430437
}
431438
newSwitch.interval = userInputs.interval || newSwitch.interval;
439+
newSwitch.cmd_timeout = userInputs.cmd_timeout || newSwitch.cmd_timeout;
432440
newSwitch.manufacturer = userInputs.manufacturer;
433441
newSwitch.model = userInputs.model;
434442
newSwitch.serial = userInputs.serial;
@@ -480,6 +488,7 @@ cmdSwitchPlatform.prototype.configurationRequestHandler = function (context, req
480488
'state_cmd': accessory.context.state_cmd,
481489
'polling': accessory.context.polling,
482490
'interval': accessory.context.interval,
491+
'cmd_timeout': accessory.context.cmd_timeout,
483492
'manufacturer': accessory.context.manufacturer,
484493
'model': accessory.context.model,
485494
'serial': accessory.context.serial

0 commit comments

Comments
 (0)