]> git.basschouten.com Git - openhab-addons.git/commitdiff
[hue] Allow handling of QuantityType for color temperature channel (#14024)
authorChristoph Weitkamp <github@christophweitkamp.de>
Wed, 21 Dec 2022 19:10:21 +0000 (20:10 +0100)
committerGitHub <noreply@github.com>
Wed, 21 Dec 2022 19:10:21 +0000 (20:10 +0100)
* Allow handling of QuantityType for color temperature channel
* Fixed log messages and reduce log level.

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
bundles/org.openhab.binding.hue/README.md
bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueGroupHandler.java
bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueLightHandler.java
bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/LightStateConverter.java
bundles/org.openhab.binding.hue/src/test/java/org/openhab/binding/hue/internal/handler/HueLightHandlerTest.java

index 2986f1b2090e08e9a26460a00b714733347da3e0..f702bde360b8a7e1b6de17cf898dcd4930d3b057 100644 (file)
@@ -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                                     |
index 8d917fe3cb47e56c67ce4e78705aa670f523690e..5ba9bb404dd890e7e0e3810a72313211de6f2c60 100644 (file)
@@ -35,7 +35,9 @@ import org.openhab.core.library.types.HSBType;
 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 class HueGroupHandler extends BaseThingHandler implements HueLightActions
                 }
                 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 class HueGroupHandler extends BaseThingHandler implements HueLightActions
                 }
                 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);
         }
     }
 
index 6b145ff72d263671a0cb2f9e7cd4f68e090b4bde..7dcf5933c9ad0d717707d0eb42926030bf889c09 100644 (file)
@@ -34,7 +34,9 @@ import org.openhab.core.library.types.HSBType;
 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 class HueLightHandler extends BaseThingHandler implements HueLightActions
                 }
                 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 class HueLightHandler extends BaseThingHandler implements HueLightActions
                     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 class HueLightHandler extends BaseThingHandler implements HueLightActions
             }
             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);
         }
     }
 
index 1212573e608feedcc85df167acb52d5d5fb3c28d..47c70882ad8bc53a7c980df33535e4fd84f41532 100644 (file)
@@ -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.HSBType;
 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 class LightStateConverter {
     }
 
     /**
-     * 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 class LightStateConverter {
     }
 
     /**
-     * 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<Temperature> toColorTemperature(State lightState) {
+        return new QuantityType<>(miredToKelvin(lightState.getColorTemperature()), Units.KELVIN);
     }
 
     /**
index 10ca508baee38a43db6664a08a96a53530efb459..ee250b364cbf7399cda4e5f45b1289f1025a9a32 100644 (file)
@@ -33,7 +33,9 @@ import org.openhab.core.library.types.HSBType;
 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 class HueLightHandlerTest {
     }
 
     @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 class HueLightHandlerTest {
     }
 
     @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 class HueLightHandlerTest {
     }
 
     @Test
-    public void asserCommandForColorChannelDecrease() {
+    public void assertCommandForColorChannelDecrease() {
         HueLightState currentState = new HueLightState().bri(200);
         String expectedReply = "{\"bri\" : 170, \"transitiontime\" : 4}";
         assertSendCommandForColor(IncreaseDecreaseType.DECREASE, currentState, expectedReply);