2 * Copyright (c) 2010-2024 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.binding.amazonechocontrol.internal.smarthome;
15 import static org.openhab.binding.amazonechocontrol.internal.smarthome.Constants.*;
17 import java.io.IOException;
18 import java.util.List;
19 import java.util.Locale;
21 import javax.measure.quantity.Temperature;
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;
39 import com.google.gson.JsonObject;
42 * The {@link HandlerThermostatController} is responsible for the Alexa.ThermostatControllerInterface
44 * @author Sven Killig - Initial contribution
47 public class HandlerThermostatController extends HandlerBase {
49 private final Logger logger = LoggerFactory.getLogger(HandlerThermostatController.class);
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 */);
66 public HandlerThermostatController(SmartHomeDeviceHandler smartHomeDeviceHandler) {
67 super(smartHomeDeviceHandler);
71 public String[] getSupportedInterface() {
72 return new String[] { INTERFACE };
76 protected ChannelInfo @Nullable [] findChannelInfos(SmartHomeCapability capability, String property) {
77 if (TARGET_SETPOINT.propertyName.equals(property)) {
78 return new ChannelInfo[] { TARGET_SETPOINT };
80 if (LOWER_SETPOINT.propertyName.equals(property)) {
81 return new ChannelInfo[] { LOWER_SETPOINT };
83 if (UPPER_SETPOINT.propertyName.equals(property)) {
84 return new ChannelInfo[] { UPPER_SETPOINT };
86 if (THERMOSTAT_MODE.propertyName.equals(property)) {
87 return new ChannelInfo[] { THERMOSTAT_MODE };
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);
106 temperatureValue = new QuantityType<Temperature>(temperature, ImperialUnits.FAHRENHEIT);
109 updateState(TARGET_SETPOINT.channelId, temperatureValue == null ? UnDefType.UNDEF : temperatureValue);
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);
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);
126 temperatureValue = new QuantityType<Temperature>(temperature, ImperialUnits.FAHRENHEIT);
129 updateState(UPPER_SETPOINT.channelId, temperatureValue == null ? UnDefType.UNDEF : temperatureValue);
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);
140 temperatureValue = new QuantityType<Temperature>(temperature, ImperialUnits.FAHRENHEIT);
143 updateState(LOWER_SETPOINT.channelId, temperatureValue == null ? UnDefType.UNDEF : temperatureValue);
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);
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);
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);
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);
188 public @Nullable StateDescription findStateDescription(String channelId, StateDescription originalStateDescription,
189 @Nullable Locale locale) {