fix: Support Quantity Types (#11229)
Signed-off-by: Schraffl Peter <p.schraffl@gmx.at>
This commit is contained in:
parent
937a331f67
commit
437fb7f336
|
@ -21,6 +21,7 @@ import org.openhab.binding.bsblan.internal.api.dto.BsbLanApiParameterDTO;
|
|||
import org.openhab.binding.bsblan.internal.handler.BsbLanParameterHandler;
|
||||
import org.openhab.core.library.types.DecimalType;
|
||||
import org.openhab.core.library.types.OnOffType;
|
||||
import org.openhab.core.library.types.QuantityType;
|
||||
import org.openhab.core.library.types.StringType;
|
||||
import org.openhab.core.types.Command;
|
||||
import org.openhab.core.types.State;
|
||||
|
@ -134,8 +135,13 @@ public class BsbLanParameterConverter {
|
|||
}
|
||||
|
||||
private static @Nullable String getValueForNumberValueChannel(Command command) {
|
||||
if (command instanceof QuantityType<?>) {
|
||||
// the target unit is yet unknown, so just use the value as is (without converting based on the unit)
|
||||
QuantityType<?> quantity = (QuantityType<?>) command;
|
||||
return String.valueOf(quantity.doubleValue());
|
||||
}
|
||||
// check if numeric
|
||||
if (command.toString().matches("-?\\d+(\\.\\d+)?")) {
|
||||
else if (command.toString().matches("-?\\d+(\\.\\d+)?")) {
|
||||
return command.toString();
|
||||
}
|
||||
LOGGER.warn("Command '{}' is not a valid number value", command);
|
||||
|
|
|
@ -20,7 +20,10 @@ import org.junit.jupiter.api.Test;
|
|||
import org.openhab.binding.bsblan.internal.api.dto.BsbLanApiParameterDTO;
|
||||
import org.openhab.core.library.types.DecimalType;
|
||||
import org.openhab.core.library.types.OnOffType;
|
||||
import org.openhab.core.library.types.PercentType;
|
||||
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.types.State;
|
||||
|
||||
/**
|
||||
|
@ -219,11 +222,14 @@ public class BsbLanParameterConverterTests {
|
|||
public void testGetValueForNumberValueChannel() {
|
||||
assertNull(BsbLanParameterConverter.getValue(PARAMETER_CHANNEL_NUMBER_VALUE, OnOffType.ON), "1");
|
||||
assertNull(BsbLanParameterConverter.getValue(PARAMETER_CHANNEL_NUMBER_VALUE, OnOffType.OFF), "0");
|
||||
assertEquals(BsbLanParameterConverter.getValue(PARAMETER_CHANNEL_NUMBER_VALUE, new DecimalType(42)), "42");
|
||||
assertEquals(BsbLanParameterConverter.getValue(PARAMETER_CHANNEL_NUMBER_VALUE, new DecimalType(22.5)), "22.5");
|
||||
assertEquals("42", BsbLanParameterConverter.getValue(PARAMETER_CHANNEL_NUMBER_VALUE, new DecimalType(42)));
|
||||
assertEquals("22.5", BsbLanParameterConverter.getValue(PARAMETER_CHANNEL_NUMBER_VALUE, new DecimalType(22.5)));
|
||||
assertNull(BsbLanParameterConverter.getValue(PARAMETER_CHANNEL_NUMBER_VALUE,
|
||||
new StringType("Not a number value")));
|
||||
assertNull(BsbLanParameterConverter.getValue(PARAMETER_CHANNEL_NUMBER_VALUE, new StringType("")));
|
||||
assertEquals("75", BsbLanParameterConverter.getValue(PARAMETER_CHANNEL_NUMBER_VALUE, new PercentType(75)));
|
||||
assertEquals("22.5", BsbLanParameterConverter.getValue(PARAMETER_CHANNEL_NUMBER_VALUE,
|
||||
new QuantityType<>(22.5, SIUnits.CELSIUS)));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
Loading…
Reference in New Issue