]> git.basschouten.com Git - openhab-addons.git/blob
a67fb8266fb1b734ee97e8a281941ff96a6a6823
[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     public 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         isChargeable = getAccessoryConfigurationAsBoolean(BATTERY_TYPE, false);
51         if (isChargeable) {
52             chargingBatteryReader = createBooleanReader(BATTERY_CHARGING_STATE);
53         }
54         getServices().add(new BatteryService(this));
55     }
56
57     @Override
58     public CompletableFuture<Integer> getBatteryLevel() {
59         final @Nullable DecimalType state = getStateAs(BATTERY_LEVEL, DecimalType.class);
60         return CompletableFuture.completedFuture(state != null ? state.intValue() : 0);
61     }
62
63     @Override
64     public CompletableFuture<StatusLowBatteryEnum> getLowBatteryState() {
65         return CompletableFuture
66                 .completedFuture(lowBatteryReader.getValue() ? StatusLowBatteryEnum.LOW : StatusLowBatteryEnum.NORMAL);
67     }
68
69     @Override
70     public CompletableFuture<ChargingStateEnum> getChargingState() {
71         return CompletableFuture.completedFuture(isChargeable
72                 ? chargingBatteryReader.getValue() ? ChargingStateEnum.CHARGING : ChargingStateEnum.NOT_CHARGING
73                 : ChargingStateEnum.NOT_CHARABLE); // the mapping to NOT_CHARABLE is correct, there is a typo in java
74                                                    // HAP
75     }
76
77     @Override
78     public void subscribeBatteryLevel(final HomekitCharacteristicChangeCallback callback) {
79         subscribe(BATTERY_LEVEL, callback);
80     }
81
82     @Override
83     public void subscribeLowBatteryState(final HomekitCharacteristicChangeCallback callback) {
84         subscribe(BATTERY_LOW_STATUS, callback);
85     }
86
87     @Override
88     public void subscribeBatteryChargingState(final HomekitCharacteristicChangeCallback callback) {
89         if (isChargeable) {
90             subscribe(BATTERY_CHARGING_STATE, callback);
91         }
92     }
93
94     @Override
95     public void unsubscribeBatteryLevel() {
96         unsubscribe(BATTERY_LEVEL);
97     }
98
99     @Override
100     public void unsubscribeLowBatteryState() {
101         unsubscribe(BATTERY_LOW_STATUS);
102     }
103
104     @Override
105     public void unsubscribeBatteryChargingState() {
106         if (isChargeable) {
107             unsubscribe(BATTERY_CHARGING_STATE);
108         }
109     }
110 }