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.devices;
15 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.*;
17 import java.util.List;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.boschshc.internal.devices.bridge.dto.DeviceServiceData;
21 import org.openhab.binding.boschshc.internal.exceptions.BoschSHCException;
22 import org.openhab.binding.boschshc.internal.services.batterylevel.BatteryLevel;
23 import org.openhab.binding.boschshc.internal.services.batterylevel.BatteryLevelService;
24 import org.openhab.core.thing.Thing;
27 * Abstract implementation for battery-powered devices.
29 * @author David Pace - Initial contribution
33 public abstract class AbstractBatteryPoweredDeviceHandler extends BoschSHCDeviceHandler {
36 * Service to monitor the battery level of the device
38 private final BatteryLevelService batteryLevelService;
40 public AbstractBatteryPoweredDeviceHandler(Thing thing) {
42 this.batteryLevelService = new BatteryLevelService();
46 protected void initializeServices() throws BoschSHCException {
47 super.initializeServices();
49 registerService(batteryLevelService, this::updateChannels, List.of(CHANNEL_BATTERY_LEVEL, CHANNEL_LOW_BATTERY),
53 private void updateChannels(DeviceServiceData deviceServiceData) {
54 BatteryLevel batteryLevel = BatteryLevel.fromDeviceServiceData(deviceServiceData);
55 super.updateState(CHANNEL_BATTERY_LEVEL, batteryLevel.toState());
56 super.updateState(CHANNEL_LOW_BATTERY, batteryLevel.toLowBatteryState());