[knx] Adapt to core, temperature differences (#15573)

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 #15567.

Signed-off-by: Holger Friedrich <mail@holger-friedrich.de>
This commit is contained in:
Holger Friedrich 2023-09-13 20:23:59 +02:00 committed by GitHub
parent 7da3012134
commit d0b161aca1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 7 deletions

View File

@ -33,6 +33,7 @@ import org.openhab.core.library.types.StopMoveType;
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;
@ -192,10 +193,15 @@ public class ValueEncoder {
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")) {

View File

@ -109,8 +109,7 @@ class DPTTest {
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"));
@ -134,8 +133,7 @@ class DPTTest {
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"));