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.homematic.internal.communicator.virtual;
15 import static org.openhab.binding.homematic.internal.misc.HomematicConstants.VIRTUAL_DATAPOINT_NAME_BATTERY_TYPE;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.util.Properties;
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;
29 * A virtual String datapoint which adds the battery type to a battery powered device.
31 * @author Gerhard Riegler - Initial contribution
33 public class BatteryTypeVirtualDatapointHandler extends AbstractVirtualDatapointHandler {
34 private final Logger logger = LoggerFactory.getLogger(BatteryTypeVirtualDatapointHandler.class);
36 private static final Properties batteries = new Properties();
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",
49 public String getName() {
50 return VIRTUAL_DATAPOINT_NAME_BATTERY_TYPE;
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);