From 6978b9978b337454b815b48937a6b7c294985bd3 Mon Sep 17 00:00:00 2001 From: Holger Friedrich Date: Wed, 1 Jan 2025 11:40:10 +0100 Subject: [PATCH] [knx] Allow color temperatures specified in mired (#18004) This allows other bindings or scripts to send commands which specify the color temperature in mired. Signed-off-by: Holger Friedrich --- .../binding/knx/internal/dpt/ValueEncoder.java | 9 ++++++++- .../binding/knx/internal/dpt/DPTTest.java | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/bundles/org.openhab.binding.knx/src/main/java/org/openhab/binding/knx/internal/dpt/ValueEncoder.java b/bundles/org.openhab.binding.knx/src/main/java/org/openhab/binding/knx/internal/dpt/ValueEncoder.java index e0d0a9f7e74..114cdae913d 100644 --- a/bundles/org.openhab.binding.knx/src/main/java/org/openhab/binding/knx/internal/dpt/ValueEncoder.java +++ b/bundles/org.openhab.binding.knx/src/main/java/org/openhab/binding/knx/internal/dpt/ValueEncoder.java @@ -228,7 +228,14 @@ public class ValueEncoder { } if (unit != null) { - QuantityType converted = ((QuantityType) value).toUnit(unit); + QuantityType converted = null; + if ("K".equals(unit) || "°C".equals(unit)) { + // workaround for color temperatures given in MIRED, required as long as toUnit does + // not convert MIRED to Kelvin + converted = ((QuantityType) value).toInvertibleUnit(unit); + } else { + converted = ((QuantityType) value).toUnit(unit); + } if (converted == null) { LOGGER.warn("Could not convert {} to unit {}, stripping unit only. Check your configuration.", value, unit); diff --git a/bundles/org.openhab.binding.knx/src/test/java/org/openhab/binding/knx/internal/dpt/DPTTest.java b/bundles/org.openhab.binding.knx/src/test/java/org/openhab/binding/knx/internal/dpt/DPTTest.java index c3134be65a3..413a31e39b6 100644 --- a/bundles/org.openhab.binding.knx/src/test/java/org/openhab/binding/knx/internal/dpt/DPTTest.java +++ b/bundles/org.openhab.binding.knx/src/test/java/org/openhab/binding/knx/internal/dpt/DPTTest.java @@ -123,6 +123,10 @@ class DPTTest { assertEquals("1000", ValueEncoder.encode(new QuantityType<>("1000 lx"), "7.013")); assertEquals("3000", ValueEncoder.encode(new QuantityType<>("3000 K"), "7.600")); + // unit for 7.600 is K; special handling for color temperature: make sure °C and mired work as well + assertEquals("3273.15", ValueEncoder.encode(new QuantityType<>("3000 °C"), "7.600")); + assertEquals("4000", ValueEncoder.encode(new QuantityType<>("250 mired"), "7.600")); + assertEquals("4000", ValueEncoder.encode(new QuantityType<>("250 mirek"), "7.600")); } @Test @@ -316,7 +320,15 @@ class DPTTest { assertEquals("1", ValueEncoder.encode(new QuantityType<>("1 Pa"), "14.066")); assertEquals("1", ValueEncoder.encode(new QuantityType<>("1 N/m"), "14.067")); assertEquals("1", ValueEncoder.encode(new QuantityType<>("1 °C"), "14.068")); + // unit for 14.068 is °C; special handling for color temperature: make sure °C and mired work as well + assertEquals("-272.15", ValueEncoder.encode(new QuantityType<>("1 K"), "14.068")); + assertEquals("3726.85", ValueEncoder.encode(new QuantityType<>("250 mired"), "14.068")); + assertEquals("3726.85", ValueEncoder.encode(new QuantityType<>("250 mirek"), "14.068")); assertEquals("1", ValueEncoder.encode(new QuantityType<>("1 K"), "14.069")); + // unit for 14.069 is K; special handling for color temperature: make sure °C and mired work as well + assertEquals("274.15", ValueEncoder.encode(new QuantityType<>("1 °C"), "14.069")); + assertEquals("4000", ValueEncoder.encode(new QuantityType<>("250 mired"), "14.069")); + assertEquals("4000", ValueEncoder.encode(new QuantityType<>("250 mirek"), "14.069")); assertEquals("1", ValueEncoder.encode(new QuantityType<>("1 K"), "14.070")); assertEquals("1", ValueEncoder.encode(new QuantityType<>("1 J/K"), "14.071")); assertEquals("1", ValueEncoder.encode(new QuantityType<>("1 W/m/K"), "14.072")); @@ -526,6 +538,12 @@ class DPTTest { // 64-bit signed (DPT 29) assertNotEquals(DPTXlator64BitSigned.DPT_REACTIVE_ENERGY.getUnit(), Units.VAR_HOUR.toString()); + + // workaround for color temperatures given in MIRED, required as long as toUnit does + // not convert MIRED to Kelvin + // -> if this test fails, workaround in ValueEncoder can be removed + assertNull((new QuantityType<>("1 mired")).toUnit("K")); + assertNotNull((new QuantityType<>("1 mired")).toInvertibleUnit("K")); } private static Stream> unitProvider() {