use of org.eclipse.smarthome.core.library.items.NumberItem in project smarthome by eclipse.
the class ItemStateConverterImplTest method testStateConversion.
@Test
public void testStateConversion() {
Item item = new NumberItem("number");
State originalState = new PercentType("42");
State convertedState = itemStateConverter.convertToAcceptedState(originalState, item);
assertThat(convertedState, is(new DecimalType("0.42")));
}
use of org.eclipse.smarthome.core.library.items.NumberItem in project smarthome by eclipse.
the class ItemStateConverterImplTest method numberItemShouldNotConvertUnitsWhereMeasurmentSystemEquals.
@Test
public void numberItemShouldNotConvertUnitsWhereMeasurmentSystemEquals() {
NumberItem item = mock(NumberItem.class);
doReturn(Length.class).when(item).getDimension();
UnitProvider unitProvider = mock(UnitProvider.class);
when(unitProvider.getUnit(Length.class)).thenReturn(SIUnits.METRE);
itemStateConverter.setUnitProvider(unitProvider);
QuantityType<Length> originalState = new QuantityType<>("100 cm");
@SuppressWarnings("unchecked") QuantityType<Length> convertedState = (QuantityType<Length>) itemStateConverter.convertToAcceptedState(originalState, item);
assertThat(convertedState.getUnit(), is(originalState.getUnit()));
}
use of org.eclipse.smarthome.core.library.items.NumberItem in project smarthome by eclipse.
the class ItemStateConverterImplTest method testNoConversion.
@Test
public void testNoConversion() {
Item item = new NumberItem("number");
State originalState = new DecimalType(12.34);
State state = itemStateConverter.convertToAcceptedState(originalState, item);
assertTrue(originalState == state);
}
use of org.eclipse.smarthome.core.library.items.NumberItem in project smarthome by eclipse.
the class ItemStateConverterImplTest method numberItemWitDimensionShouldConvertToItemStateDescriptionUnit.
@Test
public void numberItemWitDimensionShouldConvertToItemStateDescriptionUnit() {
NumberItem item = mock(NumberItem.class);
StateDescription stateDescription = mock(StateDescription.class);
when(item.getStateDescription()).thenReturn(stateDescription);
doReturn(Temperature.class).when(item).getDimension();
when(stateDescription.getPattern()).thenReturn("%.1f K");
State originalState = new QuantityType<>("12.34 °C");
State convertedState = itemStateConverter.convertToAcceptedState(originalState, item);
assertThat(convertedState, is(new QuantityType<>("285.49 K")));
}
use of org.eclipse.smarthome.core.library.items.NumberItem in project smarthome by eclipse.
the class SelectionRenderer method renderWidget.
@Override
public EList<Widget> renderWidget(Widget w, StringBuilder sb) throws RenderException {
String snippet = getSnippet("selection");
snippet = preprocessSnippet(snippet, w);
snippet = StringUtils.replace(snippet, "%value_map%", getMappingsJSON((Selection) w));
snippet = StringUtils.replace(snippet, "%label_header%", getLabel(w));
State state = itemUIRegistry.getState(w);
Selection selection = (Selection) w;
String mappingLabel = null;
Item item = null;
try {
item = itemUIRegistry.getItem(w.getItem());
} catch (ItemNotFoundException e) {
logger.debug("Failed to retrieve item during widget rendering: {}", e.getMessage());
}
StringBuilder rowSB = new StringBuilder();
for (Mapping mapping : selection.getMappings()) {
String rowSnippet = getSnippet("selection_row");
String command = mapping.getCmd() != null ? mapping.getCmd() : "";
String label = mapping.getLabel();
if (item instanceof NumberItem && ((NumberItem) item).getDimension() != null) {
String unit = getUnitForWidget(w);
command = StringUtils.replace(command, UnitUtils.UNIT_PLACEHOLDER, unit);
label = StringUtils.replace(label, UnitUtils.UNIT_PLACEHOLDER, unit);
// Special treatment for °C since uom library uses a single character: ℃
// This will ensure the current state matches the cmd and the buttonClass is set accordingly.
command = StringUtils.replace(command, "°C", "℃");
}
rowSnippet = StringUtils.replace(rowSnippet, "%item%", w.getItem() != null ? w.getItem() : "");
rowSnippet = StringUtils.replace(rowSnippet, "%cmd%", escapeHtml(command));
rowSnippet = StringUtils.replace(rowSnippet, "%label%", label != null ? escapeHtml(label) : "");
State compareMappingState = state;
if (state instanceof QuantityType) {
// convert the item state to the command value for proper
// comparison and "checked" attribute calculation
compareMappingState = convertStateToLabelUnit((QuantityType<?>) state, command);
}
if (compareMappingState.toString().equals(command)) {
mappingLabel = label;
rowSnippet = StringUtils.replace(rowSnippet, "%checked%", "checked=\"true\"");
} else {
rowSnippet = StringUtils.replace(rowSnippet, "%checked%", "");
}
rowSB.append(rowSnippet);
}
snippet = StringUtils.replace(snippet, "%rows%", rowSB.toString());
snippet = StringUtils.replace(snippet, "%value_header%", mappingLabel != null ? mappingLabel : "");
// Process the color tags
snippet = processColor(w, snippet);
sb.append(snippet);
return null;
}
Aggregations