2 * Copyright (c) 2010-2023 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.math.BigDecimal;
20 import java.util.List;
21 import java.util.concurrent.CompletableFuture;
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;
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;
37 * Implements battery services for chargeable and non-chargeable batteries
39 * @author Eugen Freiter - Initial contribution
41 public class HomekitBatteryImpl extends AbstractHomekitAccessoryImpl implements BatteryAccessory {
42 public static final String BATTERY_TYPE = "chargeable";
44 private final BooleanItemReader lowBatteryReader;
45 private BooleanItemReader chargingBatteryReader;
46 private final boolean isChargeable;
47 private final BigDecimal lowThreshold;
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);
57 chargingBatteryReader = createBooleanReader(BATTERY_CHARGING_STATE);
59 getServices().add(new BatteryService(this));
63 public CompletableFuture<Integer> getBatteryLevel() {
64 final @Nullable DecimalType state = getStateAs(BATTERY_LEVEL, DecimalType.class);
65 return CompletableFuture.completedFuture(state != null ? state.intValue() : 0);
69 public CompletableFuture<StatusLowBatteryEnum> getLowBatteryState() {
70 return CompletableFuture
71 .completedFuture(lowBatteryReader.getValue() ? StatusLowBatteryEnum.LOW : StatusLowBatteryEnum.NORMAL);
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
83 public void subscribeBatteryLevel(final HomekitCharacteristicChangeCallback callback) {
84 subscribe(BATTERY_LEVEL, callback);
88 public void subscribeLowBatteryState(final HomekitCharacteristicChangeCallback callback) {
89 subscribe(BATTERY_LOW_STATUS, callback);
93 public void subscribeBatteryChargingState(final HomekitCharacteristicChangeCallback callback) {
95 subscribe(BATTERY_CHARGING_STATE, callback);
100 public void unsubscribeBatteryLevel() {
101 unsubscribe(BATTERY_LEVEL);
105 public void unsubscribeLowBatteryState() {
106 unsubscribe(BATTERY_LOW_STATUS);
110 public void unsubscribeBatteryChargingState() {
112 unsubscribe(BATTERY_CHARGING_STATE);