From 2eeff2343a47f82304be38abad71c1e659f9e57b Mon Sep 17 00:00:00 2001 From: Cody Cutrer Date: Thu, 20 Oct 2022 12:53:32 -0600 Subject: [PATCH] [mqtt] support mired<->Kelvin unit conversion Just have to use the newer toInvertibleUnit method that allows the dimension to change. Signed-off-by: Cody Cutrer --- .../openhab/binding/mqtt/generic/values/NumberValue.java | 2 +- .../openhab/binding/mqtt/generic/values/ValueTests.java | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) 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 26e352dce0652..5b7668bc6ad04 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 @@ private BigDecimal getOldValue() { 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 8a407c3ad476a..080cdf3ccb6de 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 void numberUpdate() { 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);