From: Cody Cutrer Date: Sat, 22 Oct 2022 18:07:33 +0000 (-0600) Subject: [mqtt] support mired<->Kelvin unit conversion (#13575) X-Git-Url: https://git.basschouten.com/?a=commitdiff_plain;h=5f6241cbc62b8c1cf18bbe94aab2de0f683a8de1;p=openhab-addons.git [mqtt] support mired<->Kelvin unit conversion (#13575) Just have to use the newer toInvertibleUnit method that allows the dimension to change. Signed-off-by: Cody Cutrer --- diff --git a/bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/values/NumberValue.java b/bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/values/NumberValue.java index 26e352dce0..5b7668bc6a 100644 --- a/bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/values/NumberValue.java +++ b/bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/values/NumberValue.java @@ -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(); } diff --git a/bundles/org.openhab.binding.mqtt.generic/src/test/java/org/openhab/binding/mqtt/generic/values/ValueTests.java b/bundles/org.openhab.binding.mqtt.generic/src/test/java/org/openhab/binding/mqtt/generic/values/ValueTests.java index 8a407c3ad4..080cdf3ccb 100644 --- a/bundles/org.openhab.binding.mqtt.generic/src/test/java/org/openhab/binding/mqtt/generic/values/ValueTests.java +++ b/bundles/org.openhab.binding.mqtt.generic/src/test/java/org/openhab/binding/mqtt/generic/values/ValueTests.java @@ -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);