From dcb6d6e2c214f13c26accf69d89080f17f8230d2 Mon Sep 17 00:00:00 2001 From: Cody Cutrer Date: Sun, 29 Dec 2024 11:18:36 -0700 Subject: [PATCH] [mqtt.generic] create the proper item type for channels with units (#17929) * [mqtt.generic] create the proper item type for channels with units Signed-off-by: Cody Cutrer --- .../binding/mqtt/generic/values/NumberValue.java | 15 ++++++++++++++- .../binding/mqtt/generic/values/ValueTests.java | 6 ++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/values/NumberValue.java b/bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/values/NumberValue.java index 06afc9b497f..14ffe522c75 100644 --- a/bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/values/NumberValue.java +++ b/bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/values/NumberValue.java @@ -25,10 +25,12 @@ import org.openhab.core.library.types.IncreaseDecreaseType; import org.openhab.core.library.types.QuantityType; import org.openhab.core.library.types.StringType; import org.openhab.core.library.types.UpDownType; +import org.openhab.core.library.unit.Units; import org.openhab.core.types.Command; import org.openhab.core.types.StateDescriptionFragmentBuilder; import org.openhab.core.types.Type; import org.openhab.core.types.UnDefType; +import org.openhab.core.types.util.UnitUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -56,7 +58,7 @@ public class NumberValue extends Value { public NumberValue(@Nullable BigDecimal min, @Nullable BigDecimal max, @Nullable BigDecimal step, @Nullable Unit unit) { - super(CoreItemFactory.NUMBER, List.of(DecimalType.class, QuantityType.class, IncreaseDecreaseType.class, + super(getItemType(unit), List.of(DecimalType.class, QuantityType.class, IncreaseDecreaseType.class, UpDownType.class, StringType.class)); this.min = min; this.max = max; @@ -64,6 +66,17 @@ public class NumberValue extends Value { this.unit = unit; } + private static String getItemType(@Nullable Unit unit) { + if (unit == null) { + return CoreItemFactory.NUMBER; + } + String dimension = Units.MIRED.equals(unit) ? "Temperature" : UnitUtils.getDimensionName(unit); + if (dimension == null) { + return CoreItemFactory.NUMBER; + } + return CoreItemFactory.NUMBER + ":" + dimension; + } + protected boolean checkConditions(BigDecimal newValue) { BigDecimal min = this.min; if (min != null && newValue.compareTo(min) == -1) { diff --git a/bundles/org.openhab.binding.mqtt.generic/src/test/java/org/openhab/binding/mqtt/generic/values/ValueTests.java b/bundles/org.openhab.binding.mqtt.generic/src/test/java/org/openhab/binding/mqtt/generic/values/ValueTests.java index c79a0c8834a..6ac1e6090b3 100644 --- a/bundles/org.openhab.binding.mqtt.generic/src/test/java/org/openhab/binding/mqtt/generic/values/ValueTests.java +++ b/bundles/org.openhab.binding.mqtt.generic/src/test/java/org/openhab/binding/mqtt/generic/values/ValueTests.java @@ -229,6 +229,12 @@ public class ValueTests { assertThat(v.getMQTTpublishValue(command, null), is("20")); } + @Test + public void numberDimension() { + NumberValue v = new NumberValue(null, null, new BigDecimal(10), Units.MIRED); + assertThat(v.getItemType(), is("Number:Temperature")); + } + @Test public void rollershutterUpdateWithStrings() { RollershutterValue v = new RollershutterValue("fancyON", "fancyOff", "fancyStop");