[homekit] use characteristic factory for TemperatureSensor (#17084)

so that it automatically gets the proper unit handling

Signed-off-by: Cody Cutrer <cody@cutrer.us>
Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
Cody Cutrer 2024-07-21 02:29:05 -06:00 committed by Ciprian Pascu
parent 6a9cad8246
commit f5f08a58f1

View File

@ -12,22 +12,16 @@
*/ */
package org.openhab.io.homekit.internal.accessories; package org.openhab.io.homekit.internal.accessories;
import java.math.BigDecimal;
import java.util.List; 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.HomekitAccessoryUpdater;
import org.openhab.io.homekit.internal.HomekitCharacteristicType; import org.openhab.io.homekit.internal.HomekitCharacteristicType;
import org.openhab.io.homekit.internal.HomekitException; import org.openhab.io.homekit.internal.HomekitException;
import org.openhab.io.homekit.internal.HomekitSettings; import org.openhab.io.homekit.internal.HomekitSettings;
import org.openhab.io.homekit.internal.HomekitTaggedItem; import org.openhab.io.homekit.internal.HomekitTaggedItem;
import io.github.hapjava.accessories.TemperatureSensorAccessory;
import io.github.hapjava.characteristics.Characteristic; 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.CurrentTemperatureCharacteristic;
import io.github.hapjava.characteristics.impl.thermostat.TargetTemperatureCharacteristic;
import io.github.hapjava.services.impl.TemperatureSensorService; import io.github.hapjava.services.impl.TemperatureSensorService;
/** /**
@ -35,7 +29,7 @@ import io.github.hapjava.services.impl.TemperatureSensorService;
* *
* @author Andy Lintner - Initial contribution * @author Andy Lintner - Initial contribution
*/ */
class HomekitTemperatureSensorImpl extends AbstractHomekitAccessoryImpl implements TemperatureSensorAccessory { class HomekitTemperatureSensorImpl extends AbstractHomekitAccessoryImpl {
public HomekitTemperatureSensorImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics, public HomekitTemperatureSensorImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
List<Characteristic> mandatoryRawCharacteristics, HomekitAccessoryUpdater updater, List<Characteristic> mandatoryRawCharacteristics, HomekitAccessoryUpdater updater,
@ -46,50 +40,11 @@ class HomekitTemperatureSensorImpl extends AbstractHomekitAccessoryImpl implemen
@Override @Override
public void init() throws HomekitException { public void init() throws HomekitException {
super.init(); super.init();
addService(new TemperatureSensorService(this));
}
@Override var currentTemperatureCharacteristic = (CurrentTemperatureCharacteristic) HomekitCharacteristicFactory
public CompletableFuture<Double> getCurrentTemperature() { .createCharacteristic(getCharacteristic(HomekitCharacteristicType.CURRENT_TEMPERATURE).get(),
final @Nullable Double state = getStateAsTemperature(HomekitCharacteristicType.CURRENT_TEMPERATURE); getUpdater());
return CompletableFuture.completedFuture(state != null ? state : getMinCurrentTemperature());
}
@Override addService(new TemperatureSensorService(currentTemperatureCharacteristic));
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);
}
@Override
public void unsubscribeCurrentTemperature() {
unsubscribe(HomekitCharacteristicType.CURRENT_TEMPERATURE);
} }
} }