basicui] Fix setpoint/slider widgets when %unit% is used in state (#1612)
This is the same fix as #1611 but for OH 4.0 main branch. Signed-off-by: Laurent Garnier <lg.hc@free.fr>3.4.x
parent
52488f3683
commit
f7a0ff89ec
|
@ -14,13 +14,17 @@ package org.openhab.ui.basic.internal.render;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
|
import javax.measure.Unit;
|
||||||
|
|
||||||
import org.eclipse.emf.common.util.ECollections;
|
import org.eclipse.emf.common.util.ECollections;
|
||||||
import org.eclipse.emf.common.util.EList;
|
import org.eclipse.emf.common.util.EList;
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
import org.openhab.core.i18n.LocaleProvider;
|
import org.openhab.core.i18n.LocaleProvider;
|
||||||
import org.openhab.core.i18n.TranslationProvider;
|
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.Setpoint;
|
||||||
import org.openhab.core.model.sitemap.sitemap.Widget;
|
import org.openhab.core.model.sitemap.sitemap.Widget;
|
||||||
|
import org.openhab.core.types.State;
|
||||||
import org.openhab.core.ui.items.ItemUIRegistry;
|
import org.openhab.core.ui.items.ItemUIRegistry;
|
||||||
import org.openhab.ui.basic.render.RenderException;
|
import org.openhab.ui.basic.render.RenderException;
|
||||||
import org.openhab.ui.basic.render.WidgetRenderer;
|
import org.openhab.ui.basic.render.WidgetRenderer;
|
||||||
|
@ -70,6 +74,14 @@ public class SetpointRenderer extends AbstractWidgetRenderer {
|
||||||
}
|
}
|
||||||
|
|
||||||
String unit = getUnitForWidget(w);
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
String snippet = getSnippet("setpoint");
|
String snippet = getSnippet("setpoint");
|
||||||
|
|
||||||
|
@ -78,9 +90,7 @@ public class SetpointRenderer extends AbstractWidgetRenderer {
|
||||||
snippet = snippet.replace("%minValue%", minValue.toString());
|
snippet = snippet.replace("%minValue%", minValue.toString());
|
||||||
snippet = snippet.replace("%maxValue%", maxValue.toString());
|
snippet = snippet.replace("%maxValue%", maxValue.toString());
|
||||||
snippet = snippet.replace("%step%", step.toString());
|
snippet = snippet.replace("%step%", step.toString());
|
||||||
if (unit != null) {
|
snippet = snippet.replace("%unit%", unit == null ? "" : unit);
|
||||||
snippet = snippet.replace("%unit%", unit);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Process the color tags
|
// Process the color tags
|
||||||
snippet = processColor(w, snippet);
|
snippet = processColor(w, snippet);
|
||||||
|
|
|
@ -12,13 +12,17 @@
|
||||||
*/
|
*/
|
||||||
package org.openhab.ui.basic.internal.render;
|
package org.openhab.ui.basic.internal.render;
|
||||||
|
|
||||||
|
import javax.measure.Unit;
|
||||||
|
|
||||||
import org.eclipse.emf.common.util.ECollections;
|
import org.eclipse.emf.common.util.ECollections;
|
||||||
import org.eclipse.emf.common.util.EList;
|
import org.eclipse.emf.common.util.EList;
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
import org.openhab.core.i18n.LocaleProvider;
|
import org.openhab.core.i18n.LocaleProvider;
|
||||||
import org.openhab.core.i18n.TranslationProvider;
|
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.Slider;
|
||||||
import org.openhab.core.model.sitemap.sitemap.Widget;
|
import org.openhab.core.model.sitemap.sitemap.Widget;
|
||||||
|
import org.openhab.core.types.State;
|
||||||
import org.openhab.core.ui.items.ItemUIRegistry;
|
import org.openhab.core.ui.items.ItemUIRegistry;
|
||||||
import org.openhab.ui.basic.render.RenderException;
|
import org.openhab.ui.basic.render.RenderException;
|
||||||
import org.openhab.ui.basic.render.WidgetRenderer;
|
import org.openhab.ui.basic.render.WidgetRenderer;
|
||||||
|
@ -65,13 +69,22 @@ public class SliderRenderer extends AbstractWidgetRenderer {
|
||||||
String frequency = s.getFrequency() == 0 ? "200" : Integer.toString(s.getFrequency());
|
String frequency = s.getFrequency() == 0 ? "200" : Integer.toString(s.getFrequency());
|
||||||
|
|
||||||
String unit = getUnitForWidget(w);
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
snippet = preprocessSnippet(snippet, w);
|
snippet = preprocessSnippet(snippet, w);
|
||||||
snippet = snippet.replace("%frequency%", frequency);
|
snippet = snippet.replace("%frequency%", frequency);
|
||||||
snippet = snippet.replace("%switch%", s.isSwitchEnabled() ? "1" : "0");
|
snippet = snippet.replace("%switch%", s.isSwitchEnabled() ? "1" : "0");
|
||||||
if (unit != null) {
|
snippet = snippet.replace("%unit%", unit == null ? "" : unit);
|
||||||
snippet = snippet.replace("%unit%", unit);
|
|
||||||
}
|
|
||||||
snippet = snippet.replace("%minValue%", minValueOf(s));
|
snippet = snippet.replace("%minValue%", minValueOf(s));
|
||||||
snippet = snippet.replace("%maxValue%", maxValueOf(s));
|
snippet = snippet.replace("%maxValue%", maxValueOf(s));
|
||||||
snippet = snippet.replace("%step%", stepOf(s));
|
snippet = snippet.replace("%step%", stepOf(s));
|
||||||
|
|
|
@ -929,9 +929,9 @@
|
||||||
var stateAndUnit = itemState.split(" ");
|
var stateAndUnit = itemState.split(" ");
|
||||||
_t.value = stateAndUnit[0] * 1;
|
_t.value = stateAndUnit[0] * 1;
|
||||||
_t.unit = stateAndUnit[1];
|
_t.unit = stateAndUnit[1];
|
||||||
} else {
|
} else {
|
||||||
_t.value = itemState * 1;
|
_t.value = itemState * 1;
|
||||||
}
|
}
|
||||||
_t.valueNode.innerHTML = value;
|
_t.valueNode.innerHTML = value;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1543,6 +1543,10 @@
|
||||||
_t.reloadIcon(itemState);
|
_t.reloadIcon(itemState);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (value.indexOf(" ") > 0) {
|
||||||
|
var valueAndUnit = value.split(" ");
|
||||||
|
_t.unit = valueAndUnit[1];
|
||||||
|
}
|
||||||
_t.input.value = itemState;
|
_t.input.value = itemState;
|
||||||
_t.input.MaterialSlider.change();
|
_t.input.MaterialSlider.change();
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue