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 * Combined temperature/hygro sensor device. Can be specified on either a temp or hygro Item, with a link to the other
25 * The linked value will be retrieved in {@link #updateParams()}.
27 * @author Pepijn de Geus - Initial contribution
29 public class TempHygroDevice extends AbstractNumericValueDevice {
31 private static final String LINK_HYGRO = "hygro";
32 private static final String LINK_TEMP = "temp";
34 public TempHygroDevice(Item item) {
35 super(DeviceType.TEMP_HYGRO, item, null);
39 public void addLink(String linkType, String deviceId) {
40 super.addLink(linkType, deviceId);
42 if (getLinks().containsKey(LINK_HYGRO)) {
44 } else if (getLinks().containsKey(LINK_TEMP)) {
50 public void stateUpdated(Item item, State newState) {
51 super.stateUpdated(item, newState);
53 DecimalType value = (DecimalType) item.getStateAs(DecimalType.class);
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));
63 public void updateParams() {
66 boolean foundLink = false;
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);
75 setHygroParam(hygroDevice);
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);
87 setTempParam(tempDevice);
94 "DevTempHygro device contains no valid 'hygro' or 'temp' link. Add a link to another item using 'iss:link:<type>:<item>'");
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);
105 NumericValueParam hygroParam = new NumericValueParam(ParamType.HYGROMETRY_DUAL, valueParam.getUnit(), null);
106 hygroParam.setValue(valueParam.getValue());
107 addParam(hygroParam);
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);
117 NumericValueParam tempParam = new NumericValueParam(ParamType.TEMPERATURE_DUAL, valueParam.getUnit(), null);
118 tempParam.setValue(valueParam.getValue());