]> git.basschouten.com Git - openhab-addons.git/blob
e522e835adba2786d86ba2c5850fabb4a3914505
[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.io.imperihome.internal.model.device;
14
15 import org.openhab.core.items.Item;
16 import org.openhab.io.imperihome.internal.model.param.NumericValueParam;
17 import org.openhab.io.imperihome.internal.model.param.ParamType;
18 import org.openhab.io.imperihome.internal.processor.ItemProcessor;
19
20 /**
21  * Abstraction of devices that allow a link to a current energy consumption item.
22  *
23  * @author Pepijn de Geus - Initial contribution
24  */
25 public abstract class AbstractEnergyLinkDevice extends AbstractDevice {
26
27     public AbstractEnergyLinkDevice(DeviceType type, Item item) {
28         super(type, item);
29     }
30
31     @Override
32     public void updateParams() {
33         super.updateParams();
34
35         if (getLinks().containsKey("energy")) {
36             String deviceName = getLinks().get("energy");
37             String deviceId = ItemProcessor.getDeviceId(deviceName);
38             AbstractDevice energyDevice = getDeviceRegistry().getDevice(deviceId);
39             if (energyDevice == null) {
40                 logger.error("Couldn't resolve linked energy device '{}', make sure the Item has iss tags", deviceName);
41             } else {
42                 NumericValueParam valueParam = (NumericValueParam) energyDevice.getParams().get(ParamType.WATTS);
43                 if (valueParam == null) {
44                     logger.warn("Linked energy device has no Watts value parameter: {}", energyDevice);
45                 } else {
46                     NumericValueParam energyParam = new NumericValueParam(ParamType.ENERGY, valueParam.getUnit(), null);
47                     energyParam.setValue(valueParam.getValue());
48                     addParam(energyParam);
49                 }
50             }
51         }
52     }
53 }