2 * Copyright (c) 2010-2020 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.apache.commons.lang.StringUtils;
16 import org.openhab.core.items.Item;
17 import org.openhab.core.library.types.DecimalType;
18 import org.openhab.core.types.State;
19 import org.openhab.io.imperihome.internal.model.param.NumericValueParam;
20 import org.openhab.io.imperihome.internal.model.param.ParamType;
21 import org.openhab.io.imperihome.internal.processor.ItemProcessor;
24 * Electricity device, containing current (Watt) and total (KWh) consumption.
26 * @author Pepijn de Geus - Initial contribution
28 public class ElectricityDevice extends AbstractNumericValueDevice {
30 private static final String LINK_WATTS = "watt";
31 private static final String LINK_KWH = "kwh";
33 public ElectricityDevice(Item item) {
34 super(DeviceType.ELECTRICITY, item, "W");
38 public void addLink(String linkType, String deviceId) {
39 super.addLink(linkType, deviceId);
41 if (getLinks().containsKey(LINK_WATTS)) {
43 } else if (getLinks().containsKey(LINK_KWH)) {
49 public void stateUpdated(Item item, State newState) {
50 super.stateUpdated(item, newState);
52 DecimalType value = (DecimalType) item.getStateAs(DecimalType.class);
54 if (getLinks().containsKey(LINK_WATTS) || getUnit().equalsIgnoreCase(LINK_KWH)) {
55 addParam(new NumericValueParam(ParamType.KWH, getUnit(), value));
56 } else if (getLinks().isEmpty() || getLinks().containsKey(LINK_KWH)) {
57 addParam(new NumericValueParam(ParamType.WATTS, getUnit(), value));
62 public void updateParams() {
65 if (getLinks().containsKey(LINK_WATTS)) {
66 String deviceName = getLinks().get(LINK_WATTS);
67 String deviceId = ItemProcessor.getDeviceId(deviceName);
68 AbstractDevice wattsDevice = getDeviceRegistry().getDevice(deviceId);
69 if (wattsDevice == null) {
70 logger.error("Couldn't resolve linked watts device '{}', make sure the Item has iss tags", deviceName);
72 setWattsParam(wattsDevice);
76 if (getLinks().containsKey(LINK_KWH)) {
77 String deviceName = getLinks().get(LINK_KWH);
78 String deviceId = ItemProcessor.getDeviceId(deviceName);
79 AbstractDevice kwhDevice = getDeviceRegistry().getDevice(deviceId);
80 if (kwhDevice == null) {
81 logger.error("Couldn't resolve linked KWh device '{}', make sure the Item has iss tags", deviceName);
83 setKwhParam(kwhDevice);
88 private void setWattsParam(AbstractDevice device) {
89 NumericValueParam valueParam = (NumericValueParam) device.getParams().get(ParamType.WATTS);
90 if (valueParam == null) {
91 logger.warn("Linked Watts device has no Watt value parameter: {}", device);
95 NumericValueParam wattsParam = new NumericValueParam(ParamType.WATTS, valueParam.getUnit(), null);
96 if (StringUtils.isEmpty(wattsParam.getUnit())) {
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 if (StringUtils.isEmpty(kwhParam.getUnit())) {
112 kwhParam.setUnit("KWh");
114 kwhParam.setValue(valueParam.getValue());