Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added white color to light #336

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions accessories/light.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class LightAccessory extends SwitchAccessory {
}

async setSaturation () {


}

Expand Down Expand Up @@ -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);
});
}
Expand Down
93 changes: 51 additions & 42 deletions accessories/windowCovering.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
8 changes: 6 additions & 2 deletions config-sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@
"hue99": "2600500000012...",
"hue199": "2600500000012...",
"hue299": "2600500000012...",
"hue359": "2600500000012..."
"hue359": "2600500000012...",
"white": "2600500000012..."
}
},
{
Expand Down Expand Up @@ -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
}
}
]
Expand Down