]> git.basschouten.com Git - openhab-addons.git/blob
ea50216986d1e70685ad0926acf249fcf641ddde
[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.homematic.internal.communicator.virtual;
14
15 import static org.openhab.binding.homematic.internal.misc.HomematicConstants.VIRTUAL_DATAPOINT_NAME_BATTERY_TYPE;
16
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.util.Properties;
20
21 import org.openhab.binding.homematic.internal.model.HmDevice;
22 import org.openhab.binding.homematic.internal.model.HmValueType;
23 import org.osgi.framework.Bundle;
24 import org.osgi.framework.FrameworkUtil;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * A virtual String datapoint which adds the battery type to a battery powered device.
30  *
31  * @author Gerhard Riegler - Initial contribution
32  */
33 public class BatteryTypeVirtualDatapointHandler extends AbstractVirtualDatapointHandler {
34     private final Logger logger = LoggerFactory.getLogger(BatteryTypeVirtualDatapointHandler.class);
35
36     private static final Properties batteries = new Properties();
37
38     public BatteryTypeVirtualDatapointHandler() {
39         Bundle bundle = FrameworkUtil.getBundle(getClass());
40         try (InputStream stream = bundle.getResource("homematic/batteries.properties").openStream()) {
41             batteries.load(stream);
42         } catch (IllegalStateException | IOException e) {
43             logger.warn("The resource homematic/batteries.properties could not be loaded! Battery types not available",
44                     e);
45         }
46     }
47
48     @Override
49     public String getName() {
50         return VIRTUAL_DATAPOINT_NAME_BATTERY_TYPE;
51     }
52
53     @Override
54     public void initialize(HmDevice device) {
55         String batteryType = batteries.getProperty(device.getType());
56         if (batteryType != null) {
57             addDatapoint(device, 0, getName(), HmValueType.STRING, batteryType, true);
58         }
59     }
60 }