Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[knx] Allow color temperatures specified in mired #18004

Merged
merged 1 commit into from
Jan 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,14 @@ private static String handleNumericTypes(String dptId, String mainNumber, DPT dp
}

if (unit != null) {
QuantityType<?> converted = ((QuantityType<?>) value).toUnit(unit);
QuantityType<?> converted = null;
if ("K".equals(unit) || "°C".equals(unit)) {
// workaround for color temperatures given in MIRED, required as long as toUnit does
// not convert MIRED to Kelvin
converted = ((QuantityType<?>) value).toInvertibleUnit(unit);
} else {
converted = ((QuantityType<?>) value).toUnit(unit);
}
if (converted == null) {
LOGGER.warn("Could not convert {} to unit {}, stripping unit only. Check your configuration.",
value, unit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ void testToDPT7ValueFromQuantityType() {
assertEquals("1000", ValueEncoder.encode(new QuantityType<>("1000 lx"), "7.013"));

assertEquals("3000", ValueEncoder.encode(new QuantityType<>("3000 K"), "7.600"));
// unit for 7.600 is K; special handling for color temperature: make sure °C and mired work as well
assertEquals("3273.15", ValueEncoder.encode(new QuantityType<>("3000 °C"), "7.600"));
assertEquals("4000", ValueEncoder.encode(new QuantityType<>("250 mired"), "7.600"));
assertEquals("4000", ValueEncoder.encode(new QuantityType<>("250 mirek"), "7.600"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not strictly necessary to test it (the aliases are anyway tested in core) .. but for completeness MK⁻¹ is another possible alias.

}

@Test
Expand Down Expand Up @@ -316,7 +320,15 @@ void testToDPT14ValueFromQuantityType() {
assertEquals("1", ValueEncoder.encode(new QuantityType<>("1 Pa"), "14.066"));
assertEquals("1", ValueEncoder.encode(new QuantityType<>("1 N/m"), "14.067"));
assertEquals("1", ValueEncoder.encode(new QuantityType<>("1 °C"), "14.068"));
// unit for 14.068 is °C; special handling for color temperature: make sure °C and mired work as well
assertEquals("-272.15", ValueEncoder.encode(new QuantityType<>("1 K"), "14.068"));
assertEquals("3726.85", ValueEncoder.encode(new QuantityType<>("250 mired"), "14.068"));
assertEquals("3726.85", ValueEncoder.encode(new QuantityType<>("250 mirek"), "14.068"));
assertEquals("1", ValueEncoder.encode(new QuantityType<>("1 K"), "14.069"));
// unit for 14.069 is K; special handling for color temperature: make sure °C and mired work as well
assertEquals("274.15", ValueEncoder.encode(new QuantityType<>("1 °C"), "14.069"));
assertEquals("4000", ValueEncoder.encode(new QuantityType<>("250 mired"), "14.069"));
assertEquals("4000", ValueEncoder.encode(new QuantityType<>("250 mirek"), "14.069"));
assertEquals("1", ValueEncoder.encode(new QuantityType<>("1 K"), "14.070"));
assertEquals("1", ValueEncoder.encode(new QuantityType<>("1 J/K"), "14.071"));
assertEquals("1", ValueEncoder.encode(new QuantityType<>("1 W/m/K"), "14.072"));
Expand Down Expand Up @@ -526,6 +538,12 @@ public void unitFixes() {

// 64-bit signed (DPT 29)
assertNotEquals(DPTXlator64BitSigned.DPT_REACTIVE_ENERGY.getUnit(), Units.VAR_HOUR.toString());

// workaround for color temperatures given in MIRED, required as long as toUnit does
// not convert MIRED to Kelvin
// -> if this test fails, workaround in ValueEncoder can be removed
assertNull((new QuantityType<>("1 mired")).toUnit("K"));
assertNotNull((new QuantityType<>("1 mired")).toInvertibleUnit("K"));
}

private static Stream<Map.Entry<String, String>> unitProvider() {
Expand Down