Skip to content

Commit

Permalink
fix: Support Quantity Types (openhab#11229)
Browse files Browse the repository at this point in the history
Signed-off-by: Schraffl Peter <p.schraffl@gmx.at>
  • Loading branch information
hypetsch authored and frederictobiasc committed Oct 26, 2021
1 parent 28db0e6 commit db6b730
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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;
Expand Down Expand Up @@ -134,8 +135,13 @@ private static State getStateForSwitchValueChannel(BsbLanApiParameterDTO paramet
}

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
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;

/**
Expand Down Expand Up @@ -219,11 +222,14 @@ public void testGetValueForReadonlyChannels() {
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
Expand Down

0 comments on commit db6b730

Please sign in to comment.