diff --git a/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/accessories/AbstractHomekitAccessoryImpl.java b/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/accessories/AbstractHomekitAccessoryImpl.java index fab3ceab42383..00d1be3791977 100644 --- a/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/accessories/AbstractHomekitAccessoryImpl.java +++ b/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/accessories/AbstractHomekitAccessoryImpl.java @@ -18,12 +18,19 @@ import java.util.Map; import java.util.Optional; import java.util.concurrent.CompletableFuture; +import java.math.BigDecimal; +import java.math.RoundingMode; + +import javax.measure.Quantity; +import javax.measure.Unit; import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.Nullable; import org.eclipse.smarthome.core.items.GenericItem; import org.eclipse.smarthome.core.items.Item; import org.eclipse.smarthome.core.types.State; +import org.eclipse.smarthome.core.library.unit.SIUnits; +import org.eclipse.smarthome.core.library.unit.ImperialUnits; import org.openhab.io.homekit.internal.HomekitAccessoryUpdater; import org.openhab.io.homekit.internal.HomekitCharacteristicType; import org.openhab.io.homekit.internal.HomekitSettings; @@ -177,4 +184,17 @@ protected T getAccessoryConfiguration(String key, @NonNull T defaultValue) { protected void addCharacteristic(HomekitTaggedItem characteristic) { characteristics.add(characteristic); } + + private > double convertAndRound(double value, Unit from, Unit to) { + double rawValue = from == to ? value : from.getConverterTo(to).convert(value); + return new BigDecimal(rawValue).setScale(1, RoundingMode.HALF_UP).doubleValue(); + } + + protected double convertToCelsius(double degrees){ + return convertAndRound(degrees, getSettings().useFahrenheitTemperature ? ImperialUnits.FAHRENHEIT : SIUnits.CELSIUS, SIUnits.CELSIUS); + } + + protected double convertFromCelsius(double degrees){ + return convertAndRound(degrees, getSettings().useFahrenheitTemperature ? SIUnits.CELSIUS : ImperialUnits.FAHRENHEIT, ImperialUnits.FAHRENHEIT); + } } diff --git a/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/accessories/HomekitTemperatureSensorImpl.java b/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/accessories/HomekitTemperatureSensorImpl.java index 95ec95a3cbfe2..31e5777c6c0a0 100644 --- a/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/accessories/HomekitTemperatureSensorImpl.java +++ b/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/accessories/HomekitTemperatureSensorImpl.java @@ -55,20 +55,4 @@ public void subscribeCurrentTemperature(HomekitCharacteristicChangeCallback call public void unsubscribeCurrentTemperature() { unsubscribe(HomekitCharacteristicType.CURRENT_TEMPERATURE); } - - protected double convertToCelsius(double degrees) { - if (getSettings().useFahrenheitTemperature) { - return Math.round((5d / 9d) * (degrees - 32d) * 1000d) / 1000d; - } else { - return degrees; - } - } - - protected double convertFromCelsius(double degrees) { - if (getSettings().useFahrenheitTemperature) { - return Math.round((((9d / 5d) * degrees) + 32d) * 10d) / 10d; - } else { - return degrees; - } - } } diff --git a/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/accessories/HomekitThermostatImpl.java b/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/accessories/HomekitThermostatImpl.java index 086bc0176069b..495bffbdc8f79 100644 --- a/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/accessories/HomekitThermostatImpl.java +++ b/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/accessories/HomekitThermostatImpl.java @@ -236,20 +236,4 @@ public void unsubscribeTargetState() { public void unsubscribeTargetTemperature() { unsubscribe(HomekitCharacteristicType.TARGET_TEMPERATURE); } - - protected double convertToCelsius(double degrees) { - if (getSettings().useFahrenheitTemperature) { - return Math.round((5d / 9d) * (degrees - 32d) * 1000d) / 1000d; - } else { - return degrees; - } - } - - protected double convertFromCelsius(double degrees) { - if (getSettings().useFahrenheitTemperature) { - return Math.round((((9d / 5d) * degrees) + 32d) * 10d) / 10d; - } else { - return degrees; - } - } }