[http] enable UoM for number channels (#9601)
* add unit Signed-off-by: Jan N. Klug <jan.n.klug@rub.de> * documentation an XML Signed-off-by: Jan N. Klug <jan.n.klug@rub.de> * address review comments Signed-off-by: Jan N. Klug <jan.n.klug@rub.de> * improvements Signed-off-by: Jan N. Klug <jan.n.klug@rub.de> * improvements Signed-off-by: Jan N. Klug <jan.n.klug@rub.de>
This commit is contained in:
@@ -21,9 +21,13 @@ import org.openhab.binding.http.internal.config.HttpChannelConfig;
|
||||
import org.openhab.binding.http.internal.transform.NoOpValueTransformation;
|
||||
import org.openhab.core.library.types.DecimalType;
|
||||
import org.openhab.core.library.types.PointType;
|
||||
import org.openhab.core.library.types.QuantityType;
|
||||
import org.openhab.core.library.types.StringType;
|
||||
import org.openhab.core.library.unit.SIUnits;
|
||||
import org.openhab.core.library.unit.Units;
|
||||
import org.openhab.core.types.Command;
|
||||
import org.openhab.core.types.State;
|
||||
import org.openhab.core.types.UnDefType;
|
||||
|
||||
/**
|
||||
* The {@link ConverterTest} is a test class for state converters
|
||||
@@ -33,6 +37,40 @@ import org.openhab.core.types.State;
|
||||
@NonNullByDefault
|
||||
public class ConverterTest {
|
||||
|
||||
@Test
|
||||
public void numberItemConverter() {
|
||||
NumberItemConverter converter = new NumberItemConverter(this::updateState, this::postCommand,
|
||||
this::sendHttpValue, NoOpValueTransformation.getInstance(), NoOpValueTransformation.getInstance(),
|
||||
new HttpChannelConfig());
|
||||
|
||||
// without unit
|
||||
Assertions.assertEquals(new DecimalType(1234), converter.toState("1234"));
|
||||
|
||||
// unit in transformation result
|
||||
Assertions.assertEquals(new QuantityType<>(100, SIUnits.CELSIUS), converter.toState("100°C"));
|
||||
|
||||
// no valid value
|
||||
Assertions.assertEquals(UnDefType.UNDEF, converter.toState("W"));
|
||||
Assertions.assertEquals(UnDefType.UNDEF, converter.toState(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void numberItemConverterWithUnit() {
|
||||
HttpChannelConfig channelConfig = new HttpChannelConfig();
|
||||
channelConfig.unit = "W";
|
||||
NumberItemConverter converter = new NumberItemConverter(this::updateState, this::postCommand,
|
||||
this::sendHttpValue, NoOpValueTransformation.getInstance(), NoOpValueTransformation.getInstance(),
|
||||
channelConfig);
|
||||
|
||||
// without unit
|
||||
Assertions.assertEquals(new QuantityType<>(500, Units.WATT), converter.toState("500"));
|
||||
|
||||
// no valid value
|
||||
Assertions.assertEquals(UnDefType.UNDEF, converter.toState("100°C"));
|
||||
Assertions.assertEquals(UnDefType.UNDEF, converter.toState("foo"));
|
||||
Assertions.assertEquals(UnDefType.UNDEF, converter.toState(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void stringTypeConverter() {
|
||||
GenericItemConverter converter = createConverter(StringType::new);
|
||||
|
||||
Reference in New Issue
Block a user