From b479f6d528cd714221e242ff5bc2f37ebfaf7b32 Mon Sep 17 00:00:00 2001 From: Laurent Garnier Date: Tue, 3 Dec 2024 09:06:39 +0100 Subject: [PATCH 1/2] [BasicUI] Fix unit determination for min/max from state description Concerns the Colortemperaturepiucker element. Signed-off-by: Laurent Garnier --- .../render/ColortemppickerRenderer.java | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/bundles/org.openhab.ui.basic/src/main/java/org/openhab/ui/basic/internal/render/ColortemppickerRenderer.java b/bundles/org.openhab.ui.basic/src/main/java/org/openhab/ui/basic/internal/render/ColortemppickerRenderer.java index 6df45f4349..b8cf465869 100644 --- a/bundles/org.openhab.ui.basic/src/main/java/org/openhab/ui/basic/internal/render/ColortemppickerRenderer.java +++ b/bundles/org.openhab.ui.basic/src/main/java/org/openhab/ui/basic/internal/render/ColortemppickerRenderer.java @@ -207,8 +207,24 @@ private BigDecimal getMaximumInKelvin(Colortemperaturepicker widget, Unit wid } private boolean isUnitInKelvin(StateDescription stateDescription) { - // If no pattern or pattern with no unit, assume unit for min and max is Kelvin - Unit unit = UnitUtils.parseUnit(stateDescription.getPattern()); - return unit == null || unit == Units.KELVIN; + // Using the pattern to determine the unit of min/max is not fully reliable + // because the user can override the pattern and set a different unit than + // the one defined by binding developer at channel level. + // So we consider the values of min/max to determine the unit of the range. + // If values are lower than 1000, we assume values are in mirek. + // If values are greater than 1000, we assume values are in Kelvin. + boolean inKelvin; + BigDecimal min = stateDescription.getMinimum(); + BigDecimal max = stateDescription.getMaximum(); + if (min != null) { + inKelvin = min.doubleValue() >= 1000.0; + } else if (max != null) { + inKelvin = max.doubleValue() > 1000.0; + } else { + // If no pattern or pattern with no unit, assume unit is Kelvin + Unit unit = UnitUtils.parseUnit(stateDescription.getPattern()); + inKelvin = unit == null || unit == Units.KELVIN; + } + return inKelvin; } } From e1161c18f45f6e729794d2018239bcf2ad5a5ad5 Mon Sep 17 00:00:00 2001 From: Laurent Garnier Date: Tue, 3 Dec 2024 17:51:33 +0100 Subject: [PATCH 2/2] Review comment: consider default min/max from system channel type Signed-off-by: Laurent Garnier --- .../internal/render/ColortemppickerRenderer.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/bundles/org.openhab.ui.basic/src/main/java/org/openhab/ui/basic/internal/render/ColortemppickerRenderer.java b/bundles/org.openhab.ui.basic/src/main/java/org/openhab/ui/basic/internal/render/ColortemppickerRenderer.java index b8cf465869..7339403cd2 100644 --- a/bundles/org.openhab.ui.basic/src/main/java/org/openhab/ui/basic/internal/render/ColortemppickerRenderer.java +++ b/bundles/org.openhab.ui.basic/src/main/java/org/openhab/ui/basic/internal/render/ColortemppickerRenderer.java @@ -13,6 +13,7 @@ package org.openhab.ui.basic.internal.render; import java.math.BigDecimal; +import java.util.Objects; import javax.measure.Unit; @@ -29,6 +30,7 @@ import org.openhab.core.library.unit.Units; import org.openhab.core.model.sitemap.sitemap.Colortemperaturepicker; import org.openhab.core.model.sitemap.sitemap.Widget; +import org.openhab.core.thing.DefaultSystemChannelTypeProvider; import org.openhab.core.types.State; import org.openhab.core.types.StateDescription; import org.openhab.core.types.util.UnitUtils; @@ -54,8 +56,10 @@ @NonNullByDefault public class ColortemppickerRenderer extends AbstractWidgetRenderer { - private static final BigDecimal MIN_TEMPERATURE_KELVIN = BigDecimal.valueOf(1000); - private static final BigDecimal MAX_TEMPERATURE_KELVIN = BigDecimal.valueOf(10000); + private static final BigDecimal MIN_TEMPERATURE_KELVIN = Objects.requireNonNull(Objects + .requireNonNull(DefaultSystemChannelTypeProvider.SYSTEM_COLOR_TEMPERATURE_ABS.getState()).getMinimum()); + private static final BigDecimal MAX_TEMPERATURE_KELVIN = Objects.requireNonNull(Objects + .requireNonNull(DefaultSystemChannelTypeProvider.SYSTEM_COLOR_TEMPERATURE_ABS.getState()).getMaximum()); private static final double GRADIENT_INCREMENT_PERCENT = 2.5; private final Logger logger = LoggerFactory.getLogger(ColortemppickerRenderer.class); @@ -217,9 +221,9 @@ private boolean isUnitInKelvin(StateDescription stateDescription) { BigDecimal min = stateDescription.getMinimum(); BigDecimal max = stateDescription.getMaximum(); if (min != null) { - inKelvin = min.doubleValue() >= 1000.0; + inKelvin = min.doubleValue() >= MIN_TEMPERATURE_KELVIN.doubleValue(); } else if (max != null) { - inKelvin = max.doubleValue() > 1000.0; + inKelvin = max.doubleValue() > MIN_TEMPERATURE_KELVIN.doubleValue(); } else { // If no pattern or pattern with no unit, assume unit is Kelvin Unit unit = UnitUtils.parseUnit(stateDescription.getPattern());