]> git.basschouten.com Git - openhab-addons.git/commitdiff
[mqtt] support mired<->Kelvin unit conversion (#13575)
authorCody Cutrer <cody@cutrer.us>
Sat, 22 Oct 2022 18:07:33 +0000 (12:07 -0600)
committerGitHub <noreply@github.com>
Sat, 22 Oct 2022 18:07:33 +0000 (20:07 +0200)
Just have to use the newer toInvertibleUnit method that allows
the dimension to change.

Signed-off-by: Cody Cutrer <cody@cutrer.us>
bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/values/NumberValue.java
bundles/org.openhab.binding.mqtt.generic/src/test/java/org/openhab/binding/mqtt/generic/values/ValueTests.java

index 26e352dce06528f6fd53fd36d253b63e1379fd6c..5b7668bc6ad045582fd7bfae4e387b63d2e7fd14 100644 (file)
@@ -130,7 +130,7 @@ public class NumberValue extends Value {
     private BigDecimal getQuantityTypeAsDecimal(QuantityType<?> qType) {
         BigDecimal val = qType.toBigDecimal();
         if (!qType.getUnit().isCompatible(Units.ONE)) {
-            QuantityType<?> convertedType = qType.toUnit(unit);
+            QuantityType<?> convertedType = qType.toInvertibleUnit(unit);
             if (convertedType != null) {
                 val = convertedType.toBigDecimal();
             }
index 8a407c3ad476a4e8e6a162284774c5d776207e62..080cdf3ccb6def6c15b9db8053d7bb8f626d1686 100644 (file)
@@ -184,6 +184,14 @@ public class ValueTests {
         assertThat(v.getChannelState(), is(new QuantityType<>(20, Units.WATT)));
     }
 
+    @Test
+    public void numberUpdateMireds() {
+        NumberValue v = new NumberValue(null, null, new BigDecimal(10), Units.MIRED);
+
+        v.update(new QuantityType<>(2700, Units.KELVIN));
+        assertThat(v.getMQTTpublishValue("%.0f"), is("370"));
+    }
+
     @Test
     public void numberPercentageUpdate() {
         NumberValue v = new NumberValue(null, null, new BigDecimal(10), Units.PERCENT);