Skip to content

Commit

Permalink
[knx] Adapt to core, temperature differences (openhab#15573)
Browse files Browse the repository at this point in the history
Special handling for temperature differences in °F and °F/%,
DPT 9.002 and 9.003, needs to be adapted due to change in core.

Refs openhab/openhab-core#3792.

Implementation is valid for 4.0 and 4.x snapshot.

Fixes openhab#15567.

Signed-off-by: Holger Friedrich <mail@holger-friedrich.de>
Signed-off-by: querdenker2k <querdenker2k@gmx.de>
  • Loading branch information
holgerfriedrich authored and querdenker2k committed Oct 29, 2023
1 parent 57c2659 commit 2f7cc2c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.openhab.core.library.types.StringType;
import org.openhab.core.library.types.UpDownType;
import org.openhab.core.library.unit.SIUnits;
import org.openhab.core.library.unit.Units;
import org.openhab.core.types.Type;
import org.openhab.core.util.ColorUtil;
import org.slf4j.Logger;
Expand Down Expand Up @@ -192,10 +193,15 @@ private static String handleNumericTypes(String dptId, String mainNumber, DPT dp
unit = unit.replace("K", "°C");
}
} else if (value.toString().contains("°F")) {
if (unit != null) {
unit = unit.replace("K", "°F");
// an new approach to handle temperature differences was introduced to core
// after 4.0, stripping the unit and and creating a new QuantityType works
// both with core release 4.0 and current snapshot
boolean perPercent = value.toString().contains("/%");
value = new QuantityType<>(((QuantityType<?>) value).doubleValue() * 5.0 / 9.0, Units.KELVIN);
// PercentType needs to be adapted
if (perPercent) {
value = ((QuantityType<?>) value).multiply(BigDecimal.valueOf(100));
}
value = ((QuantityType<?>) value).multiply(BigDecimal.valueOf(5.0 / 9.0));
}
} else if (DPTXlator4ByteFloat.DPT_LIGHT_QUANTITY.getID().equals(dptId)) {
if (!value.toString().contains("J")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ void testToDPT9ValueFromQuantityType() {
assertEquals("1", ValueEncoder.encode(new QuantityType<>("1 K"), "9.002"));
assertEquals("1", ValueEncoder.encode(new QuantityType<>("1000 mK"), "9.002"));
assertEquals("1", ValueEncoder.encode(new QuantityType<>("1 °C"), "9.002"));
// TODO
// assertTrue(ValueEncoder.encode(new QuantityType<>("1 °F"), "9.002").startsWith("0.55"));
assertTrue(ValueEncoder.encode(new QuantityType<>("1 °F"), "9.002").startsWith("0.55"));
assertEquals("1", ValueEncoder.encode(new QuantityType<>("1 K/h"), "9.003"));
assertEquals("1", ValueEncoder.encode(new QuantityType<>("1 °C/h"), "9.003"));
assertEquals("1", ValueEncoder.encode(new QuantityType<>("1000 mK/h"), "9.003"));
Expand All @@ -134,8 +133,7 @@ void testToDPT9ValueFromQuantityType() {
assertEquals("12", ValueEncoder.encode(new QuantityType<>("12 W/m²"), "9.022"));
assertEquals("1", ValueEncoder.encode(new QuantityType<>("1 K/%"), "9.023"));
assertEquals("1", ValueEncoder.encode(new QuantityType<>("1 °C/%"), "9.023"));
// TODO
// assertTrue(ValueEncoder.encode(new QuantityType<>("1 °F/%"), "9.023").startsWith("0.55"));
assertTrue(ValueEncoder.encode(new QuantityType<>("1 °F/%"), "9.023").startsWith("0.55"));
assertEquals("1", ValueEncoder.encode(new QuantityType<>("1 kW"), "9.024"));
assertEquals("1", ValueEncoder.encode(new QuantityType<>("1 l/h"), "9.025"));
assertEquals("60", ValueEncoder.encode(new QuantityType<>("1 l/min"), "9.025"));
Expand Down

0 comments on commit 2f7cc2c

Please sign in to comment.