From ac4198e8bf6872eb69ff7653e4a6928585406b7d Mon Sep 17 00:00:00 2001 From: Cody Cutrer Date: Mon, 17 Jul 2023 15:05:18 -0600 Subject: [PATCH 1/2] [mqtt.homeassistant] interpret a dimmable light as OFF properly from zigbee2mqtt zigbee2mqtt can send a brightness of say 99, with a state of OFF, when a bulb is off. make sure if state is sent, it overrides all other inferences Signed-off-by: Cody Cutrer --- .../internal/component/JSONSchemaLight.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/component/JSONSchemaLight.java b/bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/component/JSONSchemaLight.java index 93dcd606a6958..51c3d4a7e79cd 100644 --- a/bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/component/JSONSchemaLight.java +++ b/bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/component/JSONSchemaLight.java @@ -253,8 +253,10 @@ public void updateChannelState(ChannelUID channel, State state) { } } + boolean off = false; if (jsonState.state != null) { onOffValue.update(onOffValue.parseCommand(new StringType(jsonState.state))); + off = onOffValue.getChannelState().equals(OnOffType.OFF); if (brightnessValue.getChannelState() instanceof UnDefType) { brightnessValue.update(brightnessValue.parseCommand((OnOffType) onOffValue.getChannelState())); } @@ -263,7 +265,7 @@ public void updateChannelState(ChannelUID channel, State state) { } } - if (jsonState.brightness != null) { + if (jsonState.brightness != null && !off) { brightnessValue.update( brightnessValue.parseCommand(new DecimalType(Objects.requireNonNull(jsonState.brightness)))); if (colorValue.getChannelState() instanceof HSBType) { @@ -284,9 +286,14 @@ public void updateChannelState(ChannelUID channel, State state) { } if (jsonState.color != null) { - PercentType brightness = brightnessValue.getChannelState() instanceof PercentType - ? (PercentType) brightnessValue.getChannelState() - : PercentType.HUNDRED; + PercentType brightness; + if (off) { + brightness = PercentType.ZERO; + } else if (brightnessValue.getChannelState() instanceof PercentType) { + brightness = (PercentType) brightnessValue.getChannelState(); + } else { + brightness = PercentType.HUNDRED; + } // This corresponds to "deprecated" color mode handling, since we're not checking which color // mode is currently active. // HS is highest priority, then XY, then RGB From 37a2fd43697f2d4268110f52baac4ec317a5b6ab Mon Sep 17 00:00:00 2001 From: Cody Cutrer Date: Sun, 19 Nov 2023 12:46:29 -0700 Subject: [PATCH 2/2] handle brightness but not color bulbs Signed-off-by: Cody Cutrer --- .../internal/component/JSONSchemaLight.java | 36 ++++++++++--------- .../component/JSONSchemaLightTests.java | 3 ++ 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/component/JSONSchemaLight.java b/bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/component/JSONSchemaLight.java index 51c3d4a7e79cd..a86f6488de5ba 100644 --- a/bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/component/JSONSchemaLight.java +++ b/bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/component/JSONSchemaLight.java @@ -258,23 +258,33 @@ public void updateChannelState(ChannelUID channel, State state) { onOffValue.update(onOffValue.parseCommand(new StringType(jsonState.state))); off = onOffValue.getChannelState().equals(OnOffType.OFF); if (brightnessValue.getChannelState() instanceof UnDefType) { - brightnessValue.update(brightnessValue.parseCommand((OnOffType) onOffValue.getChannelState())); + brightnessValue.update(off ? PercentType.ZERO : PercentType.HUNDRED); } if (colorValue.getChannelState() instanceof UnDefType) { - colorValue.update(colorValue.parseCommand((OnOffType) onOffValue.getChannelState())); + colorValue.update(off ? HSBType.BLACK : HSBType.WHITE); } } - if (jsonState.brightness != null && !off) { - brightnessValue.update( - brightnessValue.parseCommand(new DecimalType(Objects.requireNonNull(jsonState.brightness)))); + PercentType brightness; + if (off) { + brightness = PercentType.ZERO; + } else if (brightnessValue.getChannelState() instanceof PercentType percentValue) { + brightness = percentValue; + } else { + brightness = PercentType.HUNDRED; + } + + if (jsonState.brightness != null) { + if (!off) { + brightness = (PercentType) brightnessValue + .parseMessage(new DecimalType(Objects.requireNonNull(jsonState.brightness))); + } + brightnessValue.update(brightness); if (colorValue.getChannelState() instanceof HSBType) { HSBType color = (HSBType) colorValue.getChannelState(); - colorValue.update(new HSBType(color.getHue(), color.getSaturation(), - (PercentType) brightnessValue.getChannelState())); + colorValue.update(new HSBType(color.getHue(), color.getSaturation(), brightness)); } else { - colorValue.update(new HSBType(DecimalType.ZERO, PercentType.ZERO, - (PercentType) brightnessValue.getChannelState())); + colorValue.update(new HSBType(DecimalType.ZERO, PercentType.ZERO, brightness)); } } @@ -286,14 +296,6 @@ public void updateChannelState(ChannelUID channel, State state) { } if (jsonState.color != null) { - PercentType brightness; - if (off) { - brightness = PercentType.ZERO; - } else if (brightnessValue.getChannelState() instanceof PercentType) { - brightness = (PercentType) brightnessValue.getChannelState(); - } else { - brightness = PercentType.HUNDRED; - } // This corresponds to "deprecated" color mode handling, since we're not checking which color // mode is currently active. // HS is highest priority, then XY, then RGB diff --git a/bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/JSONSchemaLightTests.java b/bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/JSONSchemaLightTests.java index e1c50de28992f..604836dfc4eef 100644 --- a/bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/JSONSchemaLightTests.java +++ b/bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/JSONSchemaLightTests.java @@ -119,6 +119,9 @@ public void testBrightnessAndOnOff() throws InterruptedException { assertState(component, Light.BRIGHTNESS_CHANNEL_ID, new PercentType(new BigDecimal(128 * 100).divide(new BigDecimal(255), MathContext.DECIMAL128))); + publishMessage("zigbee2mqtt/light/state", "{ \"state\": \"OFF\", \"brightness\": 128 }"); + assertState(component, Light.BRIGHTNESS_CHANNEL_ID, PercentType.ZERO); + sendCommand(component, Light.BRIGHTNESS_CHANNEL_ID, PercentType.HUNDRED); assertPublished("zigbee2mqtt/light/set/state", "{\"state\":\"ON\",\"brightness\":255}");