From a0c2c76fd0f1ae64e9af6a0a2593519af5f6513b Mon Sep 17 00:00:00 2001 From: Christoph Weitkamp Date: Wed, 21 Dec 2022 20:10:21 +0100 Subject: [PATCH] [hue] Allow handling of QuantityType for color temperature channel (#14024) * Allow handling of QuantityType for color temperature channel * Fixed log messages and reduce log level. Signed-off-by: Christoph Weitkamp --- bundles/org.openhab.binding.hue/README.md | 2 +- .../hue/internal/handler/HueGroupHandler.java | 19 ++++++++-- .../hue/internal/handler/HueLightHandler.java | 21 +++++++++-- .../internal/handler/LightStateConverter.java | 22 +++++++----- .../internal/handler/HueLightHandlerTest.java | 36 ++++++++++++++++--- 5 files changed, 79 insertions(+), 21 deletions(-) diff --git a/bundles/org.openhab.binding.hue/README.md b/bundles/org.openhab.binding.hue/README.md index 2986f1b2090e0..f702bde360b8a 100644 --- a/bundles/org.openhab.binding.hue/README.md +++ b/bundles/org.openhab.binding.hue/README.md @@ -170,7 +170,7 @@ The devices support some of the following channels: | color | Color | This channel supports full color control with hue, saturation and brightness values. | 0200, 0210, group | | brightness | Dimmer | This channel supports adjusting the brightness value. Note that this is not available, if the color channel is supported. | 0100, 0110, 0220, group | | color_temperature | Dimmer | This channel supports adjusting the color temperature from cold (0%) to warm (100%). | 0210, 0220, group | -| color_temperature_abs | Number | This channel supports adjusting the color temperature in Kelvin. **Advanced** | 0210, 0220, group | +| color_temperature_abs | Number:Temperature | This channel supports adjusting the color temperature in Kelvin. **Advanced** | 0210, 0220, group | | alert | String | This channel supports displaying alerts by flashing the bulb either once or multiple times. Valid values are: NONE, SELECT and LSELECT. | 0000, 0100, 0200, 0210, 0220, group | | effect | Switch | This channel supports color looping. | 0200, 0210, 0220 | | dimmer_switch | Number | This channel shows which button was last pressed on the dimmer switch. | 0820 | diff --git a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueGroupHandler.java b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueGroupHandler.java index 8d917fe3cb47e..5ba9bb404dd89 100644 --- a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueGroupHandler.java +++ b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueGroupHandler.java @@ -35,7 +35,9 @@ import org.openhab.core.library.types.IncreaseDecreaseType; import org.openhab.core.library.types.OnOffType; import org.openhab.core.library.types.PercentType; +import org.openhab.core.library.types.QuantityType; import org.openhab.core.library.types.StringType; +import org.openhab.core.library.unit.Units; import org.openhab.core.thing.Bridge; import org.openhab.core.thing.ChannelUID; import org.openhab.core.thing.Thing; @@ -225,8 +227,18 @@ public void handleCommand(String channel, Command command, long fadeTime) { } break; case CHANNEL_COLORTEMPERATURE_ABS: - if (command instanceof DecimalType) { - newState = LightStateConverter.toColorTemperatureLightState((DecimalType) command, + if (command instanceof QuantityType) { + QuantityType convertedCommand = ((QuantityType) command).toInvertibleUnit(Units.KELVIN); + if (convertedCommand != null) { + newState = LightStateConverter.toColorTemperatureLightState(convertedCommand.intValue(), + colorTemperatureCapabilties); + newState.setTransitionTime(fadeTime); + } else { + logger.warn("Unable to convert unit from '{}' to '{}'. Skipping command.", + ((QuantityType) command).getUnit(), Units.KELVIN); + } + } else if (command instanceof DecimalType) { + newState = LightStateConverter.toColorTemperatureLightState(((DecimalType) command).intValue(), colorTemperatureCapabilties); newState.setTransitionTime(fadeTime); } @@ -284,13 +296,14 @@ public void handleCommand(String channel, Command command, long fadeTime) { } break; default: + logger.debug("Command sent to an unknown channel id: {}:{}", getThing().getUID(), channel); break; } if (newState != null) { cacheNewState(newState); bridgeHandler.updateGroupState(group, newState, fadeTime); } else { - logger.debug("Command sent to an unknown channel id: {}:{}", getThing().getUID(), channel); + logger.debug("Unable to handle command '{}' for channel '{}'. Skipping command.", command, channel); } } diff --git a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueLightHandler.java b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueLightHandler.java index 6b145ff72d263..7dcf5933c9ad0 100644 --- a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueLightHandler.java +++ b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueLightHandler.java @@ -34,7 +34,9 @@ import org.openhab.core.library.types.IncreaseDecreaseType; import org.openhab.core.library.types.OnOffType; import org.openhab.core.library.types.PercentType; +import org.openhab.core.library.types.QuantityType; import org.openhab.core.library.types.StringType; +import org.openhab.core.library.unit.Units; import org.openhab.core.thing.Bridge; import org.openhab.core.thing.ChannelUID; import org.openhab.core.thing.Thing; @@ -267,8 +269,18 @@ public void handleCommand(String channel, Command command, long fadeTime) { } break; case CHANNEL_COLORTEMPERATURE_ABS: - if (command instanceof DecimalType) { - newState = LightStateConverter.toColorTemperatureLightState((DecimalType) command, + if (command instanceof QuantityType) { + QuantityType convertedCommand = ((QuantityType) command).toInvertibleUnit(Units.KELVIN); + if (convertedCommand != null) { + newState = LightStateConverter.toColorTemperatureLightState(convertedCommand.intValue(), + colorTemperatureCapabilties); + newState.setTransitionTime(fadeTime); + } else { + logger.warn("Unable to convert unit from '{}' to '{}'. Skipping command.", + ((QuantityType) command).getUnit(), Units.KELVIN); + } + } else if (command instanceof DecimalType) { + newState = LightStateConverter.toColorTemperatureLightState(((DecimalType) command).intValue(), colorTemperatureCapabilties); newState.setTransitionTime(fadeTime); } @@ -356,6 +368,9 @@ public void handleCommand(String channel, Command command, long fadeTime) { newState = LightStateConverter.toOnOffEffectState((OnOffType) command); } break; + default: + logger.debug("Command sent to an unknown channel id: {}:{}", getThing().getUID(), channel); + break; } if (newState != null) { // Cache values which we have sent @@ -369,7 +384,7 @@ public void handleCommand(String channel, Command command, long fadeTime) { } bridgeHandler.updateLightState(this, light, newState, fadeTime); } else { - logger.warn("Command sent to an unknown channel id: {}:{}", getThing().getUID(), channel); + logger.debug("Unable to handle command '{}' for channel '{}'. Skipping command.", command, channel); } } diff --git a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/LightStateConverter.java b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/LightStateConverter.java index 1212573e608fe..47c70882ad8bc 100644 --- a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/LightStateConverter.java +++ b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/LightStateConverter.java @@ -12,6 +12,8 @@ */ package org.openhab.binding.hue.internal.handler; +import javax.measure.quantity.Temperature; + import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.hue.internal.dto.ColorTemperature; @@ -25,7 +27,9 @@ import org.openhab.core.library.types.IncreaseDecreaseType; import org.openhab.core.library.types.OnOffType; import org.openhab.core.library.types.PercentType; +import org.openhab.core.library.types.QuantityType; import org.openhab.core.library.types.StringType; +import org.openhab.core.library.unit.Units; /** * The {@link LightStateConverter} is responsible for mapping to/from jue types. @@ -161,14 +165,14 @@ public static int kelvinToMired(int kelvinValue) { } /** - * Transforms the given {@link DecimalType} into a light state containing - * the color temperature in Kelvin. + * Transforms the given color temperature in Kelvin into a Hue Light {@link State}. * - * @param decimalType color temperature in Kelvin + * @param kelvinValue color temperature in Kelvin + * @param capabilities color temperature capabilities (e.g. min and max values) * @return light state containing the color temperature */ - public static StateUpdate toColorTemperatureLightState(DecimalType decimalType, ColorTemperature capabilities) { - return new StateUpdate().setColorTemperature(kelvinToMired(decimalType.intValue()), capabilities); + public static StateUpdate toColorTemperatureLightState(int kelvinValue, ColorTemperature capabilities) { + return new StateUpdate().setColorTemperature(kelvinToMired(kelvinValue), capabilities); } /** @@ -207,14 +211,14 @@ public static int miredToKelvin(int miredValue) { } /** - * Transforms Hue Light {@link State} into {@link DecimalType} representing + * Transforms Hue Light {@link State} into {@link QuantityType} representing * the color temperature in Kelvin. * * @param lightState light state - * @return percent type representing the color temperature in Kelvin + * @return quantity type representing the color temperature in Kelvin */ - public static DecimalType toColorTemperature(State lightState) { - return new DecimalType(miredToKelvin(lightState.getColorTemperature())); + public static QuantityType toColorTemperature(State lightState) { + return new QuantityType<>(miredToKelvin(lightState.getColorTemperature()), Units.KELVIN); } /** diff --git a/bundles/org.openhab.binding.hue/src/test/java/org/openhab/binding/hue/internal/handler/HueLightHandlerTest.java b/bundles/org.openhab.binding.hue/src/test/java/org/openhab/binding/hue/internal/handler/HueLightHandlerTest.java index 10ca508baee38..ee250b364cbf7 100644 --- a/bundles/org.openhab.binding.hue/src/test/java/org/openhab/binding/hue/internal/handler/HueLightHandlerTest.java +++ b/bundles/org.openhab.binding.hue/src/test/java/org/openhab/binding/hue/internal/handler/HueLightHandlerTest.java @@ -33,7 +33,9 @@ import org.openhab.core.library.types.IncreaseDecreaseType; import org.openhab.core.library.types.OnOffType; import org.openhab.core.library.types.PercentType; +import org.openhab.core.library.types.QuantityType; import org.openhab.core.library.types.StringType; +import org.openhab.core.library.unit.Units; import org.openhab.core.thing.Bridge; import org.openhab.core.thing.ChannelUID; import org.openhab.core.thing.Thing; @@ -148,23 +150,47 @@ public void assertCommandForColorTemperatureChannel1000Percent() { } @Test - public void assertCommandForColorTemperatureAbsChannel6500Kelvin() { + public void assertDecimalTypeCommandForColorTemperatureAbsChannel6500Kelvin() { String expectedReply = "{\"ct\" : 153, \"transitiontime\" : 4}"; assertSendCommandForColorTempAbs(new DecimalType(6500), new HueLightState(), expectedReply); } @Test - public void assertCommandForColorTemperatureAbsChannel4500Kelvin() { + public void assertDecimalTypeCommandForColorTemperatureAbsChannel4500Kelvin() { String expectedReply = "{\"ct\" : 222, \"transitiontime\" : 4}"; assertSendCommandForColorTempAbs(new DecimalType(4500), new HueLightState(), expectedReply); } @Test - public void assertCommandForColorTemperatureAbsChannel2000Kelvin() { + public void assertDecimalTypeCommandForColorTemperatureAbsChannel2000Kelvin() { String expectedReply = "{\"ct\" : 500, \"transitiontime\" : 4}"; assertSendCommandForColorTempAbs(new DecimalType(2000), new HueLightState(), expectedReply); } + @Test + public void assertQuantityTypeCommandForColorTemperatureAbsChannel6500Kelvin() { + String expectedReply = "{\"ct\" : 153, \"transitiontime\" : 4}"; + assertSendCommandForColorTempAbs(new QuantityType<>(6500, Units.KELVIN), new HueLightState(), expectedReply); + } + + @Test + public void assertQuantityTypeCommandForColorTemperatureAbsChannel4500Kelvin() { + String expectedReply = "{\"ct\" : 222, \"transitiontime\" : 4}"; + assertSendCommandForColorTempAbs(new QuantityType<>(4500, Units.KELVIN), new HueLightState(), expectedReply); + } + + @Test + public void assertQuantityTypeCommandForColorTemperatureAbsChannel2000Kelvin() { + String expectedReply = "{\"ct\" : 500, \"transitiontime\" : 4}"; + assertSendCommandForColorTempAbs(new QuantityType<>(2000, Units.KELVIN), new HueLightState(), expectedReply); + } + + @Test + public void assertQuantityTypeCommandForColorTemperatureAbsChannel500Mired() { + String expectedReply = "{\"ct\" : 500, \"transitiontime\" : 4}"; + assertSendCommandForColorTempAbs(new QuantityType<>(500, Units.MIRED), new HueLightState(), expectedReply); + } + @Test public void assertPercentageValueOfColorTemperatureWhenCt153() { int expectedReply = 0; @@ -250,7 +276,7 @@ public void assertXYCommandForColorChannelColorful() { } @Test - public void asserCommandForColorChannelIncrease() { + public void assertCommandForColorChannelIncrease() { HueLightState currentState = new HueLightState().bri(1).on(false); String expectedReply = "{\"bri\" : 30, \"on\" : true, \"transitiontime\" : 4}"; assertSendCommandForColor(IncreaseDecreaseType.INCREASE, currentState, expectedReply); @@ -265,7 +291,7 @@ public void asserCommandForColorChannelIncrease() { } @Test - public void asserCommandForColorChannelDecrease() { + public void assertCommandForColorChannelDecrease() { HueLightState currentState = new HueLightState().bri(200); String expectedReply = "{\"bri\" : 170, \"transitiontime\" : 4}"; assertSendCommandForColor(IncreaseDecreaseType.DECREASE, currentState, expectedReply);