2 * Copyright (c) 2010-2022 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.core.library.types.DecimalType;
21 import org.openhab.io.homekit.internal.HomekitAccessoryUpdater;
22 import org.openhab.io.homekit.internal.HomekitCharacteristicType;
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.HomekitCharacteristicChangeCallback;
28 import io.github.hapjava.characteristics.impl.thermostat.CurrentTemperatureCharacteristic;
29 import io.github.hapjava.characteristics.impl.thermostat.TargetTemperatureCharacteristic;
30 import io.github.hapjava.services.impl.TemperatureSensorService;
33 * Implements a HomeKit TemperatureSensor using a NumberItem
35 * @author Andy Lintner - Initial contribution
37 class HomekitTemperatureSensorImpl extends AbstractHomekitAccessoryImpl implements TemperatureSensorAccessory {
39 public HomekitTemperatureSensorImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
40 HomekitAccessoryUpdater updater, HomekitSettings settings) {
41 super(taggedItem, mandatoryCharacteristics, updater, settings);
42 getServices().add(new TemperatureSensorService(this));
46 public CompletableFuture<Double> getCurrentTemperature() {
47 final @Nullable DecimalType state = getStateAs(HomekitCharacteristicType.CURRENT_TEMPERATURE,
49 return CompletableFuture
50 .completedFuture(state != null ? HomekitCharacteristicFactory.convertToCelsius(state.doubleValue())
51 : getMinCurrentTemperature());
55 public void subscribeCurrentTemperature(HomekitCharacteristicChangeCallback callback) {
56 subscribe(HomekitCharacteristicType.CURRENT_TEMPERATURE, callback);
60 public double getMinCurrentTemperature() {
61 // Apple defines default values in Celsius. We need to convert them to Fahrenheit if openHAB is using Fahrenheit
62 // convertToCelsius and convertFromCelsius are only converting if useFahrenheit is set to true, so no additional
64 return HomekitCharacteristicFactory.convertToCelsius(
65 getAccessoryConfiguration(HomekitCharacteristicType.CURRENT_TEMPERATURE, HomekitTaggedItem.MIN_VALUE,
66 BigDecimal.valueOf(HomekitCharacteristicFactory
67 .convertFromCelsius(CurrentTemperatureCharacteristic.DEFAULT_MIN_VALUE)))
72 public double getMaxCurrentTemperature() {
73 return HomekitCharacteristicFactory.convertToCelsius(
74 getAccessoryConfiguration(HomekitCharacteristicType.CURRENT_TEMPERATURE, HomekitTaggedItem.MAX_VALUE,
75 BigDecimal.valueOf(HomekitCharacteristicFactory
76 .convertFromCelsius(CurrentTemperatureCharacteristic.DEFAULT_MAX_VALUE)))
81 public double getMinStepCurrentTemperature() {
82 return getAccessoryConfiguration(HomekitCharacteristicType.CURRENT_TEMPERATURE, HomekitTaggedItem.STEP,
83 BigDecimal.valueOf(TargetTemperatureCharacteristic.DEFAULT_STEP)).doubleValue();
87 public void unsubscribeCurrentTemperature() {
88 unsubscribe(HomekitCharacteristicType.CURRENT_TEMPERATURE);