diff --git a/lib/extension/entityPublish.js b/lib/extension/entityPublish.js index 13512ef8..9205376c 100644 --- a/lib/extension/entityPublish.js +++ b/lib/extension/entityPublish.js @@ -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 = [];