[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 <cody@cutrer.us>
This commit is contained in:
Cody Cutrer 2024-12-29 11:18:36 -07:00 committed by GitHub
parent 9e6be5a9e0
commit dcb6d6e2c2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 1 deletions

View File

@ -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) {

View File

@ -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");