From e7b50e53f1eadfa360c81e91b9c9f4a5f2608b48 Mon Sep 17 00:00:00 2001 From: Luke Rhodes Date: Wed, 14 Mar 2018 12:47:52 +1100 Subject: [PATCH] =?UTF-8?q?Support=20for=20multiple=20and=20repeat=20hex?= =?UTF-8?q?=20codes=20in=20the=20=E2=80=9Cwindow-covering=E2=80=9D=20acces?= =?UTF-8?q?sory.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- accessories/windowCovering.js | 59 ++++++++++++++++++++++++++++++++--- config-sample.json | 21 +++++++++++++ package.json | 2 +- 3 files changed, 76 insertions(+), 6 deletions(-) diff --git a/accessories/windowCovering.js b/accessories/windowCovering.js index 93a42fb6..85a7ff05 100644 --- a/accessories/windowCovering.js +++ b/accessories/windowCovering.js @@ -45,6 +45,17 @@ class WindowCoveringAccessory extends BroadlinkRMAccessory { this.autoStopPromise.cancel(); this.autoStopPromise = null; } + + // Clear Multi-hex timeouts + if (this.intervalTimeoutPromise) { + this.intervalTimeoutPromise.cancel(); + this.intervalTimeoutPromise = null; + } + + if (this.pauseTimeoutPromise) { + this.pauseTimeoutPromise.cancel(); + this.pauseTimeoutPromise = null; + } } // User requested a specific position or asked the window-covering to be open or closed @@ -86,10 +97,6 @@ class WindowCoveringAccessory extends BroadlinkRMAccessory { } async openOrClose ({ hexData, previousValue }) { - await this.openOrCloseActual({ hexData, previousValue }) - } - - async openOrCloseActual ({ hexData, previousValue }) { let { config, data, host, name, log, state, debug, serviceManager } = this; let { totalDurationOpen, totalDurationClose } = config; const { stop } = data; @@ -98,7 +105,8 @@ class WindowCoveringAccessory extends BroadlinkRMAccessory { serviceManager.setCharacteristic(Characteristic.PositionState, newPositionState); log(`${name} setTargetPosition: currently ${state.currentPosition}%, moving to ${state.targetPosition}%`); - sendData({ host, hexData, log, name, debug }); + + this.performSend(hexData); let difference = state.targetPosition - state.currentPosition if (!state.opening) difference = -1 * difference; @@ -200,6 +208,47 @@ class WindowCoveringAccessory extends BroadlinkRMAccessory { }); } + async performSend (hexData) { + const { debug, config, host, log, name } = this; + + if (typeof hexData === 'string') { + sendData({ host, hexData, log, name, debug }); + + return; + } + + await catchDelayCancelError(async () => { + // Itterate through each hex config in the array + for (let index = 0; index < hexData.length; index++) { + const { pause } = hexData[index] + + await this.performRepeatSend(hexData[index]); + + if (pause) { + this.pauseTimeoutPromise = delayForDuration(pause); + await this.pauseTimeoutPromise; + } + } + }); + } + + async performRepeatSend (hexConfig) { + const { host, log, name, debug } = this; + let { data, interval, sendCount } = hexConfig; + + interval = interval || 1; + + // Itterate through each hex config in the array + for (let index = 0; index < sendCount; index++) { + sendData({ host, hexData: data, log, name, debug }); + + if (index < sendCount - 1) { + this.intervalTimeoutPromise = delayForDuration(interval); + await this.intervalTimeoutPromise; + } + } + } + setupServiceManager () { const { data, log, name, serviceManagerType } = this; diff --git a/config-sample.json b/config-sample.json index af97a9aa..afe462a0 100644 --- a/config-sample.json +++ b/config-sample.json @@ -223,6 +223,27 @@ "close":"2600500000012...", "stop":"2600500000012..." } + }, + { + "name":"Blind Multi", + "type":"window-covering", + "totalDurationOpen": 5, + "totalDurationClose": 2, + "data":{ + "open":"OPEN_HEX...", + "close":[ + { + "data": "CLOSE_HEX_1...", + "sendCount": 2, + "interval": 0.3, + "pause": 0.3 + }, + { + "data": "CLOSE_HEX_2..." + } + ], + "stop":"STOP_HEX..." + } } ] } diff --git a/package.json b/package.json index 433bfb42..59851c80 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "homebridge-broadlink-rm", - "version": "3.0.3", + "version": "3.0.4", "description": "Broadlink RM plugin (including the mini and pro) for homebridge: https://github.com/nfarina/homebridge", "license": "ISC", "keywords": [