From 3700ac8cb6185b2875d0b51c82a4acda50f3b8ee Mon Sep 17 00:00:00 2001 From: Laurent Garnier Date: Mon, 26 Dec 2022 21:30:45 +0100 Subject: [PATCH] [basicui] Fix setpoint/slider widgets when %unit% is used in state pattern In OH3.4, getUnitForWidget(Widget w) now returns null when the state pattern contains "%unit%". In that case, the unit must be searched in the item state. Fix #1605 Signed-off-by: Laurent Garnier --- .../internal/render/SetpointRenderer.java | 19 +++++++++++++--- .../basic/internal/render/SliderRenderer.java | 22 ++++++++++++++++--- .../org.openhab.ui.basic/web-src/smarthome.js | 6 ++--- 3 files changed, 38 insertions(+), 9 deletions(-) diff --git a/bundles/org.openhab.ui.basic/src/main/java/org/openhab/ui/basic/internal/render/SetpointRenderer.java b/bundles/org.openhab.ui.basic/src/main/java/org/openhab/ui/basic/internal/render/SetpointRenderer.java index 6005d4abc8..306186ef87 100644 --- a/bundles/org.openhab.ui.basic/src/main/java/org/openhab/ui/basic/internal/render/SetpointRenderer.java +++ b/bundles/org.openhab.ui.basic/src/main/java/org/openhab/ui/basic/internal/render/SetpointRenderer.java @@ -14,13 +14,17 @@ import java.math.BigDecimal; +import javax.measure.Unit; + import org.eclipse.emf.common.util.ECollections; import org.eclipse.emf.common.util.EList; import org.eclipse.jdt.annotation.NonNullByDefault; import org.openhab.core.i18n.LocaleProvider; import org.openhab.core.i18n.TranslationProvider; +import org.openhab.core.library.types.QuantityType; import org.openhab.core.model.sitemap.sitemap.Setpoint; import org.openhab.core.model.sitemap.sitemap.Widget; +import org.openhab.core.types.State; import org.openhab.core.ui.items.ItemUIRegistry; import org.openhab.ui.basic.render.RenderException; import org.openhab.ui.basic.render.WidgetRenderer; @@ -70,6 +74,17 @@ public EList renderWidget(Widget w, StringBuilder sb, String sitemap) th } String unit = getUnitForWidget(w); + if (unit == null) { + // Search the unit in the item state + State state = itemUIRegistry.getState(w); + if (state instanceof QuantityType) { + Unit stateUnit = ((QuantityType) state).getUnit(); + unit = stateUnit.toString(); + } + } + if (unit == null) { + unit = ""; + } String snippet = getSnippet("setpoint"); @@ -78,9 +93,7 @@ public EList renderWidget(Widget w, StringBuilder sb, String sitemap) th snippet = snippet.replace("%minValue%", minValue.toString()); snippet = snippet.replace("%maxValue%", maxValue.toString()); snippet = snippet.replace("%step%", step.toString()); - if (unit != null) { - snippet = snippet.replace("%unit%", unit); - } + snippet = snippet.replace("%unit%", unit); // Process the color tags snippet = processColor(w, snippet); diff --git a/bundles/org.openhab.ui.basic/src/main/java/org/openhab/ui/basic/internal/render/SliderRenderer.java b/bundles/org.openhab.ui.basic/src/main/java/org/openhab/ui/basic/internal/render/SliderRenderer.java index 38c7f06e14..a5134a2577 100644 --- a/bundles/org.openhab.ui.basic/src/main/java/org/openhab/ui/basic/internal/render/SliderRenderer.java +++ b/bundles/org.openhab.ui.basic/src/main/java/org/openhab/ui/basic/internal/render/SliderRenderer.java @@ -12,13 +12,17 @@ */ package org.openhab.ui.basic.internal.render; +import javax.measure.Unit; + import org.eclipse.emf.common.util.ECollections; import org.eclipse.emf.common.util.EList; import org.eclipse.jdt.annotation.NonNullByDefault; import org.openhab.core.i18n.LocaleProvider; import org.openhab.core.i18n.TranslationProvider; +import org.openhab.core.library.types.QuantityType; import org.openhab.core.model.sitemap.sitemap.Slider; import org.openhab.core.model.sitemap.sitemap.Widget; +import org.openhab.core.types.State; import org.openhab.core.ui.items.ItemUIRegistry; import org.openhab.ui.basic.render.RenderException; import org.openhab.ui.basic.render.WidgetRenderer; @@ -65,13 +69,25 @@ public EList renderWidget(Widget w, StringBuilder sb, String sitemap) th String frequency = s.getFrequency() == 0 ? "200" : Integer.toString(s.getFrequency()); String unit = getUnitForWidget(w); + if (unit == null) { + // Search the unit in the item state + // Do not use itemUIRegistry.getState(w) as it will return a DecimalType for a slider widget + // even if the item state is a QuantityType + String itemName = w.getItem(); + State state = itemName != null ? itemUIRegistry.getItemState(itemName) : null; + if (state instanceof QuantityType) { + Unit stateUnit = ((QuantityType) state).getUnit(); + unit = stateUnit.toString(); + } + } + if (unit == null) { + unit = ""; + } snippet = preprocessSnippet(snippet, w); snippet = snippet.replace("%frequency%", frequency); snippet = snippet.replace("%switch%", s.isSwitchEnabled() ? "1" : "0"); - if (unit != null) { - snippet = snippet.replace("%unit%", unit); - } + snippet = snippet.replace("%unit%", unit); snippet = snippet.replace("%minValue%", minValueOf(s)); snippet = snippet.replace("%maxValue%", maxValueOf(s)); snippet = snippet.replace("%step%", stepOf(s)); diff --git a/bundles/org.openhab.ui.basic/web-src/smarthome.js b/bundles/org.openhab.ui.basic/web-src/smarthome.js index 8b67075ca0..547b03648f 100644 --- a/bundles/org.openhab.ui.basic/web-src/smarthome.js +++ b/bundles/org.openhab.ui.basic/web-src/smarthome.js @@ -929,9 +929,9 @@ var stateAndUnit = itemState.split(" "); _t.value = stateAndUnit[0] * 1; _t.unit = stateAndUnit[1]; - } else { - _t.value = itemState * 1; - } + } else { + _t.value = itemState * 1; + } _t.valueNode.innerHTML = value; };