2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.io.homekit.internal.accessories;
15 import java.math.BigDecimal;
16 import java.util.List;
17 import java.util.concurrent.CompletableFuture;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.io.homekit.internal.HomekitAccessoryUpdater;
21 import org.openhab.io.homekit.internal.HomekitCharacteristicType;
22 import org.openhab.io.homekit.internal.HomekitException;
23 import org.openhab.io.homekit.internal.HomekitSettings;
24 import org.openhab.io.homekit.internal.HomekitTaggedItem;
26 import io.github.hapjava.accessories.TemperatureSensorAccessory;
27 import io.github.hapjava.characteristics.Characteristic;
28 import io.github.hapjava.characteristics.HomekitCharacteristicChangeCallback;
29 import io.github.hapjava.characteristics.impl.thermostat.CurrentTemperatureCharacteristic;
30 import io.github.hapjava.characteristics.impl.thermostat.TargetTemperatureCharacteristic;
31 import io.github.hapjava.services.impl.TemperatureSensorService;
34 * Implements a HomeKit TemperatureSensor using a NumberItem
36 * @author Andy Lintner - Initial contribution
38 class HomekitTemperatureSensorImpl extends AbstractHomekitAccessoryImpl implements TemperatureSensorAccessory {
40 public HomekitTemperatureSensorImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
41 List<Characteristic> mandatoryRawCharacteristics, HomekitAccessoryUpdater updater,
42 HomekitSettings settings) {
43 super(taggedItem, mandatoryCharacteristics, mandatoryRawCharacteristics, updater, settings);
47 public void init() throws HomekitException {
49 addService(new TemperatureSensorService(this));
53 public CompletableFuture<Double> getCurrentTemperature() {
54 final @Nullable Double state = getStateAsTemperature(HomekitCharacteristicType.CURRENT_TEMPERATURE);
55 return CompletableFuture.completedFuture(state != null ? state : getMinCurrentTemperature());
59 public void subscribeCurrentTemperature(HomekitCharacteristicChangeCallback callback) {
60 subscribe(HomekitCharacteristicType.CURRENT_TEMPERATURE, callback);
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
68 return HomekitCharacteristicFactory.convertToCelsius(
69 getAccessoryConfiguration(HomekitCharacteristicType.CURRENT_TEMPERATURE, HomekitTaggedItem.MIN_VALUE,
70 BigDecimal.valueOf(HomekitCharacteristicFactory
71 .convertFromCelsius(CurrentTemperatureCharacteristic.DEFAULT_MIN_VALUE)))
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)))
85 public double getMinStepCurrentTemperature() {
86 return HomekitCharacteristicFactory.getTemperatureStep(
87 getCharacteristic(HomekitCharacteristicType.CURRENT_TEMPERATURE).get(),
88 TargetTemperatureCharacteristic.DEFAULT_STEP);
92 public void unsubscribeCurrentTemperature() {
93 unsubscribe(HomekitCharacteristicType.CURRENT_TEMPERATURE);