From 53bcfc44d0bedc9eeaf551a003563ce2eb32cf13 Mon Sep 17 00:00:00 2001 From: marco Date: Tue, 17 Oct 2023 23:01:11 +0200 Subject: [PATCH] Change the conversion of the DPT251.600 to use RGB and White. With the new feature, the HSBType is converted into RGBW, and we convert the status signal from the KNX back into an HSB value. Due to the conversion, some accuracy is lost, but the result is approximately correct. Signed-off-by: marco --- .../binding/knx/internal/dpt/ValueDecoder.java | 15 ++++++++------- .../binding/knx/internal/dpt/ValueEncoder.java | 6 +++--- .../openhab/binding/knx/internal/dpt/DPTTest.java | 10 +++++----- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/bundles/org.openhab.binding.knx/src/main/java/org/openhab/binding/knx/internal/dpt/ValueDecoder.java b/bundles/org.openhab.binding.knx/src/main/java/org/openhab/binding/knx/internal/dpt/ValueDecoder.java index a54490ca3a7da..ffc1c49d68bd4 100644 --- a/bundles/org.openhab.binding.knx/src/main/java/org/openhab/binding/knx/internal/dpt/ValueDecoder.java +++ b/bundles/org.openhab.binding.knx/src/main/java/org/openhab/binding/knx/internal/dpt/ValueDecoder.java @@ -327,13 +327,14 @@ private static Type handleDpt10(String value) throws ParseException { String bString = rgbw.group("b"); String wString = rgbw.group("w"); - if (rString != null && gString != null && bString != null && HSBType.class.equals(preferredType)) { - // does not support PercentType and r,g,b valid -> HSBType - int r = coerceToRange((int) (Double.parseDouble(rString.replace(",", ".")) * 2.55), 0, 255); - int g = coerceToRange((int) (Double.parseDouble(gString.replace(",", ".")) * 2.55), 0, 255); - int b = coerceToRange((int) (Double.parseDouble(bString.replace(",", ".")) * 2.55), 0, 255); - - return HSBType.fromRGB(r, g, b); + if (rString != null && gString != null && bString != null && wString != null + && HSBType.class.equals(preferredType)) { + double r = coerceToRange(Double.parseDouble(rString.replace(",", ".")) * 2.55, 0, 255); + double g = coerceToRange(Double.parseDouble(gString.replace(",", ".")) * 2.55, 0, 255); + double b = coerceToRange(Double.parseDouble(bString.replace(",", ".")) * 2.55, 0, 255); + double w = coerceToRange(Double.parseDouble(wString.replace(",", ".")) * 2.55, 0, 255); + + return ColorUtil.rgbwTohsb(r, g, b, w); } else if (wString != null && PercentType.class.equals(preferredType)) { // does support PercentType and w valid -> PercentType BigDecimal w = new BigDecimal(wString.replace(",", ".")); 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 c2519d3bf719f..9b39fb4ce42d0 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 @@ -162,9 +162,9 @@ private static String handleHSBType(String dptId, HSBType hsb) { double[] xyY = ColorUtil.hsbToXY(hsb); return String.format("(%,.4f %,.4f) %,.1f %%", xyY[0], xyY[1], xyY[2] * 100.0); case "251.600": - PercentType[] rgbw = ColorUtil.hsbToRgbPercent(hsb); - return String.format("%,.1f %,.1f %,.1f - %%", rgbw[0].doubleValue(), rgbw[1].doubleValue(), - rgbw[2].doubleValue()); + PercentType[] rgbw = ColorUtil.hsbToRgbwPercent(hsb); + return String.format("%,.1f %,.1f %,.1f %,.1f %%", rgbw[0].doubleValue(), rgbw[1].doubleValue(), + rgbw[2].doubleValue(), rgbw[3].doubleValue()); case "5.003": return hsb.getHue().toString(); default: 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 71c3b7ab56de0..1077f17b083ad 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 @@ -336,13 +336,13 @@ public void dpt232HsbValue() { @Test public void dpt252EncoderTest() { // input data - byte[] data = new byte[] { 0x26, 0x2b, 0x31, 0x00, 0x00, 0x0e }; + byte[] data = new byte[] { 0x26, 0x2b, 0x31, 0x00, 0x00, 0x0f }; HSBType hsbType = (HSBType) ValueDecoder.decode("251.600", data, HSBType.class); assertNotNull(hsbType); - assertEquals(207, hsbType.getHue().doubleValue(), 0.5); - assertEquals(23, hsbType.getSaturation().doubleValue(), 0.5); - assertEquals(19, hsbType.getBrightness().doubleValue(), 0.5); + assertEquals(210, hsbType.getHue().doubleValue(), 0.5); + assertEquals(22, hsbType.getSaturation().doubleValue(), 0.5); + assertEquals(3.5, hsbType.getBrightness().doubleValue(), 0.5); } // This test checks all our overrides for units. It allows to detect unnecessary overrides when we @@ -457,7 +457,7 @@ public void dpt242BackToBackTest(byte[] value) { // for back to back test, compare numerical values to allow tolerances double dx = (((value[0] & 0xff) << 8) | (value[1] & 0xff)) / 65535.0; double dy = (((value[2] & 0xff) << 8) | (value[3] & 0xff)) / 65535.0; - double dY = ((double) (value[4] & 0xff)) * 100.0 / 255.0; + double dY = (value[4] & 0xff) * 100.0 / 255.0; Matcher matcher = ValueDecoder.XYY_PATTERN.matcher(result); Assertions.assertTrue(matcher.matches());