Skip to content

Commit 54095eb

Browse files
ccutrerlolodomo
authored andcommitted
[mqtt.generic] create the proper item type for channels with units (openhab#17929)
* [mqtt.generic] create the proper item type for channels with units Signed-off-by: Cody Cutrer <[email protected]>
1 parent f4cdcd8 commit 54095eb

File tree

2 files changed

+20
-1
lines changed
  • bundles/org.openhab.binding.mqtt.generic/src

2 files changed

+20
-1
lines changed

bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/values/NumberValue.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@
2525
import org.openhab.core.library.types.QuantityType;
2626
import org.openhab.core.library.types.StringType;
2727
import org.openhab.core.library.types.UpDownType;
28+
import org.openhab.core.library.unit.Units;
2829
import org.openhab.core.types.Command;
2930
import org.openhab.core.types.StateDescriptionFragmentBuilder;
3031
import org.openhab.core.types.Type;
3132
import org.openhab.core.types.UnDefType;
33+
import org.openhab.core.types.util.UnitUtils;
3234
import org.slf4j.Logger;
3335
import org.slf4j.LoggerFactory;
3436

@@ -56,14 +58,25 @@ public class NumberValue extends Value {
5658

5759
public NumberValue(@Nullable BigDecimal min, @Nullable BigDecimal max, @Nullable BigDecimal step,
5860
@Nullable Unit<?> unit) {
59-
super(CoreItemFactory.NUMBER, List.of(DecimalType.class, QuantityType.class, IncreaseDecreaseType.class,
61+
super(getItemType(unit), List.of(DecimalType.class, QuantityType.class, IncreaseDecreaseType.class,
6062
UpDownType.class, StringType.class));
6163
this.min = min;
6264
this.max = max;
6365
this.step = step == null ? BigDecimal.ONE : step;
6466
this.unit = unit;
6567
}
6668

69+
private static String getItemType(@Nullable Unit<?> unit) {
70+
if (unit == null) {
71+
return CoreItemFactory.NUMBER;
72+
}
73+
String dimension = Units.MIRED.equals(unit) ? "Temperature" : UnitUtils.getDimensionName(unit);
74+
if (dimension == null) {
75+
return CoreItemFactory.NUMBER;
76+
}
77+
return CoreItemFactory.NUMBER + ":" + dimension;
78+
}
79+
6780
protected boolean checkConditions(BigDecimal newValue) {
6881
BigDecimal min = this.min;
6982
if (min != null && newValue.compareTo(min) == -1) {

bundles/org.openhab.binding.mqtt.generic/src/test/java/org/openhab/binding/mqtt/generic/values/ValueTests.java

+6
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,12 @@ public void numberPercentageUpdate() {
229229
assertThat(v.getMQTTpublishValue(command, null), is("20"));
230230
}
231231

232+
@Test
233+
public void numberDimension() {
234+
NumberValue v = new NumberValue(null, null, new BigDecimal(10), Units.MIRED);
235+
assertThat(v.getItemType(), is("Number:Temperature"));
236+
}
237+
232238
@Test
233239
public void rollershutterUpdateWithStrings() {
234240
RollershutterValue v = new RollershutterValue("fancyON", "fancyOff", "fancyStop");

0 commit comments

Comments
 (0)