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 java.util.List;
18 import org.openhab.core.items.Item;
19 import org.openhab.core.library.types.DecimalType;
20 import org.openhab.core.types.State;
21 import org.openhab.io.imperihome.internal.model.param.DeviceParam;
22 import org.openhab.io.imperihome.internal.model.param.NumericValueParam;
23 import org.openhab.io.imperihome.internal.model.param.ParamType;
24 import org.openhab.io.imperihome.internal.processor.ItemProcessor;
25 import org.openhab.io.imperihome.internal.processor.TagType;
30 * @author Pepijn de Geus - Initial contribution
32 public class ThermostatDevice extends AbstractDevice {
34 public ThermostatDevice(Item item) {
35 super(DeviceType.THERMOSTAT, item);
39 public void processCustomTags(Map<TagType, List<String>> issTags) {
40 if (issTags.containsKey(TagType.STEP)) {
41 setStep(issTags.get(TagType.STEP).get(0));
43 if (issTags.containsKey(TagType.MIN_VAL)) {
44 setMinValue(issTags.get(TagType.MIN_VAL).get(0));
46 if (issTags.containsKey(TagType.MAX_VAL)) {
47 setMaxValue(issTags.get(TagType.MAX_VAL).get(0));
49 if (issTags.containsKey(TagType.MODES)) {
50 setAvailableModes(issTags.get(TagType.MODES).get(0));
55 public void stateUpdated(Item item, State newState) {
56 DecimalType state = (DecimalType) item.getStateAs(DecimalType.class);
58 DeviceParam param = new DeviceParam(ParamType.CUR_SETPOINT, state.floatValue());
64 public void updateParams() {
65 AbstractDevice curModeDevice = getLinkedDevice("curmode", true);
66 if (curModeDevice != null) {
67 setCurModeParam(curModeDevice);
70 AbstractDevice curTempDevice = getLinkedDevice("curtemp", true);
71 if (curTempDevice != null) {
72 setCurTempParam(curTempDevice);
76 public void setStep(String step) {
77 addParam(new DeviceParam(ParamType.STEP, step));
80 public void setMinValue(String minValue) {
81 addParam(new DeviceParam(ParamType.MIN_VAL, minValue));
84 public void setMaxValue(String maxValue) {
85 addParam(new DeviceParam(ParamType.MAX_VAL, maxValue));
88 public void setAvailableModes(String modes) {
89 addParam(new DeviceParam(ParamType.AVAIL_MODE, modes));
92 private void setCurModeParam(AbstractDevice device) {
93 DeviceParam valueParam = device.getParams().get(ParamType.GENERIC_VALUE);
94 if (valueParam == null) {
95 logger.warn("Linked curmode device has no Value parameter: {}", device);
98 addParam(new DeviceParam(ParamType.CUR_MODE, valueParam.getValue()));
101 private void setCurTempParam(AbstractDevice device) {
102 NumericValueParam valueParam = (NumericValueParam) device.getParams().get(ParamType.TEMPERATURE_VALUE);
103 if (valueParam == null) {
104 logger.warn("Linked curtemp device has no Value parameter: {}", device);
108 NumericValueParam tempParam = new NumericValueParam(ParamType.CUR_TEMP, valueParam.getUnit(), null);
109 tempParam.setValue(valueParam.getValue());
113 private AbstractDevice getLinkedDevice(String linkName, boolean logWhenMissing) {
114 String deviceName = getLinks().get(linkName);
115 AbstractDevice device = null;
116 if (deviceName != null) {
117 String deviceId = ItemProcessor.getDeviceId(deviceName);
118 device = getDeviceRegistry().getDevice(deviceId);
120 if (logWhenMissing && device == null) {
121 logger.error("Couldn't resolve linked {} device '{}', make sure the Item has iss tags", linkName,