2 * Copyright (c) 2010-2020 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.jsons.JsonSmartHomeCapabilities.SmartHomeCapability;
27 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonSmartHomeDevices.SmartHomeDevice;
28 import org.openhab.core.library.types.QuantityType;
29 import org.openhab.core.library.unit.ImperialUnits;
30 import org.openhab.core.library.unit.SIUnits;
31 import org.openhab.core.types.Command;
32 import org.openhab.core.types.StateDescription;
33 import org.openhab.core.types.UnDefType;
35 import com.google.gson.JsonObject;
38 * The {@link HandlerThermostatController} is responsible for the Alexa.ThermostatControllerInterface
40 * @author Sven Killig - Initial contribution
43 public class HandlerThermostatController extends HandlerBase {
45 public static final String INTERFACE = "Alexa.ThermostatController";
46 // Channel definitions
47 private static final ChannelInfo TARGET_SETPOINT = new ChannelInfo("targetSetpoint" /* propertyName */ ,
48 "targetSetpoint" /* ChannelId */, CHANNEL_TYPE_TEMPERATURE /* Channel Type */ ,
49 ITEM_TYPE_NUMBER_TEMPERATURE /* Item Type */);
52 public String[] getSupportedInterface() {
53 return new String[] { INTERFACE };
57 protected ChannelInfo @Nullable [] findChannelInfos(SmartHomeCapability capability, String property) {
58 if (TARGET_SETPOINT.propertyName.equals(property)) {
59 return new ChannelInfo[] { TARGET_SETPOINT };
65 public void updateChannels(String interfaceName, List<JsonObject> stateList, UpdateChannelResult result) {
66 QuantityType<Temperature> temperatureValue = null;
67 for (JsonObject state : stateList) {
68 if (TARGET_SETPOINT.propertyName.equals(state.get("name").getAsString())) {
69 JsonObject value = state.get("value").getAsJsonObject();
70 // For groups take the first
71 if (temperatureValue == null) {
72 float temperature = value.get("value").getAsFloat();
73 String scale = value.get("scale").getAsString().toUpperCase();
74 if ("CELSIUS".equals(scale)) {
75 temperatureValue = new QuantityType<Temperature>(temperature, SIUnits.CELSIUS);
77 temperatureValue = new QuantityType<Temperature>(temperature, ImperialUnits.FAHRENHEIT);
82 updateState(TARGET_SETPOINT.channelId, temperatureValue == null ? UnDefType.UNDEF : temperatureValue);
86 public boolean handleCommand(Connection connection, SmartHomeDevice shd, String entityId,
87 SmartHomeCapability[] capabilties, String channelId, Command command)
88 throws IOException, InterruptedException {
89 if (channelId.equals(TARGET_SETPOINT.channelId)) {
90 if (containsCapabilityProperty(capabilties, TARGET_SETPOINT.propertyName)) {
91 if (command instanceof QuantityType) {
92 connection.smartHomeCommand(entityId, "setTargetTemperature", "targetTemperature", command);
101 public @Nullable StateDescription findStateDescription(String channelId, StateDescription originalStateDescription,
102 @Nullable Locale locale) {