Skip to content

Commit

Permalink
[mqtt.generic] create the proper item type for channels with units (#…
Browse files Browse the repository at this point in the history
…17929)

* [mqtt.generic] create the proper item type for channels with units

Signed-off-by: Cody Cutrer <[email protected]>
  • Loading branch information
ccutrer authored Dec 29, 2024
1 parent 9e6be5a commit dcb6d6e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
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;

Expand Down Expand Up @@ -56,14 +58,25 @@ 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;
this.step = step == null ? BigDecimal.ONE : step;
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,12 @@ public void numberPercentageUpdate() {
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");
Expand Down

0 comments on commit dcb6d6e

Please sign in to comment.