]> git.basschouten.com Git - openhab-addons.git/blob
fd9da556d5f0212e2ed5b322eb3ae3d2329446fd
[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.amazonechocontrol.internal.smarthome;
14
15 import static org.openhab.binding.amazonechocontrol.internal.smarthome.Constants.*;
16
17 import java.io.IOException;
18 import java.util.List;
19 import java.util.Locale;
20
21 import javax.measure.quantity.Temperature;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.amazonechocontrol.internal.Connection;
26 import org.openhab.binding.amazonechocontrol.internal.handler.SmartHomeDeviceHandler;
27 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonSmartHomeCapabilities.SmartHomeCapability;
28 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonSmartHomeDevices.SmartHomeDevice;
29 import org.openhab.core.library.types.QuantityType;
30 import org.openhab.core.library.types.StringType;
31 import org.openhab.core.library.unit.ImperialUnits;
32 import org.openhab.core.library.unit.SIUnits;
33 import org.openhab.core.types.Command;
34 import org.openhab.core.types.StateDescription;
35 import org.openhab.core.types.UnDefType;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 import com.google.gson.JsonObject;
40
41 /**
42  * The {@link HandlerThermostatController} is responsible for the Alexa.ThermostatControllerInterface
43  *
44  * @author Sven Killig - Initial contribution
45  */
46 @NonNullByDefault
47 public class HandlerThermostatController extends HandlerBase {
48     // Logger
49     private final Logger logger = LoggerFactory.getLogger(HandlerThermostatController.class);
50     // Interface
51     public static final String INTERFACE = "Alexa.ThermostatController";
52     // Channel definitions
53     private static final ChannelInfo TARGET_SETPOINT = new ChannelInfo("targetSetpoint" /* propertyName */ ,
54             "targetSetpoint" /* ChannelId */, CHANNEL_TYPE_TARGETSETPOINT /* Channel Type */ ,
55             ITEM_TYPE_NUMBER_TEMPERATURE /* Item Type */);
56     private static final ChannelInfo LOWER_SETPOINT = new ChannelInfo("lowerSetpoint" /* propertyName */ ,
57             "lowerSetpoint" /* ChannelId */, CHANNEL_TYPE_LOWERSETPOINT /* Channel Type */ ,
58             ITEM_TYPE_NUMBER_TEMPERATURE /* Item Type */);
59     private static final ChannelInfo UPPER_SETPOINT = new ChannelInfo("upperSetpoint" /* propertyName */ ,
60             "upperSetpoint" /* ChannelId */, CHANNEL_TYPE_UPPERSETPOINT /* Channel Type */ ,
61             ITEM_TYPE_NUMBER_TEMPERATURE /* Item Type */);
62     private static final ChannelInfo THERMOSTAT_MODE = new ChannelInfo("thermostatMode" /* propertyName */ ,
63             "thermostatMode" /* ChannelId */, CHANNEL_TYPE_THERMOSTATMODE /* Channel Type */ ,
64             ITEM_TYPE_STRING /* Item Type */);
65
66     public HandlerThermostatController(SmartHomeDeviceHandler smartHomeDeviceHandler) {
67         super(smartHomeDeviceHandler);
68     }
69
70     @Override
71     public String[] getSupportedInterface() {
72         return new String[] { INTERFACE };
73     }
74
75     @Override
76     protected ChannelInfo @Nullable [] findChannelInfos(SmartHomeCapability capability, String property) {
77         if (TARGET_SETPOINT.propertyName.equals(property)) {
78             return new ChannelInfo[] { TARGET_SETPOINT };
79         }
80         if (LOWER_SETPOINT.propertyName.equals(property)) {
81             return new ChannelInfo[] { LOWER_SETPOINT };
82         }
83         if (UPPER_SETPOINT.propertyName.equals(property)) {
84             return new ChannelInfo[] { UPPER_SETPOINT };
85         }
86         if (THERMOSTAT_MODE.propertyName.equals(property)) {
87             return new ChannelInfo[] { THERMOSTAT_MODE };
88         }
89         return null;
90     }
91
92     @Override
93     public void updateChannels(String interfaceName, List<JsonObject> stateList, UpdateChannelResult result) {
94         for (JsonObject state : stateList) {
95             QuantityType<Temperature> temperatureValue = null;
96             logger.debug("Updating {} with state: {}", interfaceName, state.toString());
97             if (TARGET_SETPOINT.propertyName.equals(state.get("name").getAsString())) {
98                 // For groups take the first
99                 if (temperatureValue == null) {
100                     JsonObject value = state.get("value").getAsJsonObject();
101                     float temperature = value.get("value").getAsFloat();
102                     String scale = value.get("scale").getAsString().toUpperCase();
103                     if ("CELSIUS".equals(scale)) {
104                         temperatureValue = new QuantityType<Temperature>(temperature, SIUnits.CELSIUS);
105                     } else {
106                         temperatureValue = new QuantityType<Temperature>(temperature, ImperialUnits.FAHRENHEIT);
107                     }
108                 }
109                 updateState(TARGET_SETPOINT.channelId, temperatureValue == null ? UnDefType.UNDEF : temperatureValue);
110             }
111             if (THERMOSTAT_MODE.propertyName.equals(state.get("name").getAsString())) {
112                 // For groups take the first
113                 String operation = state.get("value").getAsString().toUpperCase();
114                 StringType operationValue = new StringType(operation);
115                 updateState(THERMOSTAT_MODE.channelId, operationValue);
116             }
117             if (UPPER_SETPOINT.propertyName.equals(state.get("name").getAsString())) {
118                 // For groups take the first
119                 if (temperatureValue == null) {
120                     JsonObject value = state.get("value").getAsJsonObject();
121                     float temperature = value.get("value").getAsFloat();
122                     String scale = value.get("scale").getAsString().toUpperCase();
123                     if ("CELSIUS".equals(scale)) {
124                         temperatureValue = new QuantityType<Temperature>(temperature, SIUnits.CELSIUS);
125                     } else {
126                         temperatureValue = new QuantityType<Temperature>(temperature, ImperialUnits.FAHRENHEIT);
127                     }
128                 }
129                 updateState(UPPER_SETPOINT.channelId, temperatureValue == null ? UnDefType.UNDEF : temperatureValue);
130             }
131             if (LOWER_SETPOINT.propertyName.equals(state.get("name").getAsString())) {
132                 // For groups take the first
133                 if (temperatureValue == null) {
134                     JsonObject value = state.get("value").getAsJsonObject();
135                     float temperature = value.get("value").getAsFloat();
136                     String scale = value.get("scale").getAsString().toUpperCase();
137                     if ("CELSIUS".equals(scale)) {
138                         temperatureValue = new QuantityType<Temperature>(temperature, SIUnits.CELSIUS);
139                     } else {
140                         temperatureValue = new QuantityType<Temperature>(temperature, ImperialUnits.FAHRENHEIT);
141                     }
142                 }
143                 updateState(LOWER_SETPOINT.channelId, temperatureValue == null ? UnDefType.UNDEF : temperatureValue);
144             }
145         }
146     }
147
148     @Override
149     public boolean handleCommand(Connection connection, SmartHomeDevice shd, String entityId,
150             List<SmartHomeCapability> capabilities, String channelId, Command command)
151             throws IOException, InterruptedException {
152         if (channelId.equals(TARGET_SETPOINT.channelId)) {
153             if (containsCapabilityProperty(capabilities, TARGET_SETPOINT.propertyName)) {
154                 if (command instanceof QuantityType) {
155                     connection.smartHomeCommand(entityId, "setTargetTemperature", "targetTemperature", command);
156                     return true;
157                 }
158             }
159         }
160         if (channelId.equals(LOWER_SETPOINT.channelId)) {
161             if (containsCapabilityProperty(capabilities, LOWER_SETPOINT.propertyName)) {
162                 if (command instanceof QuantityType) {
163                     connection.smartHomeCommand(entityId, "setTargetTemperature", "lowerSetTemperature", command);
164                     return true;
165                 }
166             }
167         }
168         if (channelId.equals(UPPER_SETPOINT.channelId)) {
169             if (containsCapabilityProperty(capabilities, UPPER_SETPOINT.propertyName)) {
170                 if (command instanceof QuantityType) {
171                     connection.smartHomeCommand(entityId, "setTargetTemperature", "upperSetTemperature", command);
172                     return true;
173                 }
174             }
175         }
176         if (channelId.equals(THERMOSTAT_MODE.channelId)) {
177             if (containsCapabilityProperty(capabilities, THERMOSTAT_MODE.propertyName)) {
178                 if (command instanceof StringType) {
179                     connection.smartHomeCommand(entityId, "setThermostatMode", "thermostatMode", command);
180                     return true;
181                 }
182             }
183         }
184         return false;
185     }
186
187     @Override
188     public @Nullable StateDescription findStateDescription(String channelId, StateDescription originalStateDescription,
189             @Nullable Locale locale) {
190         return null;
191     }
192 }