Adjust handling empty values (#15760)
Signed-off-by: Christian Kittel <ckittel@gmx.de>
This commit is contained in:
parent
2df66bfee2
commit
cb74d85eb0
@ -115,9 +115,9 @@ public abstract class AbstractTypeConverter<T extends State> implements TypeConv
|
|||||||
if (dp.getValue() == null) {
|
if (dp.getValue() == null) {
|
||||||
return (T) UnDefType.NULL;
|
return (T) UnDefType.NULL;
|
||||||
} else if (!fromBindingValidation(dp)) {
|
} else if (!fromBindingValidation(dp)) {
|
||||||
String errorMessage = String.format("Can't convert %s value '%s' with %s for '%s'", dp.getType(),
|
logger.debug("Can't convert {} value '{}' with {} for '{}'", dp.getType(), dp.getValue(),
|
||||||
dp.getValue(), this.getClass().getSimpleName(), new HmDatapointInfo(dp));
|
this.getClass().getSimpleName(), new HmDatapointInfo(dp));
|
||||||
throw new ConverterTypeException(errorMessage);
|
return (T) UnDefType.NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
return fromBinding(dp);
|
return fromBinding(dp);
|
||||||
|
|||||||
@ -39,5 +39,6 @@ public class BaseConverterTest {
|
|||||||
HmChannel stubChannel = new HmChannel("stubChannel", 0);
|
HmChannel stubChannel = new HmChannel("stubChannel", 0);
|
||||||
stubChannel.setDevice(new HmDevice("LEQ123456", HmInterface.RF, "HM-STUB-DEVICE", "", "", ""));
|
stubChannel.setDevice(new HmDevice("LEQ123456", HmInterface.RF, "HM-STUB-DEVICE", "", "", ""));
|
||||||
floatDp.setChannel(stubChannel);
|
floatDp.setChannel(stubChannel);
|
||||||
|
integerDp.setChannel(stubChannel);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,11 +18,13 @@ import static org.hamcrest.MatcherAssert.assertThat;
|
|||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.openhab.binding.homematic.internal.model.HmDatapoint;
|
import org.openhab.binding.homematic.internal.model.HmDatapoint;
|
||||||
import org.openhab.core.library.types.DecimalType;
|
import org.openhab.core.library.types.DecimalType;
|
||||||
|
import org.openhab.core.library.types.PercentType;
|
||||||
import org.openhab.core.library.types.QuantityType;
|
import org.openhab.core.library.types.QuantityType;
|
||||||
import org.openhab.core.library.unit.ImperialUnits;
|
import org.openhab.core.library.unit.ImperialUnits;
|
||||||
import org.openhab.core.library.unit.SIUnits;
|
import org.openhab.core.library.unit.SIUnits;
|
||||||
import org.openhab.core.library.unit.Units;
|
import org.openhab.core.library.unit.Units;
|
||||||
import org.openhab.core.types.State;
|
import org.openhab.core.types.State;
|
||||||
|
import org.openhab.core.types.UnDefType;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for
|
* Tests for
|
||||||
@ -99,4 +101,29 @@ public class ConvertFromBindingTest extends BaseConverterTest {
|
|||||||
assertThat(((QuantityType<?>) convertedState).getUnit(), is(Units.PERCENT));
|
assertThat(((QuantityType<?>) convertedState).getUnit(), is(Units.PERCENT));
|
||||||
assertThat(((QuantityType<?>) convertedState).toUnit(Units.ONE).doubleValue(), is(0.7));
|
assertThat(((QuantityType<?>) convertedState).toUnit(Units.ONE).doubleValue(), is(0.7));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPercentTypeConverter() throws ConverterException {
|
||||||
|
State convertedState;
|
||||||
|
TypeConverter<?> percentTypeConverter = ConverterFactory.createConverter("Dimmer");
|
||||||
|
|
||||||
|
// the binding is backwards compatible, so clients may still use DecimalType, even if a unit is used
|
||||||
|
integerDp.setUnit("%");
|
||||||
|
|
||||||
|
integerDp.setValue(99.9);
|
||||||
|
integerDp.setMaxValue(100);
|
||||||
|
convertedState = percentTypeConverter.convertFromBinding(integerDp);
|
||||||
|
assertThat(convertedState, instanceOf(PercentType.class));
|
||||||
|
assertThat(((PercentType) convertedState).doubleValue(), is(99.0));
|
||||||
|
|
||||||
|
integerDp.setValue(77.77777778);
|
||||||
|
convertedState = percentTypeConverter.convertFromBinding(integerDp);
|
||||||
|
assertThat(convertedState, instanceOf(PercentType.class));
|
||||||
|
assertThat(((PercentType) convertedState).doubleValue(), is(77.0));
|
||||||
|
|
||||||
|
integerDp.setValue("");
|
||||||
|
convertedState = percentTypeConverter.convertFromBinding(integerDp);
|
||||||
|
assertThat(convertedState, instanceOf(UnDefType.class));
|
||||||
|
assertThat(((UnDefType) convertedState), is(UnDefType.NULL));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user