]> git.basschouten.com Git - openhab-addons.git/blob
a33725f507cab11c0ec72cf9888a1b477ee27a4d
[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 java.util.List;
16 import java.util.Map;
17
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;
26
27 /**
28  * Thermostat device.
29  *
30  * @author Pepijn de Geus - Initial contribution
31  */
32 public class ThermostatDevice extends AbstractDevice {
33
34     public ThermostatDevice(Item item) {
35         super(DeviceType.THERMOSTAT, item);
36     }
37
38     @Override
39     public void processCustomTags(Map<TagType, List<String>> issTags) {
40         if (issTags.containsKey(TagType.STEP)) {
41             setStep(issTags.get(TagType.STEP).get(0));
42         }
43         if (issTags.containsKey(TagType.MIN_VAL)) {
44             setMinValue(issTags.get(TagType.MIN_VAL).get(0));
45         }
46         if (issTags.containsKey(TagType.MAX_VAL)) {
47             setMaxValue(issTags.get(TagType.MAX_VAL).get(0));
48         }
49         if (issTags.containsKey(TagType.MODES)) {
50             setAvailableModes(issTags.get(TagType.MODES).get(0));
51         }
52     }
53
54     @Override
55     public void stateUpdated(Item item, State newState) {
56         DecimalType state = (DecimalType) item.getStateAs(DecimalType.class);
57         if (state != null) {
58             DeviceParam param = new DeviceParam(ParamType.CUR_SETPOINT, state.floatValue());
59             addParam(param);
60         }
61     }
62
63     @Override
64     public void updateParams() {
65         AbstractDevice curModeDevice = getLinkedDevice("curmode", true);
66         if (curModeDevice != null) {
67             setCurModeParam(curModeDevice);
68         }
69
70         AbstractDevice curTempDevice = getLinkedDevice("curtemp", true);
71         if (curTempDevice != null) {
72             setCurTempParam(curTempDevice);
73         }
74     }
75
76     public void setStep(String step) {
77         addParam(new DeviceParam(ParamType.STEP, step));
78     }
79
80     public void setMinValue(String minValue) {
81         addParam(new DeviceParam(ParamType.MIN_VAL, minValue));
82     }
83
84     public void setMaxValue(String maxValue) {
85         addParam(new DeviceParam(ParamType.MAX_VAL, maxValue));
86     }
87
88     public void setAvailableModes(String modes) {
89         addParam(new DeviceParam(ParamType.AVAIL_MODE, modes));
90     }
91
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);
96             return;
97         }
98         addParam(new DeviceParam(ParamType.CUR_MODE, valueParam.getValue()));
99     }
100
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);
105             return;
106         }
107
108         NumericValueParam tempParam = new NumericValueParam(ParamType.CUR_TEMP, valueParam.getUnit(), null);
109         tempParam.setValue(valueParam.getValue());
110         addParam(tempParam);
111     }
112
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);
119         }
120         if (logWhenMissing && device == null) {
121             logger.error("Couldn't resolve linked {} device '{}', make sure the Item has iss tags", linkName,
122                     deviceName);
123         }
124         return device;
125     }
126 }