Sort state/brightness to front or back depending on bulb state (#3368)

This commit is contained in:
Jorge Schrauwen 2020-04-16 21:42:47 +02:00 committed by GitHub
parent c9298c5606
commit 4a771a75df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -113,26 +113,37 @@ class EntityPublish extends Extension {
}
}
const deviceState = this.state.get(entity.settings.ID) || {};
const isOn = deviceState && deviceState.state === 'ON' ? true : false;
/**
* Home Assistant always publishes 'state', even when e.g. only setting
* the color temperature. This would lead to 2 zigbee publishes, where the first one
* (state) is probably unecessary.
*/
const deviceState = this.state.get(entity.settings.ID) || {};
if (settings.get().homeassistant) {
const hasColorTemp = json.hasOwnProperty('color_temp');
const hasColor = json.hasOwnProperty('color');
const hasBrightness = json.hasOwnProperty('brightness');
const isOn = deviceState && deviceState.state === 'ON' ? true : false;
if (isOn && (hasColorTemp || hasColor) && !hasBrightness) {
delete json.state;
logger.debug('Skipping state because of Home Assistant');
}
}
// Ensure that state and brightness are executed before other commands.
/**
* Order state & brightness based on current bulb state
*
* Not all bulbs support setting the color/color_temp while it is off
* this results in inconsistant behavior between different vendors.
*
* bulb on => move state & brightness to the back
* bulb off => move state & brightness to the front
*/
const entries = Object.entries(json);
entries.sort((a, b) => (['state', 'brightness', 'brightness_percent'].includes(a[0]) ? -1 : 1));
entries.sort((a, b) => (
['state', 'brightness', 'brightness_percent'].includes(a[0]) ? (isOn ? 1 : -1) : (isOn ? -1 : 1)
));
// For each attribute call the corresponding converter
const usedConverters = [];