From b5bf9db9fd0bd78735ce574212dd858ca7d7a909 Mon Sep 17 00:00:00 2001 From: afbenevides Date: Sun, 25 Mar 2018 07:05:18 -0400 Subject: [PATCH] Added white color to light Added third position preset to window covering Modified config-sample.json --- accessories/light.js | 14 +++++- accessories/windowCovering.js | 93 +++++++++++++++++++---------------- config-sample.json | 8 ++- 3 files changed, 69 insertions(+), 46 deletions(-) diff --git a/accessories/light.js b/accessories/light.js index 9514dac9..0dcccb20 100644 --- a/accessories/light.js +++ b/accessories/light.js @@ -54,6 +54,7 @@ class LightAccessory extends SwitchAccessory { } async setSaturation () { + } @@ -83,9 +84,18 @@ class LightAccessory extends SwitchAccessory { // Find hue closest to the one requested const foundValues = this.dataKeys('hue'); const closest = foundValues.reduce((prev, curr) => Math.abs(curr - state.hue) < Math.abs(prev - state.hue) ? curr : prev); - const hexData = data[`hue${closest}`]; - log(`${name} setHue: (closest: hue${closest})`); + var hexData =""; + + if (state.saturation < 10) { + hexData = data[`white`]; + log(`${name} setHue: (closest: white)`); + } + else{ + hexData = data[`hue${closest}`]; + log(`${name} setHue: (closest: hue${closest})`); + } + await this.performSend(hexData); }); } diff --git a/accessories/windowCovering.js b/accessories/windowCovering.js index 68105c1b..b0b2a9a6 100644 --- a/accessories/windowCovering.js +++ b/accessories/windowCovering.js @@ -46,70 +46,79 @@ class WindowCoveringAccessory extends BroadlinkRMAccessory { // User requested a specific position or asked the window-covering to be open or closed async setTargetPosition (hexData, previousValue) { - await catchDelayCancelError(async () => { - const { config, host, debug, data, log, name, state, serviceManager } = this; - const { initialDelay } = config; - const { open, close, stop } = data; - - this.reset(); + await this.setTargetPositionActual(); + } - // Ignore if no change to the targetPosition - if (state.targetPosition === previousValue) return; + async setTargetPositionActual (hexData, previousValue) { + const { config, host, debug, data, log, name, state, serviceManager } = this; + const { initialDelay } = config; + const { open, close, stop, third_hex, third_position, third_span } = data; + + this.reset(); - // `initialDelay` allows multiple `window-covering` accessories to be updated at the same time - // without RF interference by adding an offset to each `window-covering` accessory - this.initialDelayPromise = delayForDuration(initialDelay); - await this.initialDelayPromise; + // Ignore if no change to the targetPosition + if (state.targetPosition === previousValue) return; - const closeCompletely = await this.checkOpenOrCloseCompletely(); - if (closeCompletely) return; + // `initialDelay` allows multiple `window-covering` accessories to be updated at the same time + // without RF interference by adding an offset to each `window-covering` accessory + this.initialDelayPromise = delayForDuration(initialDelay); + await this.initialDelayPromise; - log(`${name} setTargetPosition: (currentPosition: ${state.currentPosition})`); + const closeCompletely = await this.checkOpenOrCloseCompletely(); + if (closeCompletely) return; - // Determine if we're opening or closing - let difference = state.targetPosition - state.currentPosition; + log(`${name} setTargetPosition: (currentPosition: ${state.currentPosition})`); - state.opening = (difference > 0); - if (!state.opening) difference = -1 * difference; + // Determine if we're opening or closing + let difference = state.targetPosition - state.currentPosition; + state.opening = (difference > 0); + if (!state.opening) difference = -1 * difference; + + + if (state.targetPosition > (third_position - third_span) && state.targetPosition < (third_position + third_span)){ + log(`${name} ======= YES THERE IS A THIRD POSITION =======`); + log(`${name} ======= target position is : ${state.targetPosition} =======`); + hexData = third_hex + } + else { + log(`${name} ======= NO NO NO =======`); hexData = state.opening ? open : close + } - // Perform the actual open/close asynchronously i.e. without await so that HomeKit status can be updated - this.openOrClose({ hexData, previousValue }); - }); + // Perform the actual open/close asynchronously i.e. without await so that HomeKit status can be updated + this.openOrClose({ hexData, previousValue }); } async openOrClose ({ hexData, previousValue }) { - await catchDelayCancelError(async () => { - let { config, data, host, name, log, state, debug, serviceManager } = this; - let { totalDurationOpen, totalDurationClose } = config; - const { stop } = data; + let { config, data, host, name, log, state, debug, serviceManager } = this; + let { totalDurationOpen, totalDurationClose } = config; + const { stop } = data; - const newPositionState = state.opening ? Characteristic.PositionState.INCREASING : Characteristic.PositionState.DECREASING; - serviceManager.setCharacteristic(Characteristic.PositionState, newPositionState); + const newPositionState = state.opening ? Characteristic.PositionState.INCREASING : Characteristic.PositionState.DECREASING; + serviceManager.setCharacteristic(Characteristic.PositionState, newPositionState); - log(`${name} setTargetPosition: currently ${state.currentPosition}%, moving to ${state.targetPosition}%`); + log(`${name} setTargetPosition: currently ${state.currentPosition}%, moving to ${state.targetPosition}%`); - await this.performSend(hexData); + await this.performSend(hexData); - let difference = state.targetPosition - state.currentPosition - if (!state.opening) difference = -1 * difference; + let difference = state.targetPosition - state.currentPosition + if (!state.opening) difference = -1 * difference; - const fullOpenCloseTime = state.opening ? totalDurationOpen : totalDurationClose; - const durationPerPercentage = fullOpenCloseTime / 100; - const totalTime = durationPerPercentage * difference; + const fullOpenCloseTime = state.opening ? totalDurationOpen : totalDurationClose; + const durationPerPercentage = fullOpenCloseTime / 100; + const totalTime = durationPerPercentage * difference; - log(`${name} setTargetPosition: ${totalTime}s (${fullOpenCloseTime} / 100 * ${difference}) until auto-stop`); + log(`${name} setTargetPosition: ${totalTime}s (${fullOpenCloseTime} / 100 * ${difference}) until auto-stop`); - this.startUpdatingCurrentPositionAtIntervals(); + this.startUpdatingCurrentPositionAtIntervals(); - this.autoStopPromise = delayForDuration(totalTime); - await this.autoStopPromise; + this.autoStopPromise = delayForDuration(totalTime); + await this.autoStopPromise; - await this.stopWindowCovering(); + await this.stopWindowCovering(); - serviceManager.setCharacteristic(Characteristic.CurrentPosition, state.targetPosition); - }); + serviceManager.setCharacteristic(Characteristic.CurrentPosition, state.targetPosition); } async stopWindowCovering () { diff --git a/config-sample.json b/config-sample.json index b5289c8f..01d3283f 100644 --- a/config-sample.json +++ b/config-sample.json @@ -165,7 +165,8 @@ "hue99": "2600500000012...", "hue199": "2600500000012...", "hue299": "2600500000012...", - "hue359": "2600500000012..." + "hue359": "2600500000012...", + "white": "2600500000012..." } }, { @@ -264,7 +265,10 @@ "data": "CLOSE_HEX_2..." } ], - "stop":"STOP_HEX..." + "stop":"STOP_HEX...", + "third_hex":"THIRD_POSITION_MOTO_PRESET_HEX", + "third_position": 5, + "third_span": 3 } } ]