]> git.basschouten.com Git - openhab-addons.git/blob
82cc78b7db40ce66394ed40a97d6c0ad51e74cf4
[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 thermostat) {
44             updateThermostatState(thermostat);
45             updateTargetTemperature(thermostat);
46             updateRoomTemperature(thermostat);
47             updateMode(thermostat);
48             updateFanLevel(thermostat);
49         } else {
50             logger.warn("updateTouchWandUnitState incompatible TouchWandUnitData instance");
51         }
52     }
53
54     @Override
55     void touchWandUnitHandleCommand(Command command) {
56         TouchWandBridgeHandler touchWandBridgeHandler = bridgeHandler;
57         if (touchWandBridgeHandler != null) {
58             if (command instanceof OnOffType onOffCommand) {
59                 touchWandBridgeHandler.touchWandClient.cmdThermostatOnOff(unitId, onOffCommand);
60                 return;
61             }
62             if (command instanceof QuantityType quantityCommand) {
63                 final QuantityType<?> value = quantityCommand.toUnit(SIUnits.CELSIUS);
64                 String targetTemperature = String.valueOf(value.intValue());
65                 touchWandBridgeHandler.touchWandClient.cmdThermostatTargetTemperature(unitId, targetTemperature);
66                 return;
67             }
68
69             String sCommand = command.toString();
70             switch (sCommand) {
71                 case "cool":
72                 case "heat":
73                 case "fan":
74                 case "auto":
75                 case "dry":
76                     touchWandBridgeHandler.touchWandClient.cmdThermostatMode(unitId, sCommand);
77                     break;
78                 case "low":
79                 case "medium":
80                 case "high":
81                     touchWandBridgeHandler.touchWandClient.cmdThermostatFanLevel(unitId, sCommand);
82                     break;
83                 case "fanAuto":
84                     touchWandBridgeHandler.touchWandClient.cmdThermostatFanLevel(unitId, "auto");
85                     break;
86                 default:
87                     break;
88             }
89         }
90     }
91
92     void updateThermostatState(TouchWandThermostatUnitData unitData) {
93         String state = unitData.getCurrStatus().getState();
94         updateState(CHANNEL_THERMOSTAT_STATE, OnOffType.from(state));
95     }
96
97     void updateTargetTemperature(TouchWandThermostatUnitData unitData) {
98         int targetTemperature = unitData.getCurrStatus().getTargetTemperature();
99         QuantityType<Temperature> temperatureValue = new QuantityType<Temperature>(targetTemperature, SIUnits.CELSIUS);
100         updateState(CHANNEL_THERMOSTAT_TARGET_TEMPERATURE, temperatureValue);
101     }
102
103     void updateRoomTemperature(TouchWandThermostatUnitData unitData) {
104         int roomTemperature = unitData.getCurrStatus().getRoomTemperature();
105         QuantityType<Temperature> temperatureValue = new QuantityType<Temperature>(roomTemperature, SIUnits.CELSIUS);
106         updateState(CHANNEL_THERMOSTAT_ROOM_TEMPERATURE, temperatureValue);
107     }
108
109     void updateMode(TouchWandThermostatUnitData unitData) {
110         String mode = unitData.getCurrStatus().getMode();
111         StringType newVal = StringType.valueOf(mode);
112         updateState(CHANNEL_THERMOSTAT_MODE, newVal);
113     }
114
115     void updateFanLevel(TouchWandThermostatUnitData unitData) {
116         String fanLevel = unitData.getCurrStatus().getFanLevel();
117         StringType newVal = StringType.valueOf(fanLevel);
118         updateState(CHANNEL_THERMOSTAT_FAN_LEVEL, newVal);
119     }
120 }