]> git.basschouten.com Git - openhab-addons.git/blob
316cb2c145e53e33a1b91cfe36e079c2de495b2e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.boschshc.internal.devices;
14
15 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.*;
16
17 import java.util.List;
18
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;
25
26 /**
27  * Abstract implementation for battery-powered devices.
28  *
29  * @author David Pace - Initial contribution
30  *
31  */
32 @NonNullByDefault
33 public abstract class AbstractBatteryPoweredDeviceHandler extends BoschSHCDeviceHandler {
34
35     /**
36      * Service to monitor the battery level of the device
37      */
38     private final BatteryLevelService batteryLevelService;
39
40     public AbstractBatteryPoweredDeviceHandler(Thing thing) {
41         super(thing);
42         this.batteryLevelService = new BatteryLevelService();
43     }
44
45     @Override
46     protected void initializeServices() throws BoschSHCException {
47         super.initializeServices();
48
49         registerService(batteryLevelService, this::updateChannels, List.of(CHANNEL_BATTERY_LEVEL, CHANNEL_LOW_BATTERY),
50                 true);
51     }
52
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());
57     }
58 }