2 * Copyright (c) 2010-2024 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.HomekitException;
28 import org.openhab.io.homekit.internal.HomekitSettings;
29 import org.openhab.io.homekit.internal.HomekitTaggedItem;
31 import io.github.hapjava.accessories.BatteryAccessory;
32 import io.github.hapjava.characteristics.HomekitCharacteristicChangeCallback;
33 import io.github.hapjava.characteristics.impl.battery.ChargingStateEnum;
34 import io.github.hapjava.characteristics.impl.battery.StatusLowBatteryEnum;
35 import io.github.hapjava.services.impl.BatteryService;
38 * Implements battery services for chargeable and non-chargeable batteries
40 * @author Eugen Freiter - Initial contribution
42 public class HomekitBatteryImpl extends AbstractHomekitAccessoryImpl implements BatteryAccessory {
43 public static final String BATTERY_TYPE = "chargeable";
45 private final BooleanItemReader lowBatteryReader;
46 private BooleanItemReader chargingBatteryReader;
47 private final boolean isChargeable;
48 private final BigDecimal lowThreshold;
50 public HomekitBatteryImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
51 HomekitAccessoryUpdater updater, HomekitSettings settings) throws IncompleteAccessoryException {
52 super(taggedItem, mandatoryCharacteristics, updater, settings);
53 lowThreshold = getAccessoryConfiguration(HomekitCharacteristicType.BATTERY_LOW_STATUS,
54 HomekitTaggedItem.BATTERY_LOW_THRESHOLD, BigDecimal.valueOf(20));
55 lowBatteryReader = createBooleanReader(BATTERY_LOW_STATUS, lowThreshold, true);
56 isChargeable = getAccessoryConfigurationAsBoolean(BATTERY_TYPE, false);
58 chargingBatteryReader = createBooleanReader(BATTERY_CHARGING_STATE);
63 public void init() throws HomekitException {
65 addService(new BatteryService(this));
69 public CompletableFuture<Integer> getBatteryLevel() {
70 final @Nullable DecimalType state = getStateAs(BATTERY_LEVEL, DecimalType.class);
71 return CompletableFuture.completedFuture(state != null ? state.intValue() : 0);
75 public CompletableFuture<StatusLowBatteryEnum> getLowBatteryState() {
76 return CompletableFuture
77 .completedFuture(lowBatteryReader.getValue() ? StatusLowBatteryEnum.LOW : StatusLowBatteryEnum.NORMAL);
81 public CompletableFuture<ChargingStateEnum> getChargingState() {
82 return CompletableFuture.completedFuture(isChargeable
83 ? chargingBatteryReader.getValue() ? ChargingStateEnum.CHARGING : ChargingStateEnum.NOT_CHARGING
84 : ChargingStateEnum.NOT_CHARABLE); // the mapping to NOT_CHARABLE is correct, there is a typo in java
89 public void subscribeBatteryLevel(final HomekitCharacteristicChangeCallback callback) {
90 subscribe(BATTERY_LEVEL, callback);
94 public void subscribeLowBatteryState(final HomekitCharacteristicChangeCallback callback) {
95 subscribe(BATTERY_LOW_STATUS, callback);
99 public void subscribeBatteryChargingState(final HomekitCharacteristicChangeCallback callback) {
101 subscribe(BATTERY_CHARGING_STATE, callback);
106 public void unsubscribeBatteryLevel() {
107 unsubscribe(BATTERY_LEVEL);
111 public void unsubscribeLowBatteryState() {
112 unsubscribe(BATTERY_LOW_STATUS);
116 public void unsubscribeBatteryChargingState() {
118 unsubscribe(BATTERY_CHARGING_STATE);