[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 <cody@cutrer.us>
This commit is contained in:
Cody Cutrer 2022-10-22 12:07:33 -06:00 committed by GitHub
parent 790e5d58c4
commit 5f6241cbc6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 1 deletions

View File

@ -130,7 +130,7 @@ public class NumberValue extends Value {
private BigDecimal getQuantityTypeAsDecimal(QuantityType<?> qType) { private BigDecimal getQuantityTypeAsDecimal(QuantityType<?> qType) {
BigDecimal val = qType.toBigDecimal(); BigDecimal val = qType.toBigDecimal();
if (!qType.getUnit().isCompatible(Units.ONE)) { if (!qType.getUnit().isCompatible(Units.ONE)) {
QuantityType<?> convertedType = qType.toUnit(unit); QuantityType<?> convertedType = qType.toInvertibleUnit(unit);
if (convertedType != null) { if (convertedType != null) {
val = convertedType.toBigDecimal(); val = convertedType.toBigDecimal();
} }

View File

@ -184,6 +184,14 @@ public class ValueTests {
assertThat(v.getChannelState(), is(new QuantityType<>(20, Units.WATT))); 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 @Test
public void numberPercentageUpdate() { public void numberPercentageUpdate() {
NumberValue v = new NumberValue(null, null, new BigDecimal(10), Units.PERCENT); NumberValue v = new NumberValue(null, null, new BigDecimal(10), Units.PERCENT);