]> git.basschouten.com Git - openhab-addons.git/blob
fa81361c7d72ba9daf3038c5a39fb2c7ce71abe1
[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  * Combined temperature/hygro sensor device. Can be specified on either a temp or hygro Item, with a link to the other
24  * one.
25  * The linked value will be retrieved in {@link #updateParams()}.
26  *
27  * @author Pepijn de Geus - Initial contribution
28  */
29 public class TempHygroDevice extends AbstractNumericValueDevice {
30
31     private static final String LINK_HYGRO = "hygro";
32     private static final String LINK_TEMP = "temp";
33
34     public TempHygroDevice(Item item) {
35         super(DeviceType.TEMP_HYGRO, item, null);
36     }
37
38     @Override
39     public void addLink(String linkType, String deviceId) {
40         super.addLink(linkType, deviceId);
41
42         if (getLinks().containsKey(LINK_HYGRO)) {
43             setUnit("°C");
44         } else if (getLinks().containsKey(LINK_TEMP)) {
45             setUnit("%");
46         }
47     }
48
49     @Override
50     public void stateUpdated(Item item, State newState) {
51         super.stateUpdated(item, newState);
52
53         DecimalType value = (DecimalType) item.getStateAs(DecimalType.class);
54
55         if (getLinks().containsKey(LINK_HYGRO)) {
56             addParam(new NumericValueParam(ParamType.TEMPERATURE_DUAL, getUnit(), value));
57         } else if (getLinks().containsKey(LINK_TEMP)) {
58             addParam(new NumericValueParam(ParamType.HYGROMETRY_DUAL, getUnit(), value));
59         }
60     }
61
62     @Override
63     public void updateParams() {
64         super.updateParams();
65
66         boolean foundLink = false;
67
68         if (getLinks().containsKey(LINK_HYGRO)) {
69             String deviceName = getLinks().get(LINK_HYGRO);
70             String deviceId = ItemProcessor.getDeviceId(deviceName);
71             AbstractDevice hygroDevice = getDeviceRegistry().getDevice(deviceId);
72             if (hygroDevice == null) {
73                 logger.error("Couldn't resolve linked hygro device '{}', make sure the Item has iss tags", deviceName);
74             } else {
75                 setHygroParam(hygroDevice);
76                 foundLink = true;
77             }
78         }
79
80         if (getLinks().containsKey(LINK_TEMP)) {
81             String deviceName = getLinks().get(LINK_TEMP);
82             String deviceId = ItemProcessor.getDeviceId(deviceName);
83             AbstractDevice tempDevice = getDeviceRegistry().getDevice(deviceId);
84             if (tempDevice == null) {
85                 logger.error("Couldn't resolve linked temp device '{}', make sure the Item has iss tags", deviceName);
86             } else {
87                 setTempParam(tempDevice);
88                 foundLink = true;
89             }
90         }
91
92         if (!foundLink) {
93             logger.warn(
94                     "DevTempHygro device contains no valid 'hygro' or 'temp' link. Add a link to another item using 'iss:link:<type>:<item>'");
95         }
96     }
97
98     private void setHygroParam(AbstractDevice device) {
99         NumericValueParam valueParam = (NumericValueParam) device.getParams().get(ParamType.HYGROMETRY_VALUE);
100         if (valueParam == null) {
101             logger.warn("Linked Hygro device has no Value parameter: {}", device);
102             return;
103         }
104
105         NumericValueParam hygroParam = new NumericValueParam(ParamType.HYGROMETRY_DUAL, valueParam.getUnit(), null);
106         hygroParam.setValue(valueParam.getValue());
107         addParam(hygroParam);
108     }
109
110     private void setTempParam(AbstractDevice device) {
111         NumericValueParam valueParam = (NumericValueParam) device.getParams().get(ParamType.TEMPERATURE_VALUE);
112         if (valueParam == null) {
113             logger.warn("Linked Temperature device has no Value parameter: {}", device);
114             return;
115         }
116
117         NumericValueParam tempParam = new NumericValueParam(ParamType.TEMPERATURE_DUAL, valueParam.getUnit(), null);
118         tempParam.setValue(valueParam.getValue());
119         addParam(tempParam);
120     }
121 }