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.io.imperihome.internal.model.device;
15 import org.openhab.core.items.Item;
16 import org.openhab.core.library.types.DecimalType;
17 import org.openhab.core.types.State;
18 import org.openhab.io.imperihome.internal.model.param.NumericValueParam;
19 import org.openhab.io.imperihome.internal.model.param.ParamType;
20 import org.openhab.io.imperihome.internal.processor.ItemProcessor;
23 * Electricity device, containing current (Watt) and total (KWh) consumption.
25 * @author Pepijn de Geus - Initial contribution
27 public class ElectricityDevice extends AbstractNumericValueDevice {
29 private static final String LINK_WATTS = "watt";
30 private static final String LINK_KWH = "kwh";
32 public ElectricityDevice(Item item) {
33 super(DeviceType.ELECTRICITY, item, "W");
37 public void addLink(String linkType, String deviceId) {
38 super.addLink(linkType, deviceId);
40 if (getLinks().containsKey(LINK_WATTS)) {
42 } else if (getLinks().containsKey(LINK_KWH)) {
48 public void stateUpdated(Item item, State newState) {
49 super.stateUpdated(item, newState);
51 DecimalType value = item.getStateAs(DecimalType.class);
53 if (getLinks().containsKey(LINK_WATTS) || getUnit().equalsIgnoreCase(LINK_KWH)) {
54 addParam(new NumericValueParam(ParamType.KWH, getUnit(), value));
55 } else if (getLinks().isEmpty() || getLinks().containsKey(LINK_KWH)) {
56 addParam(new NumericValueParam(ParamType.WATTS, getUnit(), value));
61 public void updateParams() {
64 if (getLinks().containsKey(LINK_WATTS)) {
65 String deviceName = getLinks().get(LINK_WATTS);
66 String deviceId = ItemProcessor.getDeviceId(deviceName);
67 AbstractDevice wattsDevice = getDeviceRegistry().getDevice(deviceId);
68 if (wattsDevice == null) {
69 logger.error("Couldn't resolve linked watts device '{}', make sure the Item has iss tags", deviceName);
71 setWattsParam(wattsDevice);
75 if (getLinks().containsKey(LINK_KWH)) {
76 String deviceName = getLinks().get(LINK_KWH);
77 String deviceId = ItemProcessor.getDeviceId(deviceName);
78 AbstractDevice kwhDevice = getDeviceRegistry().getDevice(deviceId);
79 if (kwhDevice == null) {
80 logger.error("Couldn't resolve linked KWh device '{}', make sure the Item has iss tags", deviceName);
82 setKwhParam(kwhDevice);
87 private void setWattsParam(AbstractDevice device) {
88 NumericValueParam valueParam = (NumericValueParam) device.getParams().get(ParamType.WATTS);
89 if (valueParam == null) {
90 logger.warn("Linked Watts device has no Watt value parameter: {}", device);
94 NumericValueParam wattsParam = new NumericValueParam(ParamType.WATTS, valueParam.getUnit(), null);
95 String unit = wattsParam.getUnit();
96 if (unit == null || unit.isEmpty()) {
97 wattsParam.setUnit("W");
99 wattsParam.setValue(valueParam.getValue());
100 addParam(wattsParam);
103 private void setKwhParam(AbstractDevice device) {
104 NumericValueParam valueParam = (NumericValueParam) device.getParams().get(ParamType.KWH);
105 if (valueParam == null) {
106 logger.warn("Linked KWh device has no KWh value parameter: {}", device);
110 NumericValueParam kwhParam = new NumericValueParam(ParamType.KWH, valueParam.getUnit(), null);
111 String unit = kwhParam.getUnit();
112 if (unit == null || unit.isEmpty()) {
113 kwhParam.setUnit("KWh");
115 kwhParam.setValue(valueParam.getValue());