Skip to content

Commit

Permalink
[homekit] Use BigDecimal to ensure accurate rounding (openhab#7777)
Browse files Browse the repository at this point in the history
Temperature values are often not reported correctly in HomeKit
especially when they are in Fahrenheit this also has the side effect of
having no decimal when using Fahrenheit temperatures.

This intends to fix this and allows for accurate rounding and reporting
of temperatures.

Signed-off-by: Ethan Dye <[email protected]>
Signed-off-by: Daan Meijer <[email protected]>
  • Loading branch information
ecdye authored and DaanMeijer committed Sep 1, 2020
1 parent 10697f2 commit c1bfc02
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -177,4 +184,17 @@ protected <T> T getAccessoryConfiguration(String key, @NonNull T defaultValue) {
protected void addCharacteristic(HomekitTaggedItem characteristic) {
characteristics.add(characteristic);
}

private <T extends Quantity<T>> double convertAndRound(double value, Unit<T> from, Unit<T> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

0 comments on commit c1bfc02

Please sign in to comment.