]> git.basschouten.com Git - openhab-addons.git/blob
4d326aa39f8b39e00399358d6b47c58085ddf787
[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.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;
21
22 /**
23  * Electricity device, containing current (Watt) and total (KWh) consumption.
24  *
25  * @author Pepijn de Geus - Initial contribution
26  */
27 public class ElectricityDevice extends AbstractNumericValueDevice {
28
29     private static final String LINK_WATTS = "watt";
30     private static final String LINK_KWH = "kwh";
31
32     public ElectricityDevice(Item item) {
33         super(DeviceType.ELECTRICITY, item, "W");
34     }
35
36     @Override
37     public void addLink(String linkType, String deviceId) {
38         super.addLink(linkType, deviceId);
39
40         if (getLinks().containsKey(LINK_WATTS)) {
41             setUnit("KWh");
42         } else if (getLinks().containsKey(LINK_KWH)) {
43             setUnit("W");
44         }
45     }
46
47     @Override
48     public void stateUpdated(Item item, State newState) {
49         super.stateUpdated(item, newState);
50
51         DecimalType value = item.getStateAs(DecimalType.class);
52
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));
57         }
58     }
59
60     @Override
61     public void updateParams() {
62         super.updateParams();
63
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);
70             } else {
71                 setWattsParam(wattsDevice);
72             }
73         }
74
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);
81             } else {
82                 setKwhParam(kwhDevice);
83             }
84         }
85     }
86
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);
91             return;
92         }
93
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");
98         }
99         wattsParam.setValue(valueParam.getValue());
100         addParam(wattsParam);
101     }
102
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);
107             return;
108         }
109
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");
114         }
115         kwhParam.setValue(valueParam.getValue());
116         addParam(kwhParam);
117     }
118 }