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