From 422aaad5f74c58a8d60a977dc41b29b87da90201 Mon Sep 17 00:00:00 2001 From: Cody Cutrer Date: Sun, 21 Jul 2024 02:29:05 -0600 Subject: [PATCH] [homekit] use characteristic factory for TemperatureSensor (#17084) so that it automatically gets the proper unit handling Signed-off-by: Cody Cutrer --- .../HomekitTemperatureSensorImpl.java | 55 ++----------------- 1 file changed, 5 insertions(+), 50 deletions(-) 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 c5f0bb7ac2..dcb8460210 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 @@ -12,22 +12,16 @@ */ package org.openhab.io.homekit.internal.accessories; -import java.math.BigDecimal; import java.util.List; -import java.util.concurrent.CompletableFuture; -import org.eclipse.jdt.annotation.Nullable; import org.openhab.io.homekit.internal.HomekitAccessoryUpdater; import org.openhab.io.homekit.internal.HomekitCharacteristicType; import org.openhab.io.homekit.internal.HomekitException; import org.openhab.io.homekit.internal.HomekitSettings; import org.openhab.io.homekit.internal.HomekitTaggedItem; -import io.github.hapjava.accessories.TemperatureSensorAccessory; import io.github.hapjava.characteristics.Characteristic; -import io.github.hapjava.characteristics.HomekitCharacteristicChangeCallback; import io.github.hapjava.characteristics.impl.thermostat.CurrentTemperatureCharacteristic; -import io.github.hapjava.characteristics.impl.thermostat.TargetTemperatureCharacteristic; import io.github.hapjava.services.impl.TemperatureSensorService; /** @@ -35,7 +29,7 @@ import io.github.hapjava.services.impl.TemperatureSensorService; * * @author Andy Lintner - Initial contribution */ -class HomekitTemperatureSensorImpl extends AbstractHomekitAccessoryImpl implements TemperatureSensorAccessory { +class HomekitTemperatureSensorImpl extends AbstractHomekitAccessoryImpl { public HomekitTemperatureSensorImpl(HomekitTaggedItem taggedItem, List mandatoryCharacteristics, List mandatoryRawCharacteristics, HomekitAccessoryUpdater updater, @@ -46,50 +40,11 @@ class HomekitTemperatureSensorImpl extends AbstractHomekitAccessoryImpl implemen @Override public void init() throws HomekitException { super.init(); - addService(new TemperatureSensorService(this)); - } - - @Override - public CompletableFuture getCurrentTemperature() { - final @Nullable Double state = getStateAsTemperature(HomekitCharacteristicType.CURRENT_TEMPERATURE); - return CompletableFuture.completedFuture(state != null ? state : getMinCurrentTemperature()); - } - - @Override - public void subscribeCurrentTemperature(HomekitCharacteristicChangeCallback callback) { - subscribe(HomekitCharacteristicType.CURRENT_TEMPERATURE, callback); - } - - @Override - public double getMinCurrentTemperature() { - // Apple defines default values in Celsius. We need to convert them to Fahrenheit if openHAB is using Fahrenheit - // convertToCelsius and convertFromCelsius are only converting if useFahrenheit is set to true, so no additional - // check here needed - return HomekitCharacteristicFactory.convertToCelsius( - getAccessoryConfiguration(HomekitCharacteristicType.CURRENT_TEMPERATURE, HomekitTaggedItem.MIN_VALUE, - BigDecimal.valueOf(HomekitCharacteristicFactory - .convertFromCelsius(CurrentTemperatureCharacteristic.DEFAULT_MIN_VALUE))) - .doubleValue()); - } - - @Override - public double getMaxCurrentTemperature() { - return HomekitCharacteristicFactory.convertToCelsius( - getAccessoryConfiguration(HomekitCharacteristicType.CURRENT_TEMPERATURE, HomekitTaggedItem.MAX_VALUE, - BigDecimal.valueOf(HomekitCharacteristicFactory - .convertFromCelsius(CurrentTemperatureCharacteristic.DEFAULT_MAX_VALUE))) - .doubleValue()); - } - @Override - public double getMinStepCurrentTemperature() { - return HomekitCharacteristicFactory.getTemperatureStep( - getCharacteristic(HomekitCharacteristicType.CURRENT_TEMPERATURE).get(), - TargetTemperatureCharacteristic.DEFAULT_STEP); - } + var currentTemperatureCharacteristic = (CurrentTemperatureCharacteristic) HomekitCharacteristicFactory + .createCharacteristic(getCharacteristic(HomekitCharacteristicType.CURRENT_TEMPERATURE).get(), + getUpdater()); - @Override - public void unsubscribeCurrentTemperature() { - unsubscribe(HomekitCharacteristicType.CURRENT_TEMPERATURE); + addService(new TemperatureSensorService(currentTemperatureCharacteristic)); } } -- 2.47.3