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