Skip to content

Commit

Permalink
Refactor color mode. #5514
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenkk committed Feb 23, 2023
1 parent 467803c commit ac726b5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 23 deletions.
8 changes: 4 additions & 4 deletions converters/fromZigbee.js
Original file line number Diff line number Diff line change
Expand Up @@ -601,8 +601,8 @@ const converters = {
}

if (msg.data.hasOwnProperty('colorMode')) {
result.color_mode = constants.colorMode.hasOwnProperty(msg.data['colorMode']) ?
constants.colorMode[msg.data['colorMode']] : msg.data['colorMode'];
result.color_mode = constants.colorModeLookup.hasOwnProperty(msg.data['colorMode']) ?
constants.colorModeLookup[msg.data['colorMode']] : msg.data['colorMode'];
}

if (
Expand Down Expand Up @@ -2299,9 +2299,9 @@ const converters = {

if (msg.data.hasOwnProperty('tuyaRgbMode')) {
if (msg.data['tuyaRgbMode'] === 1) {
result.color_mode = constants.colorMode[0];
result.color_mode = constants.colorModeLookup[0];
} else {
result.color_mode = constants.colorMode[2];
result.color_mode = constants.colorModeLookup[2];
}
}

Expand Down
28 changes: 14 additions & 14 deletions converters/toZigbee.js
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ const converters = {
};
await entity.command('lightingColorCtrl', 'moveToColor', payload, utils.getOptions(meta.mapped, entity));
return {
state: libColor.syncColorState({'color_mode': constants.colorMode[2], 'color_temp': value}, meta.state,
state: libColor.syncColorState({'color_mode': constants.colorModeLookup[2], 'color_temp': value}, meta.state,
entity, meta.options, meta.logger), readAfterWriteTime: payload.transtime * 100,
};
}
Expand Down Expand Up @@ -1031,7 +1031,7 @@ const converters = {
const payload = {colortemp: value, transtime: utils.getTransition(entity, key, meta).time};
await entity.command('lightingColorCtrl', 'moveToColorTemp', payload, utils.getOptions(meta.mapped, entity));
return {
state: libColor.syncColorState({'color_mode': constants.colorMode[2], 'color_temp': value}, meta.state,
state: libColor.syncColorState({'color_mode': constants.colorModeLookup[2], 'color_temp': value}, meta.state,
entity, meta.options, meta.logger), readAfterWriteTime: payload.transtime * 100,
};
},
Expand Down Expand Up @@ -1091,7 +1091,7 @@ const converters = {
xy.y = 0.2993;
}

newState.color_mode = constants.colorMode[1];
newState.color_mode = constants.colorModeLookup[1];
newState.color = xy.toObject();
zclData.colorx = utils.mapNumberRange(xy.x, 0, 1, 0, 65535);
zclData.colory = utils.mapNumberRange(xy.y, 0, 1, 0, 65535);
Expand All @@ -1100,7 +1100,7 @@ const converters = {
const enhancedHue = utils.getMetaValue(entity, meta.mapped, 'enhancedHue', 'allEqual', true);
const hsv = newColor.hsv;
const hsvCorrected = hsv.colorCorrected(meta);
newState.color_mode = constants.colorMode[0];
newState.color_mode = constants.colorModeLookup[0];
newState.color = hsv.toObject(false);

if (hsv.hue !== null) {
Expand Down Expand Up @@ -3542,7 +3542,7 @@ const converters = {
key: ['brightness', 'color', 'color_temp'],
options: [exposes.options.color_sync()],
convertSet: async (entity, key, value, meta) => {
if (key === 'brightness' && meta.state.color_mode == constants.colorMode[2] &&
if (key === 'brightness' && meta.state.color_mode == constants.colorModeLookup[2] &&
!meta.message.hasOwnProperty('color') && !meta.message.hasOwnProperty('color_temp')) {
const zclData = {level: Number(value), transtime: 0};

Expand All @@ -3565,7 +3565,7 @@ const converters = {

const newState = {
brightness: zclDataBrightness.level,
color_mode: constants.colorMode[2],
color_mode: constants.colorModeLookup[2],
color_temp: meta.message.color_temp,
};

Expand All @@ -3583,7 +3583,7 @@ const converters = {

const newState = {
brightness: zclDataBrightness.level,
color_mode: constants.colorMode[2],
color_mode: constants.colorModeLookup[2],
color_temp: value,
};

Expand Down Expand Up @@ -3649,7 +3649,7 @@ const converters = {
s: utils.mapNumberRange(zclData.saturation, 0, 254, 0, 100),
saturation: utils.mapNumberRange(zclData.saturation, 0, 254, 0, 100),
},
color_mode: constants.colorMode[0],
color_mode: constants.colorModeLookup[0],
};

return {state: libColor.syncColorState(newState, meta.state, entity, meta.options, meta.logger),
Expand Down Expand Up @@ -5285,12 +5285,12 @@ const converters = {

const addColorMode = (newState) => {
if (newState.hasOwnProperty('color_temp')) {
newState.color_mode = constants.colorMode[2];
newState.color_mode = constants.colorModeLookup[2];
} else if (newState.hasOwnProperty('color')) {
if (newState.color.hasOwnProperty('x')) {
newState.color_mode = constants.colorMode[1];
newState.color_mode = constants.colorModeLookup[1];
} else {
newState.color_mode = constants.colorMode[0];
newState.color_mode = constants.colorModeLookup[0];
}
}

Expand Down Expand Up @@ -5391,7 +5391,7 @@ const converters = {
const xScaled = utils.mapNumberRange(xy.x, 0, 1, 0, 65535);
const yScaled = utils.mapNumberRange(xy.y, 0, 1, 0, 65535);
extensionfieldsets.push({'clstId': 768, 'len': 4, 'extField': [xScaled, yScaled]});
state['color_mode'] = constants.colorMode[2];
state['color_mode'] = constants.colorModeLookup[2];
state['color_temp'] = val;
} else if (attribute === 'color') {
try {
Expand All @@ -5411,7 +5411,7 @@ const converters = {
'extField': [xScaled, yScaled],
},
);
state['color_mode'] = constants.colorMode[1];
state['color_mode'] = constants.colorModeLookup[1];
state['color'] = newColor.xy.toObject();
} else if (newColor.isHSV()) {
const hsvCorrected = newColor.hsv.colorCorrected(meta);
Expand Down Expand Up @@ -5439,7 +5439,7 @@ const converters = {
},
);
}
state['color_mode'] = constants.colorMode[0];
state['color_mode'] = constants.colorModeLookup[0];
state['color'] = newColor.hsv.toObject(false, false);
}
}
Expand Down
17 changes: 12 additions & 5 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,16 @@ const armNotification = {

// ID's from ZCL mapped to ha names where appropriate
// https://github.com/home-assistant/core/pull/47720
const colorMode = {
0: 'hs',
1: 'xy',
2: 'color_temp',
const ColorMode = {
HS: 0,
XY: 1,
ColorTemp: 2,
};

const colorModeLookup = {
[ColorMode.HS]: 'hs',
[ColorMode.XY]: 'xy',
[ColorMode.ColorTemp]: 'color_temp',
};

const lockSoundVolume = ['silent_mode', 'low_volume', 'high_volume'];
Expand Down Expand Up @@ -294,7 +300,8 @@ module.exports = {
lockSourceName,
armMode,
armNotification,
colorMode,
colorModeLookup,
ColorMode,
lockSoundVolume,
lockUserStatus,
easyCodeTouchActions,
Expand Down

0 comments on commit ac726b5

Please sign in to comment.