]> git.basschouten.com Git - openhab-addons.git/blob
c20574247abacb59883793964cd7e4ea0b02aca3
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.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;
25
26 import io.github.hapjava.accessories.TemperatureSensorAccessory;
27 import io.github.hapjava.characteristics.HomekitCharacteristicChangeCallback;
28 import io.github.hapjava.characteristics.impl.thermostat.TargetTemperatureCharacteristic;
29 import io.github.hapjava.services.impl.TemperatureSensorService;
30
31 /**
32  * Implements a HomeKit TemperatureSensor using a NumberItem
33  *
34  * @author Andy Lintner - Initial contribution
35  */
36 class HomekitTemperatureSensorImpl extends AbstractHomekitAccessoryImpl implements TemperatureSensorAccessory {
37
38     public HomekitTemperatureSensorImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
39             HomekitAccessoryUpdater updater, HomekitSettings settings) {
40         super(taggedItem, mandatoryCharacteristics, updater, settings);
41         getServices().add(new TemperatureSensorService(this));
42     }
43
44     @Override
45     public CompletableFuture<Double> getCurrentTemperature() {
46         final @Nullable DecimalType state = getStateAs(HomekitCharacteristicType.CURRENT_TEMPERATURE,
47                 DecimalType.class);
48         return CompletableFuture
49                 .completedFuture(state != null ? convertToCelsius(state.doubleValue()) : getMinCurrentTemperature());
50     }
51
52     @Override
53     public void subscribeCurrentTemperature(HomekitCharacteristicChangeCallback callback) {
54         subscribe(HomekitCharacteristicType.CURRENT_TEMPERATURE, callback);
55     }
56
57     @Override
58     public double getMinCurrentTemperature() {
59         return convertToCelsius(
60                 getAccessoryConfiguration(HomekitCharacteristicType.CURRENT_TEMPERATURE, HomekitTaggedItem.MIN_VALUE,
61                         BigDecimal.valueOf(TargetTemperatureCharacteristic.DEFAULT_MIN_VALUE)).doubleValue());
62     }
63
64     @Override
65     public double getMaxCurrentTemperature() {
66         return convertToCelsius(
67                 getAccessoryConfiguration(HomekitCharacteristicType.CURRENT_TEMPERATURE, HomekitTaggedItem.MAX_VALUE,
68                         BigDecimal.valueOf(TargetTemperatureCharacteristic.DEFAULT_MAX_VALUE)).doubleValue());
69     }
70
71     @Override
72     public double getMinStepCurrentTemperature() {
73         return getAccessoryConfiguration(HomekitCharacteristicType.CURRENT_TEMPERATURE, HomekitTaggedItem.STEP,
74                 BigDecimal.valueOf(TargetTemperatureCharacteristic.DEFAULT_STEP)).doubleValue();
75     }
76
77     @Override
78     public void unsubscribeCurrentTemperature() {
79         unsubscribe(HomekitCharacteristicType.CURRENT_TEMPERATURE);
80     }
81 }