]> git.basschouten.com Git - openhab-addons.git/blob
45ccd1b34b5a580aa646da42b6b860fa94dc6635
[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.binding.touchwand.internal;
14
15 import static org.openhab.binding.touchwand.internal.TouchWandBindingConstants.*;
16
17 import javax.measure.quantity.Temperature;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.touchwand.internal.dto.TouchWandThermostatUnitData;
21 import org.openhab.binding.touchwand.internal.dto.TouchWandUnitData;
22 import org.openhab.core.library.types.OnOffType;
23 import org.openhab.core.library.types.QuantityType;
24 import org.openhab.core.library.types.StringType;
25 import org.openhab.core.library.unit.SIUnits;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.types.Command;
28
29 /**
30  * The {@link TouchWandAlarmSensorHandler} is responsible for handling command for Alarm Sensor unit
31  *
32  * @author Roie Geron - Initial contribution
33  */
34 @NonNullByDefault
35 public class TouchWandThermostatHandler extends TouchWandBaseUnitHandler {
36
37     public TouchWandThermostatHandler(Thing thing) {
38         super(thing);
39     }
40
41     @Override
42     void updateTouchWandUnitState(TouchWandUnitData unitData) {
43         if (unitData instanceof TouchWandThermostatUnitData) {
44             TouchWandThermostatUnitData thermostat = (TouchWandThermostatUnitData) unitData;
45             updateThermostatState(thermostat);
46             updateTargetTemperature(thermostat);
47             updateRoomTemperature(thermostat);
48             updateMode(thermostat);
49             updateFanLevel(thermostat);
50         } else {
51             logger.warn("updateTouchWandUnitState incompatible TouchWandUnitData instance");
52         }
53     }
54
55     @Override
56     void touchWandUnitHandleCommand(Command command) {
57         TouchWandBridgeHandler touchWandBridgeHandler = bridgeHandler;
58         if (touchWandBridgeHandler != null) {
59             if (command instanceof OnOffType) {
60                 touchWandBridgeHandler.touchWandClient.cmdThermostatOnOff(unitId, (OnOffType) command);
61                 return;
62             }
63             if (command instanceof QuantityType) {
64                 final QuantityType<?> value = ((QuantityType<?>) command).toUnit(SIUnits.CELSIUS);
65                 String targetTemperature = String.valueOf(value.intValue());
66                 touchWandBridgeHandler.touchWandClient.cmdThermostatTargetTemperature(unitId, targetTemperature);
67                 return;
68             }
69
70             String sCommand = command.toString();
71             switch (sCommand) {
72                 case "cool":
73                 case "heat":
74                 case "fan":
75                 case "auto":
76                 case "dry":
77                     touchWandBridgeHandler.touchWandClient.cmdThermostatMode(unitId, sCommand);
78                     break;
79                 case "low":
80                 case "medium":
81                 case "high":
82                     touchWandBridgeHandler.touchWandClient.cmdThermostatFanLevel(unitId, sCommand);
83                     break;
84                 case "fanAuto":
85                     touchWandBridgeHandler.touchWandClient.cmdThermostatFanLevel(unitId, "auto");
86                     break;
87                 default:
88                     break;
89             }
90         }
91     }
92
93     void updateThermostatState(TouchWandThermostatUnitData unitData) {
94         String state = unitData.getCurrStatus().getState();
95         updateState(CHANNEL_THERMOSTAT_STATE, OnOffType.from(state));
96     }
97
98     void updateTargetTemperature(TouchWandThermostatUnitData unitData) {
99         int targetTemperature = unitData.getCurrStatus().getTargetTemperature();
100         QuantityType<Temperature> temperatureValue = new QuantityType<Temperature>(targetTemperature, SIUnits.CELSIUS);
101         updateState(CHANNEL_THERMOSTAT_TARGET_TEMPERATURE, temperatureValue);
102     }
103
104     void updateRoomTemperature(TouchWandThermostatUnitData unitData) {
105         int roomTemperature = unitData.getCurrStatus().getRoomTemperature();
106         QuantityType<Temperature> temperatureValue = new QuantityType<Temperature>(roomTemperature, SIUnits.CELSIUS);
107         updateState(CHANNEL_THERMOSTAT_ROOM_TEMPERATURE, temperatureValue);
108     }
109
110     void updateMode(TouchWandThermostatUnitData unitData) {
111         String mode = unitData.getCurrStatus().getMode();
112         StringType newVal = StringType.valueOf(mode);
113         updateState(CHANNEL_THERMOSTAT_MODE, newVal);
114     }
115
116     void updateFanLevel(TouchWandThermostatUnitData unitData) {
117         String fanLevel = unitData.getCurrStatus().getFanLevel();
118         StringType newVal = StringType.valueOf(fanLevel);
119         updateState(CHANNEL_THERMOSTAT_FAN_LEVEL, newVal);
120     }
121 }