2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.io.homekit.internal.accessories;
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;
19 import java.util.List;
20 import java.util.concurrent.CompletableFuture;
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;
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;
35 * Implements battery services for chargeable and non-chargeable batteries
37 * @author Eugen Freiter - Initial contribution
39 public class HomekitBatteryImpl extends AbstractHomekitAccessoryImpl implements BatteryAccessory {
40 public static final String BATTERY_TYPE = "chargeable";
42 private final BooleanItemReader lowBatteryReader;
43 private BooleanItemReader chargingBatteryReader;
44 private final boolean isChargeable;
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);
52 chargingBatteryReader = createBooleanReader(BATTERY_CHARGING_STATE);
54 getServices().add(new BatteryService(this));
58 public CompletableFuture<Integer> getBatteryLevel() {
59 final @Nullable DecimalType state = getStateAs(BATTERY_LEVEL, DecimalType.class);
60 return CompletableFuture.completedFuture(state != null ? state.intValue() : 0);
64 public CompletableFuture<StatusLowBatteryEnum> getLowBatteryState() {
65 return CompletableFuture
66 .completedFuture(lowBatteryReader.getValue() ? StatusLowBatteryEnum.LOW : StatusLowBatteryEnum.NORMAL);
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
78 public void subscribeBatteryLevel(final HomekitCharacteristicChangeCallback callback) {
79 subscribe(BATTERY_LEVEL, callback);
83 public void subscribeLowBatteryState(final HomekitCharacteristicChangeCallback callback) {
84 subscribe(BATTERY_LOW_STATUS, callback);
88 public void subscribeBatteryChargingState(final HomekitCharacteristicChangeCallback callback) {
90 subscribe(BATTERY_CHARGING_STATE, callback);
95 public void unsubscribeBatteryLevel() {
96 unsubscribe(BATTERY_LEVEL);
100 public void unsubscribeLowBatteryState() {
101 unsubscribe(BATTERY_LOW_STATUS);
105 public void unsubscribeBatteryChargingState() {
107 unsubscribe(BATTERY_CHARGING_STATE);