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.binding.boschshc.internal.services.batterylevel;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.boschshc.internal.devices.bridge.dto.DeviceServiceData;
18 import org.openhab.binding.boschshc.internal.devices.bridge.dto.Fault;
19 import org.openhab.binding.boschshc.internal.devices.bridge.dto.Faults;
20 import org.openhab.core.library.types.DecimalType;
21 import org.openhab.core.library.types.OnOffType;
22 import org.openhab.core.types.State;
23 import org.openhab.core.types.UnDefType;
26 * Possible battery levels.
28 * @author David Pace - Initial contribution
32 public enum BatteryLevel {
36 CRITICALLY_LOW_BATTERY,
40 * Derives a battery level by analyzing the fault elements in the given device service data.
42 * Note that no fault elements are present when the battery level is OK.
44 * @param deviceServiceData a device service data model
45 * @return the derived battery level
47 public static BatteryLevel fromDeviceServiceData(DeviceServiceData deviceServiceData) {
48 Faults faults = deviceServiceData.faults;
49 if (faults == null || faults.entries == null || faults.entries.isEmpty()) {
53 for (Fault faultEntry : faults.entries) {
54 if ("warning".equalsIgnoreCase(faultEntry.category)) {
55 BatteryLevel batteryLevelState = BatteryLevel.get(faultEntry.type);
56 if (batteryLevelState != null) {
57 return batteryLevelState;
66 * Returns the corresponding battery level for the given string or <code>null</code> if no state matches.
68 * @param identifier the battery level identifier
70 * @return the matching battery level or <code>null</code>
72 public static @Nullable BatteryLevel get(String identifier) {
73 for (BatteryLevel batteryLevelState : values()) {
74 if (batteryLevelState.toString().equalsIgnoreCase(identifier)) {
75 return batteryLevelState;
83 * Transforms a Bosch-specific battery level to a percentage for the <code>system.battery-level</code> channel.
85 * @return a percentage between 0 and 100 as integer
87 public State toState() {
90 return new DecimalType(10);
92 case CRITICALLY_LOW_BATTERY:
93 return new DecimalType(1);
95 return UnDefType.UNDEF;
97 return new DecimalType(100);
102 * Transforms a Bosch-specific battery level to an <code>ON</code>/<code>OFF</code> state for the
103 * <code>system.low-battery</code> channel.
105 * If the result is <code>ON</code>, the battery is low; if the result is <code>OFF</code> the battery level is OK.
109 public OnOffType toLowBatteryState() {
113 case CRITICALLY_LOW_BATTERY:
116 return OnOffType.OFF;