Skip to content

Commit

Permalink
Change the conversion of the DPT251.600 to use RGB and White.
Browse files Browse the repository at this point in the history
With the new feature, the HSBType is converted into RGBW, and we convert
the status signal from the KNX back into an HSB value.

Due to the conversion, some accuracy is lost, but the result is
approximately correct.

Signed-off-by: marco <marco@ms-mueller.ch>
  • Loading branch information
marco committed Oct 17, 2023
1 parent d2b3f68 commit 53bcfc4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -327,13 +327,14 @@ private static Type handleDpt10(String value) throws ParseException {
String bString = rgbw.group("b");
String wString = rgbw.group("w");

if (rString != null && gString != null && bString != null && HSBType.class.equals(preferredType)) {
// does not support PercentType and r,g,b valid -> HSBType
int r = coerceToRange((int) (Double.parseDouble(rString.replace(",", ".")) * 2.55), 0, 255);
int g = coerceToRange((int) (Double.parseDouble(gString.replace(",", ".")) * 2.55), 0, 255);
int b = coerceToRange((int) (Double.parseDouble(bString.replace(",", ".")) * 2.55), 0, 255);

return HSBType.fromRGB(r, g, b);
if (rString != null && gString != null && bString != null && wString != null
&& HSBType.class.equals(preferredType)) {
double r = coerceToRange(Double.parseDouble(rString.replace(",", ".")) * 2.55, 0, 255);
double g = coerceToRange(Double.parseDouble(gString.replace(",", ".")) * 2.55, 0, 255);
double b = coerceToRange(Double.parseDouble(bString.replace(",", ".")) * 2.55, 0, 255);
double w = coerceToRange(Double.parseDouble(wString.replace(",", ".")) * 2.55, 0, 255);

return ColorUtil.rgbwTohsb(r, g, b, w);

Check failure on line 337 in bundles/org.openhab.binding.knx/src/main/java/org/openhab/binding/knx/internal/dpt/ValueDecoder.java

View workflow job for this annotation

GitHub Actions / Build (Java 17, ubuntu-22.04)

The method rgbwTohsb(double, double, double, double) is undefined for the type org.openhab.core.util.ColorUtil
} else if (wString != null && PercentType.class.equals(preferredType)) {
// does support PercentType and w valid -> PercentType
BigDecimal w = new BigDecimal(wString.replace(",", "."));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ private static String handleHSBType(String dptId, HSBType hsb) {
double[] xyY = ColorUtil.hsbToXY(hsb);
return String.format("(%,.4f %,.4f) %,.1f %%", xyY[0], xyY[1], xyY[2] * 100.0);
case "251.600":
PercentType[] rgbw = ColorUtil.hsbToRgbPercent(hsb);
return String.format("%,.1f %,.1f %,.1f - %%", rgbw[0].doubleValue(), rgbw[1].doubleValue(),
rgbw[2].doubleValue());
PercentType[] rgbw = ColorUtil.hsbToRgbwPercent(hsb);

Check failure on line 165 in bundles/org.openhab.binding.knx/src/main/java/org/openhab/binding/knx/internal/dpt/ValueEncoder.java

View workflow job for this annotation

GitHub Actions / Build (Java 17, ubuntu-22.04)

The method hsbToRgbwPercent(org.openhab.core.library.types.HSBType) is undefined for the type org.openhab.core.util.ColorUtil
return String.format("%,.1f %,.1f %,.1f %,.1f %%", rgbw[0].doubleValue(), rgbw[1].doubleValue(),
rgbw[2].doubleValue(), rgbw[3].doubleValue());
case "5.003":
return hsb.getHue().toString();
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,13 +336,13 @@ public void dpt232HsbValue() {
@Test
public void dpt252EncoderTest() {
// input data
byte[] data = new byte[] { 0x26, 0x2b, 0x31, 0x00, 0x00, 0x0e };
byte[] data = new byte[] { 0x26, 0x2b, 0x31, 0x00, 0x00, 0x0f };
HSBType hsbType = (HSBType) ValueDecoder.decode("251.600", data, HSBType.class);

assertNotNull(hsbType);
assertEquals(207, hsbType.getHue().doubleValue(), 0.5);
assertEquals(23, hsbType.getSaturation().doubleValue(), 0.5);
assertEquals(19, hsbType.getBrightness().doubleValue(), 0.5);
assertEquals(210, hsbType.getHue().doubleValue(), 0.5);
assertEquals(22, hsbType.getSaturation().doubleValue(), 0.5);
assertEquals(3.5, hsbType.getBrightness().doubleValue(), 0.5);
}

// This test checks all our overrides for units. It allows to detect unnecessary overrides when we
Expand Down Expand Up @@ -457,7 +457,7 @@ public void dpt242BackToBackTest(byte[] value) {
// for back to back test, compare numerical values to allow tolerances
double dx = (((value[0] & 0xff) << 8) | (value[1] & 0xff)) / 65535.0;
double dy = (((value[2] & 0xff) << 8) | (value[3] & 0xff)) / 65535.0;
double dY = ((double) (value[4] & 0xff)) * 100.0 / 255.0;
double dY = (value[4] & 0xff) * 100.0 / 255.0;

Matcher matcher = ValueDecoder.XYY_PATTERN.matcher(result);
Assertions.assertTrue(matcher.matches());
Expand Down

0 comments on commit 53bcfc4

Please sign in to comment.