Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BasicUI] Fix unit determination for min/max from item state description #2894

Merged
merged 2 commits into from
Dec 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
lolodomo marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.openhab.ui.basic.internal.render;

import java.math.BigDecimal;
import java.util.Objects;

import javax.measure.Unit;

Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -207,8 +211,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() >= MIN_TEMPERATURE_KELVIN.doubleValue();
} else if (max != null) {
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());
inKelvin = unit == null || unit == Units.KELVIN;
}
return inKelvin;
}
}