Search in sources :

Example 1 with UnitProvider

use of org.eclipse.smarthome.core.i18n.UnitProvider 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()));
}
Also used : NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) UnitProvider(org.eclipse.smarthome.core.i18n.UnitProvider) Length(javax.measure.quantity.Length) QuantityType(org.eclipse.smarthome.core.library.types.QuantityType) Test(org.junit.Test)

Example 2 with UnitProvider

use of org.eclipse.smarthome.core.i18n.UnitProvider 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")));
}
Also used : NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) UnitProvider(org.eclipse.smarthome.core.i18n.UnitProvider) QuantityType(org.eclipse.smarthome.core.library.types.QuantityType) State(org.eclipse.smarthome.core.types.State) Test(org.junit.Test)

Example 3 with UnitProvider

use of org.eclipse.smarthome.core.i18n.UnitProvider in project smarthome by eclipse.

the class NumberItemTest method testSetQuantityTypeUnconverted.

@Test
public void testSetQuantityTypeUnconverted() {
    NumberItem item = new NumberItem("Number:Temperature", "test");
    UnitProvider unitProvider = mock(UnitProvider.class);
    when(unitProvider.getUnit(Temperature.class)).thenReturn(SIUnits.CELSIUS);
    item.setUnitProvider(unitProvider);
    // should not be accepted as valid state
    item.setState(new QuantityType<>("10 A"));
    assertThat(item.getState(), is(UnDefType.NULL));
}
Also used : UnitProvider(org.eclipse.smarthome.core.i18n.UnitProvider) Test(org.junit.Test)

Aggregations

UnitProvider (org.eclipse.smarthome.core.i18n.UnitProvider)3 Test (org.junit.Test)3 NumberItem (org.eclipse.smarthome.core.library.items.NumberItem)2 QuantityType (org.eclipse.smarthome.core.library.types.QuantityType)2 Length (javax.measure.quantity.Length)1 State (org.eclipse.smarthome.core.types.State)1