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.rfxcom.internal.messages;
15 import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.CHANNEL_BATTERY_LEVEL;
17 import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
18 import org.openhab.binding.rfxcom.internal.exceptions.RFXComInvalidStateException;
19 import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
20 import org.openhab.binding.rfxcom.internal.handler.DeviceState;
21 import org.openhab.core.library.types.DecimalType;
22 import org.openhab.core.types.State;
25 * A base class for all battery device messages
27 * @author Martin van Wingerden - Initial contribution
29 abstract class RFXComBatteryDeviceMessage<T> extends RFXComDeviceMessageImpl<T> {
32 RFXComBatteryDeviceMessage(PacketType packetType) {
36 RFXComBatteryDeviceMessage() {
41 public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
42 throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
44 case CHANNEL_BATTERY_LEVEL:
45 return convertBatteryLevelToSystemWideLevel(batteryLevel);
48 return super.convertToState(channelId, config, deviceState);
53 * Convert internal battery level (0-9) to system wide battery level (0-100%).
55 * @param batteryLevel Internal battery level
56 * @return Battery level in system wide level
58 private State convertBatteryLevelToSystemWideLevel(int batteryLevel) {
59 int ohLevel = (batteryLevel + 1) * 10;
60 return new DecimalType(ohLevel);