Skip to content

Commit

Permalink
Merge pull request #78 from Garfonso/fix-manual-dimmer
Browse files Browse the repository at this point in the history
fix manual dimmer
  • Loading branch information
GermanBluefox authored Feb 13, 2020
2 parents c449496 + ef819ec commit 0ffe04b
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -1647,14 +1647,18 @@ class WebServer {
is_rgb_string: true,
getParser: (entity, attr, state) => {
state = state || {val: '#000000'};
let str = state.val;
if (str.charAt(0) === '#') {
let str = (state.val || '#000000').toString();
if (str[0] === '#') {
str = str.substring(1);
}
//from rgb to hsv:
const r = parseInt(str.substr(0, 2), 16) * 100/255;
const g = parseInt(str.substr(2, 2), 16) * 100/255;
const b = parseInt(str.substr(4, 2), 16) * 100/255;
if (!/^[\da-fA-F]{6}/.test(str)) {
this.log.error('Malformed rgb string ' + str + ' expecting six hex digits.');
return;
}
// from rgb to hsv:
const r = parseInt(str.substr(0, 2), 16) * 100 / 255;
const g = parseInt(str.substr(2, 2), 16) * 100 / 255;
const b = parseInt(str.substr(4, 2), 16) * 100 / 255;
const hsv = this._RGB2HSV(r, g, b);
entity.attributes.hs_color = [hsv[0], hsv[1]];
attr.value = hsv[2]; //store value in attribute.
Expand All @@ -1677,10 +1681,11 @@ class WebServer {
return;
}

const redState = control.states.find(s => s.id && s.name === 'RED');
const redState = control.states.find(s => s.id && s.name === 'RED');
const greenState = control.states.find(s => s.id && s.name === 'GREEN');
const blueState = control.states.find(s => s.id && s.name === 'BLUE');
const blueState = control.states.find(s => s.id && s.name === 'BLUE');
const whiteState = control.states.find(s => s.id && s.name === 'WHITE');

if (redState && redState.id && greenState && greenState.id && blueState && blueState.id) {
//create main attribute for red:
const attribute = {
Expand All @@ -1691,7 +1696,7 @@ class WebServer {
max: objects[redState.id].common.max || 100,
getParser: (entity, attr, state) => {
state = state || {val: 0};
const r = state.val / attr.max * 255; //ok, we get red. Now do kind of a hack, calculate RGB from current setting and change r and recaluclate HS.
const r = state.val / attr.max * 255; // ok, we get red. Now do kind of a hack, calculate RGB from current setting and change r and recaluclate HS.
const hsv = [entity.attributes.hs_color[0], entity.attributes.hs_color[1], attribute.value || 100];
const rgb = this._HSV2RGB(hsv[0], hsv[1], hsv[2]);
const hsv_new = this._RGB2HSV(r, rgb.g, rgb.b);
Expand All @@ -1707,7 +1712,7 @@ class WebServer {
max: objects[greenState.id].common.max || 100,
getParser: (entity, attr, state) => {
state = state || {val: 0};
const g = state.val / attr.max * 255; //ok, we get green. Now do kind of a hack, calculate RGB from current setting and change r and recaluclate HS.
const g = state.val / attr.max * 255; // ok, we get green. Now do kind of a hack, calculate RGB from current setting and change r and recaluclate HS.
const hsv = [entity.attributes.hs_color[0], entity.attributes.hs_color[1], attribute.value || 100];
const rgb = this._HSV2RGB(hsv[0], hsv[1], hsv[2]);
const hsv_new = this._RGB2HSV(rgb.r, g, rgb.b);
Expand All @@ -1723,8 +1728,8 @@ class WebServer {
max: objects[blueState.id].common.max || 100,
getParser: (entity, attr, state) => {
state = state || {val: 0};
const b = state.val / attr.max * 255; //ok, we get blue. Now do kind of a hack, calculate RGB from current setting and change r and recaluclate HS.
const hsv = [entity.attributes.hs_color[0], entity.attributes.hs_color[1], attribute.value ||100];
const b = state.val / attr.max * 255; // ok, we get blue. Now do kind of a hack, calculate RGB from current setting and change r and recaluclate HS.
const hsv = [entity.attributes.hs_color[0], entity.attributes.hs_color[1], attribute.value || 100];
const rgb = this._HSV2RGB(hsv[0], hsv[1], hsv[2]);
const hsv_new = this._RGB2HSV(rgb.r, rgb.g, b);
entity.attributes.hs_color = [hsv_new[0], hsv_new[1]];
Expand Down Expand Up @@ -2026,7 +2031,20 @@ class WebServer {
service: 'turn_on',
setId: id,
on: iobMaxValue,
parseCommand: this._parseLightAdvancedOn.bind(this)
parseCommand: (entity, command, data, user) => {
let val = command.on;
if (data.service_data.brightness >= 0) {
entity.attributes.brightness = data.service_data.brightness;
entity.attributes.brightness_pct = data.service_data.brightness / 255;
val = data.service_data.brightness / 255 * iobMaxValuen;
}
if (data.service_data.brightness_pct >= 0) {
entity.attributes.brightness = (data.service_data.brightness_pct / 100) * 255;
entity.attributes.brightness_pct = data.service_data.brightness_pct;
val = data.service_data.brightness_pct / 100 * iobMaxValue;
}
return this.adapter.setForeignStateAsync(command.setId, val, false, {user});
}
},
{
service: 'turn_off',
Expand Down

0 comments on commit 0ffe04b

Please sign in to comment.