Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[basicui] Fix setpoint/slider widgets when %unit% is used in state #1611

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -70,6 +74,17 @@ public EList<Widget> 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");

Expand All @@ -78,9 +93,7 @@ public EList<Widget> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -65,13 +69,25 @@ public EList<Widget> 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));
Expand Down
10 changes: 7 additions & 3 deletions bundles/org.openhab.ui.basic/web-src/smarthome.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down Expand Up @@ -1543,6 +1543,10 @@
_t.reloadIcon(itemState);
return;
}
if (value.indexOf(" ") > 0) {
var valueAndUnit = value.split(" ");
_t.unit = valueAndUnit[1];
}
_t.input.value = itemState;
_t.input.MaterialSlider.change();
};
Expand Down