]> git.basschouten.com Git - openhab-addons.git/commitdiff
Adjust handling empty values (#15760)
authorChristian Kittel <EvilPingu@users.noreply.github.com>
Fri, 3 Nov 2023 18:35:19 +0000 (19:35 +0100)
committerGitHub <noreply@github.com>
Fri, 3 Nov 2023 18:35:19 +0000 (19:35 +0100)
Signed-off-by: Christian Kittel <ckittel@gmx.de>
bundles/org.openhab.binding.homematic/src/main/java/org/openhab/binding/homematic/internal/converter/type/AbstractTypeConverter.java
bundles/org.openhab.binding.homematic/src/test/java/org/openhab/binding/homematic/internal/converter/BaseConverterTest.java
bundles/org.openhab.binding.homematic/src/test/java/org/openhab/binding/homematic/internal/converter/ConvertFromBindingTest.java

index 288eee826c1eeae76699cc5328fe86da77e24faf..2036d023766ccc2311fc1b9299d691dfd1c42bd0 100644 (file)
@@ -115,9 +115,9 @@ public abstract class AbstractTypeConverter<T extends State> implements TypeConv
         if (dp.getValue() == null) {
             return (T) UnDefType.NULL;
         } else if (!fromBindingValidation(dp)) {
-            String errorMessage = String.format("Can't convert %s value '%s' with %s for '%s'", dp.getType(),
-                    dp.getValue(), this.getClass().getSimpleName(), new HmDatapointInfo(dp));
-            throw new ConverterTypeException(errorMessage);
+            logger.debug("Can't convert {} value '{}' with {} for '{}'", dp.getType(), dp.getValue(),
+                    this.getClass().getSimpleName(), new HmDatapointInfo(dp));
+            return (T) UnDefType.NULL;
         }
 
         return fromBinding(dp);
index 4ca3e534698162ba4214102fcb77440a6e8183b5..5b1d54f5f51cf9dce2d5fa84d1101fd5add64536 100644 (file)
@@ -39,5 +39,6 @@ public class BaseConverterTest {
         HmChannel stubChannel = new HmChannel("stubChannel", 0);
         stubChannel.setDevice(new HmDevice("LEQ123456", HmInterface.RF, "HM-STUB-DEVICE", "", "", ""));
         floatDp.setChannel(stubChannel);
+        integerDp.setChannel(stubChannel);
     }
 }
index 4b7e8ef68e2f16352cdb53298cc801bed138842e..2dfc759f9a64ecd7fb12f017f267226b4a54b5cc 100644 (file)
@@ -18,11 +18,13 @@ import static org.hamcrest.MatcherAssert.assertThat;
 import org.junit.jupiter.api.Test;
 import org.openhab.binding.homematic.internal.model.HmDatapoint;
 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.unit.ImperialUnits;
 import org.openhab.core.library.unit.SIUnits;
 import org.openhab.core.library.unit.Units;
 import org.openhab.core.types.State;
+import org.openhab.core.types.UnDefType;
 
 /**
  * Tests for
@@ -99,4 +101,29 @@ public class ConvertFromBindingTest extends BaseConverterTest {
         assertThat(((QuantityType<?>) convertedState).getUnit(), is(Units.PERCENT));
         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));
+    }
 }