use of org.eclipse.smarthome.core.library.types.QuantityType in project smarthome by eclipse.
the class GroupItemTest method assertThatNumberGroupItemWithDifferentDimensionsCalculatesCorrectState.
@Test
public void assertThatNumberGroupItemWithDifferentDimensionsCalculatesCorrectState() {
NumberItem baseItem = createNumberItem("baseItem", Temperature.class, UnDefType.NULL);
GroupFunctionDTO gfDTO = new GroupFunctionDTO();
gfDTO.name = "sum";
GroupFunction function = groupFunctionHelper.createGroupFunction(gfDTO, Collections.emptyList(), Temperature.class);
GroupItem groupItem = new GroupItem("number", baseItem, function);
groupItem.setUnitProvider(unitProvider);
groupItem.setItemStateConverter(itemStateConverter);
NumberItem celsius = createNumberItem("C", Temperature.class, new QuantityType<Temperature>("23 °C"));
groupItem.addMember(celsius);
NumberItem hectoPascal = createNumberItem("F", Pressure.class, new QuantityType<Pressure>("1010 hPa"));
groupItem.addMember(hectoPascal);
NumberItem percent = createNumberItem("K", Dimensionless.class, new QuantityType<Dimensionless>("110 %"));
groupItem.addMember(percent);
QuantityType<?> state = (QuantityType<?>) groupItem.getStateAs(QuantityType.class);
assertThat(state, is(new QuantityType<Temperature>("23 °C")));
groupItem.stateUpdated(celsius, UnDefType.NULL);
assertThat(groupItem.getState(), is(new QuantityType<Temperature>("23 °C")));
}
use of org.eclipse.smarthome.core.library.types.QuantityType in project smarthome by eclipse.
the class StateUtil method getAllStates.
public static List<State> getAllStates() {
LinkedList<State> states = new LinkedList<>();
DateTimeType dateTime = new DateTimeType();
states.add(dateTime);
DecimalType decimal = new DecimalType(23);
states.add(decimal);
PercentType percent = new PercentType(50);
states.add(percent);
HSBType hsb = new HSBType("50,75,42");
states.add(hsb);
states.add(OnOffType.ON);
states.add(OnOffType.OFF);
states.add(OpenClosedType.OPEN);
states.add(OpenClosedType.CLOSED);
states.add(PlayPauseType.PLAY);
states.add(PlayPauseType.PAUSE);
PointType point = new PointType("42.23,23.5");
states.add(point);
RawType raw = new RawType(new byte[0], "application/octet-stream");
states.add(raw);
states.add(RewindFastforwardType.REWIND);
states.add(RewindFastforwardType.FASTFORWARD);
StringListType stringList = new StringListType(new String[] { "foo", "bar" });
states.add(stringList);
StringType string = new StringType("foo");
states.add(string);
states.add(UnDefType.NULL);
states.add(UnDefType.UNDEF);
states.add(UpDownType.UP);
states.add(UpDownType.DOWN);
QuantityType<Temperature> quantityType = new QuantityType<Temperature>("12 °C");
states.add(quantityType);
return states;
}
use of org.eclipse.smarthome.core.library.types.QuantityType in project smarthome by eclipse.
the class ItemStateConverterImpl method convertToAcceptedState.
@Override
@NonNull
public State convertToAcceptedState(@Nullable State state, @Nullable Item item) {
if (state == null) {
logger.error("A conversion of null was requested:", new IllegalArgumentException("State must not be null."));
return UnDefType.NULL;
}
if (item != null && !isAccepted(item, state)) {
for (Class<? extends State> acceptedType : item.getAcceptedDataTypes()) {
State convertedState = state.as(acceptedType);
if (convertedState != null) {
logger.debug("Converting {} '{}' to {} '{}' for item '{}'", state.getClass().getSimpleName(), state, convertedState.getClass().getSimpleName(), convertedState, item.getName());
return convertedState;
}
}
}
if (item instanceof NumberItem && state instanceof QuantityType) {
NumberItem numberItem = (NumberItem) item;
if (numberItem.getDimension() != null) {
QuantityType<?> quantityState = (QuantityType<?>) state;
// in case the item does define a unit it takes precedence over all other conversions:
Unit<?> itemUnit = parseItemUnit(numberItem);
if (itemUnit != null) {
if (!itemUnit.equals(quantityState.getUnit())) {
return convertOrUndef(quantityState, itemUnit);
}
return quantityState;
}
Class<? extends Quantity<?>> dimension = numberItem.getDimension();
@SuppressWarnings({ "unchecked", "rawtypes" }) Unit<? extends Quantity<?>> conversionUnit = unitProvider.getUnit((Class<Quantity>) dimension);
if (conversionUnit != null && UnitUtils.isDifferentMeasurementSystem(conversionUnit, quantityState.getUnit())) {
return convertOrUndef(quantityState, conversionUnit);
}
return state;
} else {
return state.as(DecimalType.class);
}
}
return state;
}
use of org.eclipse.smarthome.core.library.types.QuantityType in project smarthome by eclipse.
the class ItemStateConverterImplTest method numberItemWitDimensionShouldConvertToLocaleBasedUnit.
@Test
public void numberItemWitDimensionShouldConvertToLocaleBasedUnit() {
NumberItem item = mock(NumberItem.class);
doReturn(Temperature.class).when(item).getDimension();
UnitProvider unitProvider = mock(UnitProvider.class);
when(unitProvider.getUnit(Temperature.class)).thenReturn(ImperialUnits.FAHRENHEIT);
itemStateConverter.setUnitProvider(unitProvider);
State originalState = new QuantityType<>("12.34 °C");
State convertedState = itemStateConverter.convertToAcceptedState(originalState, item);
assertThat(convertedState, is(new QuantityType<>("54.212 °F")));
}
use of org.eclipse.smarthome.core.library.types.QuantityType in project smarthome by eclipse.
the class ItemStateConverterImplTest method numberItemWithoutDimensionShouldConvertToDecimalType.
@Test
public void numberItemWithoutDimensionShouldConvertToDecimalType() {
Item item = new NumberItem("number");
State originalState = new QuantityType<>("12.34 °C");
State convertedState = itemStateConverter.convertToAcceptedState(originalState, item);
assertThat(convertedState, is(new DecimalType("12.34")));
}
Aggregations