]> git.basschouten.com Git - openhab-addons.git/blob
4c9529fd3ce2420e2491d808b59cc044718edc15
[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 static org.openhab.io.homekit.internal.HomekitCharacteristicType.BATTERY_CHARGING_STATE;
16 import static org.openhab.io.homekit.internal.HomekitCharacteristicType.BATTERY_LEVEL;
17 import static org.openhab.io.homekit.internal.HomekitCharacteristicType.BATTERY_LOW_STATUS;
18
19 import java.math.BigDecimal;
20 import java.util.List;
21 import java.util.concurrent.CompletableFuture;
22
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.core.library.types.DecimalType;
25 import org.openhab.io.homekit.internal.HomekitAccessoryUpdater;
26 import org.openhab.io.homekit.internal.HomekitCharacteristicType;
27 import org.openhab.io.homekit.internal.HomekitException;
28 import org.openhab.io.homekit.internal.HomekitSettings;
29 import org.openhab.io.homekit.internal.HomekitTaggedItem;
30
31 import io.github.hapjava.accessories.BatteryAccessory;
32 import io.github.hapjava.characteristics.HomekitCharacteristicChangeCallback;
33 import io.github.hapjava.characteristics.impl.battery.ChargingStateEnum;
34 import io.github.hapjava.characteristics.impl.battery.StatusLowBatteryEnum;
35 import io.github.hapjava.services.impl.BatteryService;
36
37 /**
38  * Implements battery services for chargeable and non-chargeable batteries
39  *
40  * @author Eugen Freiter - Initial contribution
41  */
42 public class HomekitBatteryImpl extends AbstractHomekitAccessoryImpl implements BatteryAccessory {
43     public static final String BATTERY_TYPE = "chargeable";
44
45     private final BooleanItemReader lowBatteryReader;
46     private BooleanItemReader chargingBatteryReader;
47     private final boolean isChargeable;
48     private final BigDecimal lowThreshold;
49
50     public HomekitBatteryImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
51             HomekitAccessoryUpdater updater, HomekitSettings settings) throws IncompleteAccessoryException {
52         super(taggedItem, mandatoryCharacteristics, updater, settings);
53         lowThreshold = getAccessoryConfiguration(HomekitCharacteristicType.BATTERY_LOW_STATUS,
54                 HomekitTaggedItem.BATTERY_LOW_THRESHOLD, BigDecimal.valueOf(20));
55         lowBatteryReader = createBooleanReader(BATTERY_LOW_STATUS, lowThreshold, true);
56         isChargeable = getAccessoryConfigurationAsBoolean(BATTERY_TYPE, false);
57         if (isChargeable) {
58             chargingBatteryReader = createBooleanReader(BATTERY_CHARGING_STATE);
59         }
60     }
61
62     @Override
63     public void init() throws HomekitException {
64         super.init();
65         getServices().add(new BatteryService(this));
66     }
67
68     @Override
69     public CompletableFuture<Integer> getBatteryLevel() {
70         final @Nullable DecimalType state = getStateAs(BATTERY_LEVEL, DecimalType.class);
71         return CompletableFuture.completedFuture(state != null ? state.intValue() : 0);
72     }
73
74     @Override
75     public CompletableFuture<StatusLowBatteryEnum> getLowBatteryState() {
76         return CompletableFuture
77                 .completedFuture(lowBatteryReader.getValue() ? StatusLowBatteryEnum.LOW : StatusLowBatteryEnum.NORMAL);
78     }
79
80     @Override
81     public CompletableFuture<ChargingStateEnum> getChargingState() {
82         return CompletableFuture.completedFuture(isChargeable
83                 ? chargingBatteryReader.getValue() ? ChargingStateEnum.CHARGING : ChargingStateEnum.NOT_CHARGING
84                 : ChargingStateEnum.NOT_CHARABLE); // the mapping to NOT_CHARABLE is correct, there is a typo in java
85                                                    // HAP
86     }
87
88     @Override
89     public void subscribeBatteryLevel(final HomekitCharacteristicChangeCallback callback) {
90         subscribe(BATTERY_LEVEL, callback);
91     }
92
93     @Override
94     public void subscribeLowBatteryState(final HomekitCharacteristicChangeCallback callback) {
95         subscribe(BATTERY_LOW_STATUS, callback);
96     }
97
98     @Override
99     public void subscribeBatteryChargingState(final HomekitCharacteristicChangeCallback callback) {
100         if (isChargeable) {
101             subscribe(BATTERY_CHARGING_STATE, callback);
102         }
103     }
104
105     @Override
106     public void unsubscribeBatteryLevel() {
107         unsubscribe(BATTERY_LEVEL);
108     }
109
110     @Override
111     public void unsubscribeLowBatteryState() {
112         unsubscribe(BATTERY_LOW_STATUS);
113     }
114
115     @Override
116     public void unsubscribeBatteryChargingState() {
117         if (isChargeable) {
118             unsubscribe(BATTERY_CHARGING_STATE);
119         }
120     }
121 }