diff --git a/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/internal/items/ItemUIRegistryImpl.java b/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/internal/items/ItemUIRegistryImpl.java index 011c980dfdb..78a7e832b2e 100644 --- a/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/internal/items/ItemUIRegistryImpl.java +++ b/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/internal/items/ItemUIRegistryImpl.java @@ -1264,6 +1264,11 @@ public void removeRegistryHook(RegistryHook hook) { // we require the item to define a dimension, otherwise no unit will be reported to the UIs. if (item instanceof NumberItem && ((NumberItem) item).getDimension() != null) { + if (w.getLabel() == null) { + // if no Label was assigned to the Widget we fallback to the items unit + return ((NumberItem) item).getUnitSymbol(); + } + String unit = getUnitFromLabel(w.getLabel()); if (!UnitUtils.UNIT_PLACEHOLDER.equals(unit)) { return unit; diff --git a/bundles/org.openhab.core.ui/src/test/java/org/openhab/core/ui/internal/items/ItemUIRegistryImplTest.java b/bundles/org.openhab.core.ui/src/test/java/org/openhab/core/ui/internal/items/ItemUIRegistryImplTest.java index 3f9b58d1484..a2093f08c5c 100644 --- a/bundles/org.openhab.core.ui/src/test/java/org/openhab/core/ui/internal/items/ItemUIRegistryImplTest.java +++ b/bundles/org.openhab.core.ui/src/test/java/org/openhab/core/ui/internal/items/ItemUIRegistryImplTest.java @@ -24,6 +24,8 @@ import java.util.List; import java.util.TimeZone; +import javax.measure.quantity.Temperature; + import org.eclipse.emf.common.util.BasicEList; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -875,4 +877,43 @@ public void getDefaultWidgetsForStringItem() { defaultWidget = uiRegistry.getDefaultWidget(StringItem.class, ITEM_NAME); assertThat(defaultWidget, is(instanceOf(Text.class))); } + + @Test + public void getUnitForWidgetForNonNumberItem() throws Exception { + String unit = uiRegistry.getUnitForWidget(widget); + + assertThat(unit, is("")); + } + + @Test + public void getUnitForWidgetWithWidgetLabel() throws Exception { + // a NumberItem having a Dimension must be returned + NumberItem item = mock(NumberItem.class); + when(registry.getItem(ITEM_NAME)).thenReturn(item); + + doReturn(Temperature.class).when(item).getDimension(); + + // we set the Label on the widget itself + when(widget.getLabel()).thenReturn("Label [%.1f °C]"); + + String unit = uiRegistry.getUnitForWidget(widget); + + assertThat(unit, is(equalTo("°C"))); + } + + @Test + public void getUnitForWidgetWithItemLabelAndWithoutWidgetLabel() throws Exception { + // a NumberItem having a Dimension must be returned + NumberItem item = mock(NumberItem.class); + when(registry.getItem(ITEM_NAME)).thenReturn(item); + + doReturn(Temperature.class).when(item).getDimension(); + + // we set the UnitSymbol on the item, this must be used as a fallback if no Widget label was used + when(item.getUnitSymbol()).thenReturn("°C"); + + String unit = uiRegistry.getUnitForWidget(widget); + + assertThat(unit, is(equalTo("°C"))); + } }