2 * Copyright (c) 2010-2022 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.unit.ImperialUnits;
31 import org.openhab.core.library.unit.SIUnits;
32 import org.openhab.core.types.Command;
33 import org.openhab.core.types.StateDescription;
34 import org.openhab.core.types.UnDefType;
36 import com.google.gson.JsonObject;
39 * The {@link HandlerThermostatController} is responsible for the Alexa.ThermostatControllerInterface
41 * @author Sven Killig - Initial contribution
44 public class HandlerThermostatController extends HandlerBase {
46 public static final String INTERFACE = "Alexa.ThermostatController";
47 // Channel definitions
48 private static final ChannelInfo TARGET_SETPOINT = new ChannelInfo("targetSetpoint" /* propertyName */ ,
49 "targetSetpoint" /* ChannelId */, CHANNEL_TYPE_TARGETSETPOINT /* Channel Type */ ,
50 ITEM_TYPE_NUMBER_TEMPERATURE /* Item Type */);
52 public HandlerThermostatController(SmartHomeDeviceHandler smartHomeDeviceHandler) {
53 super(smartHomeDeviceHandler);
57 public String[] getSupportedInterface() {
58 return new String[] { INTERFACE };
62 protected ChannelInfo @Nullable [] findChannelInfos(SmartHomeCapability capability, String property) {
63 if (TARGET_SETPOINT.propertyName.equals(property)) {
64 return new ChannelInfo[] { TARGET_SETPOINT };
70 public void updateChannels(String interfaceName, List<JsonObject> stateList, UpdateChannelResult result) {
71 QuantityType<Temperature> temperatureValue = null;
72 for (JsonObject state : stateList) {
73 if (TARGET_SETPOINT.propertyName.equals(state.get("name").getAsString())) {
74 JsonObject value = state.get("value").getAsJsonObject();
75 // For groups take the first
76 if (temperatureValue == null) {
77 float temperature = value.get("value").getAsFloat();
78 String scale = value.get("scale").getAsString().toUpperCase();
79 if ("CELSIUS".equals(scale)) {
80 temperatureValue = new QuantityType<Temperature>(temperature, SIUnits.CELSIUS);
82 temperatureValue = new QuantityType<Temperature>(temperature, ImperialUnits.FAHRENHEIT);
87 updateState(TARGET_SETPOINT.channelId, temperatureValue == null ? UnDefType.UNDEF : temperatureValue);
91 public boolean handleCommand(Connection connection, SmartHomeDevice shd, String entityId,
92 List<SmartHomeCapability> capabilities, String channelId, Command command)
93 throws IOException, InterruptedException {
94 if (channelId.equals(TARGET_SETPOINT.channelId)) {
95 if (containsCapabilityProperty(capabilities, TARGET_SETPOINT.propertyName)) {
96 if (command instanceof QuantityType) {
97 connection.smartHomeCommand(entityId, "setTargetTemperature", "targetTemperature", command);
106 public @Nullable StateDescription findStateDescription(String channelId, StateDescription originalStateDescription,
107 @Nullable Locale locale) {