]> git.basschouten.com Git - openhab-addons.git/blob
c5f0bb7ac2464bfa36ca9711b4ed4133e2674142
[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.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;
32
33 /**
34  * Implements a HomeKit TemperatureSensor using a NumberItem
35  *
36  * @author Andy Lintner - Initial contribution
37  */
38 class HomekitTemperatureSensorImpl extends AbstractHomekitAccessoryImpl implements TemperatureSensorAccessory {
39
40     public HomekitTemperatureSensorImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
41             List<Characteristic> mandatoryRawCharacteristics, HomekitAccessoryUpdater updater,
42             HomekitSettings settings) {
43         super(taggedItem, mandatoryCharacteristics, mandatoryRawCharacteristics, updater, settings);
44     }
45
46     @Override
47     public void init() throws HomekitException {
48         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     }
83
84     @Override
85     public double getMinStepCurrentTemperature() {
86         return HomekitCharacteristicFactory.getTemperatureStep(
87                 getCharacteristic(HomekitCharacteristicType.CURRENT_TEMPERATURE).get(),
88                 TargetTemperatureCharacteristic.DEFAULT_STEP);
89     }
90
91     @Override
92     public void unsubscribeCurrentTemperature() {
93         unsubscribe(HomekitCharacteristicType.CURRENT_TEMPERATURE);
94     }
95 }