fix: Support Quantity Types (#11229)

Signed-off-by: Schraffl Peter <p.schraffl@gmx.at>
This commit is contained in:
Peter Schraffl 2021-10-03 01:38:28 +02:00 committed by GitHub
parent 937a331f67
commit 437fb7f336
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 3 deletions

View File

@ -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);

View File

@ -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