]> git.basschouten.com Git - openhab-addons.git/blob
bf024b57e2b57a7b77801ca6c8d838054b25452d
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.io.homekit.internal.accessories;
14
15 import java.math.BigDecimal;
16 import java.util.List;
17 import java.util.concurrent.CompletableFuture;
18
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;
25
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;
31
32 /**
33  * Implements a HomeKit TemperatureSensor using a NumberItem
34  *
35  * @author Andy Lintner - Initial contribution
36  */
37 class HomekitTemperatureSensorImpl extends AbstractHomekitAccessoryImpl implements TemperatureSensorAccessory {
38
39     public HomekitTemperatureSensorImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
40             HomekitAccessoryUpdater updater, HomekitSettings settings) {
41         super(taggedItem, mandatoryCharacteristics, updater, settings);
42     }
43
44     @Override
45     public void init() throws HomekitException {
46         super.init();
47         getServices().add(new TemperatureSensorService(this));
48     }
49
50     @Override
51     public CompletableFuture<Double> getCurrentTemperature() {
52         final @Nullable Double state = getStateAsTemperature(HomekitCharacteristicType.CURRENT_TEMPERATURE);
53         return CompletableFuture.completedFuture(state != null ? state : getMinCurrentTemperature());
54     }
55
56     @Override
57     public void subscribeCurrentTemperature(HomekitCharacteristicChangeCallback callback) {
58         subscribe(HomekitCharacteristicType.CURRENT_TEMPERATURE, callback);
59     }
60
61     @Override
62     public double getMinCurrentTemperature() {
63         // Apple defines default values in Celsius. We need to convert them to Fahrenheit if openHAB is using Fahrenheit
64         // convertToCelsius and convertFromCelsius are only converting if useFahrenheit is set to true, so no additional
65         // check here needed
66         return HomekitCharacteristicFactory.convertToCelsius(
67                 getAccessoryConfiguration(HomekitCharacteristicType.CURRENT_TEMPERATURE, HomekitTaggedItem.MIN_VALUE,
68                         BigDecimal.valueOf(HomekitCharacteristicFactory
69                                 .convertFromCelsius(CurrentTemperatureCharacteristic.DEFAULT_MIN_VALUE)))
70                         .doubleValue());
71     }
72
73     @Override
74     public double getMaxCurrentTemperature() {
75         return HomekitCharacteristicFactory.convertToCelsius(
76                 getAccessoryConfiguration(HomekitCharacteristicType.CURRENT_TEMPERATURE, HomekitTaggedItem.MAX_VALUE,
77                         BigDecimal.valueOf(HomekitCharacteristicFactory
78                                 .convertFromCelsius(CurrentTemperatureCharacteristic.DEFAULT_MAX_VALUE)))
79                         .doubleValue());
80     }
81
82     @Override
83     public double getMinStepCurrentTemperature() {
84         return HomekitCharacteristicFactory.getTemperatureStep(
85                 getCharacteristic(HomekitCharacteristicType.CURRENT_TEMPERATURE).get(),
86                 TargetTemperatureCharacteristic.DEFAULT_STEP);
87     }
88
89     @Override
90     public void unsubscribeCurrentTemperature() {
91         unsubscribe(HomekitCharacteristicType.CURRENT_TEMPERATURE);
92     }
93 }