Skip to content

Commit 422aaad

Browse files
authored
[homekit] use characteristic factory for TemperatureSensor (openhab#17084)
so that it automatically gets the proper unit handling Signed-off-by: Cody Cutrer <[email protected]>
1 parent 2f8e9ad commit 422aaad

File tree

1 file changed

+5
-50
lines changed

1 file changed

+5
-50
lines changed

bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/accessories/HomekitTemperatureSensorImpl.java

+5-50
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,24 @@
1212
*/
1313
package org.openhab.io.homekit.internal.accessories;
1414

15-
import java.math.BigDecimal;
1615
import java.util.List;
17-
import java.util.concurrent.CompletableFuture;
1816

19-
import org.eclipse.jdt.annotation.Nullable;
2017
import org.openhab.io.homekit.internal.HomekitAccessoryUpdater;
2118
import org.openhab.io.homekit.internal.HomekitCharacteristicType;
2219
import org.openhab.io.homekit.internal.HomekitException;
2320
import org.openhab.io.homekit.internal.HomekitSettings;
2421
import org.openhab.io.homekit.internal.HomekitTaggedItem;
2522

26-
import io.github.hapjava.accessories.TemperatureSensorAccessory;
2723
import io.github.hapjava.characteristics.Characteristic;
28-
import io.github.hapjava.characteristics.HomekitCharacteristicChangeCallback;
2924
import io.github.hapjava.characteristics.impl.thermostat.CurrentTemperatureCharacteristic;
30-
import io.github.hapjava.characteristics.impl.thermostat.TargetTemperatureCharacteristic;
3125
import io.github.hapjava.services.impl.TemperatureSensorService;
3226

3327
/**
3428
* Implements a HomeKit TemperatureSensor using a NumberItem
3529
*
3630
* @author Andy Lintner - Initial contribution
3731
*/
38-
class HomekitTemperatureSensorImpl extends AbstractHomekitAccessoryImpl implements TemperatureSensorAccessory {
32+
class HomekitTemperatureSensorImpl extends AbstractHomekitAccessoryImpl {
3933

4034
public HomekitTemperatureSensorImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
4135
List<Characteristic> mandatoryRawCharacteristics, HomekitAccessoryUpdater updater,
@@ -46,50 +40,11 @@ public HomekitTemperatureSensorImpl(HomekitTaggedItem taggedItem, List<HomekitTa
4640
@Override
4741
public void init() throws HomekitException {
4842
super.init();
49-
addService(new TemperatureSensorService(this));
50-
}
51-
52-
@Override
53-
public CompletableFuture<Double> getCurrentTemperature() {
54-
final @Nullable Double state = getStateAsTemperature(HomekitCharacteristicType.CURRENT_TEMPERATURE);
55-
return CompletableFuture.completedFuture(state != null ? state : getMinCurrentTemperature());
56-
}
57-
58-
@Override
59-
public void subscribeCurrentTemperature(HomekitCharacteristicChangeCallback callback) {
60-
subscribe(HomekitCharacteristicType.CURRENT_TEMPERATURE, callback);
61-
}
62-
63-
@Override
64-
public double getMinCurrentTemperature() {
65-
// Apple defines default values in Celsius. We need to convert them to Fahrenheit if openHAB is using Fahrenheit
66-
// convertToCelsius and convertFromCelsius are only converting if useFahrenheit is set to true, so no additional
67-
// check here needed
68-
return HomekitCharacteristicFactory.convertToCelsius(
69-
getAccessoryConfiguration(HomekitCharacteristicType.CURRENT_TEMPERATURE, HomekitTaggedItem.MIN_VALUE,
70-
BigDecimal.valueOf(HomekitCharacteristicFactory
71-
.convertFromCelsius(CurrentTemperatureCharacteristic.DEFAULT_MIN_VALUE)))
72-
.doubleValue());
73-
}
74-
75-
@Override
76-
public double getMaxCurrentTemperature() {
77-
return HomekitCharacteristicFactory.convertToCelsius(
78-
getAccessoryConfiguration(HomekitCharacteristicType.CURRENT_TEMPERATURE, HomekitTaggedItem.MAX_VALUE,
79-
BigDecimal.valueOf(HomekitCharacteristicFactory
80-
.convertFromCelsius(CurrentTemperatureCharacteristic.DEFAULT_MAX_VALUE)))
81-
.doubleValue());
82-
}
8343

84-
@Override
85-
public double getMinStepCurrentTemperature() {
86-
return HomekitCharacteristicFactory.getTemperatureStep(
87-
getCharacteristic(HomekitCharacteristicType.CURRENT_TEMPERATURE).get(),
88-
TargetTemperatureCharacteristic.DEFAULT_STEP);
89-
}
44+
var currentTemperatureCharacteristic = (CurrentTemperatureCharacteristic) HomekitCharacteristicFactory
45+
.createCharacteristic(getCharacteristic(HomekitCharacteristicType.CURRENT_TEMPERATURE).get(),
46+
getUpdater());
9047

91-
@Override
92-
public void unsubscribeCurrentTemperature() {
93-
unsubscribe(HomekitCharacteristicType.CURRENT_TEMPERATURE);
48+
addService(new TemperatureSensorService(currentTemperatureCharacteristic));
9449
}
9550
}

0 commit comments

Comments
 (0)