From 68dd5569f08ff36f4d696b1a0dbb323fc996a98f Mon Sep 17 00:00:00 2001 From: Sven Killig Date: Sat, 5 Dec 2020 17:20:17 +0100 Subject: [PATCH] [amazonechocontrol] HandlerThermostatController (#9212) * HandlerThermostatController Signed-off-by: Sven Killig --- .../internal/Connection.java | 23 +++- .../internal/smarthome/Constants.java | 1 + .../HandlerThermostatController.java | 105 ++++++++++++++++++ 3 files changed, 126 insertions(+), 3 deletions(-) create mode 100644 bundles/org.openhab.binding.amazonechocontrol/src/main/java/org/openhab/binding/amazonechocontrol/internal/smarthome/HandlerThermostatController.java diff --git a/bundles/org.openhab.binding.amazonechocontrol/src/main/java/org/openhab/binding/amazonechocontrol/internal/Connection.java b/bundles/org.openhab.binding.amazonechocontrol/src/main/java/org/openhab/binding/amazonechocontrol/internal/Connection.java index dcc16059f26..e7287194ef2 100644 --- a/bundles/org.openhab.binding.amazonechocontrol/src/main/java/org/openhab/binding/amazonechocontrol/internal/Connection.java +++ b/bundles/org.openhab.binding.amazonechocontrol/src/main/java/org/openhab/binding/amazonechocontrol/internal/Connection.java @@ -40,7 +40,12 @@ import java.util.Objects; import java.util.Random; import java.util.Scanner; import java.util.Set; -import java.util.concurrent.*; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.Future; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; import java.util.regex.Matcher; @@ -102,11 +107,19 @@ import org.openhab.binding.amazonechocontrol.internal.jsons.JsonWakeWords.WakeWo import org.openhab.binding.amazonechocontrol.internal.jsons.JsonWebSiteCookie; import org.openhab.binding.amazonechocontrol.internal.jsons.SmartHomeBaseDevice; import org.openhab.core.common.ThreadPoolManager; +import org.openhab.core.library.types.QuantityType; +import org.openhab.core.library.unit.SIUnits; import org.openhab.core.util.HexUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.gson.*; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; +import com.google.gson.JsonSyntaxException; /** * The {@link Connection} is responsible for the connection to the amazon server @@ -1155,7 +1168,11 @@ public class Connection { JsonObject parameters = new JsonObject(); parameters.addProperty("action", action); if (property != null) { - if (value instanceof Boolean) { + if (value instanceof QuantityType) { + parameters.addProperty(property + ".value", ((QuantityType) value).floatValue()); + parameters.addProperty(property + ".scale", + ((QuantityType) value).getUnit().equals(SIUnits.CELSIUS) ? "celsius" : "fahrenheit"); + } else if (value instanceof Boolean) { parameters.addProperty(property, (boolean) value); } else if (value instanceof String) { parameters.addProperty(property, (String) value); diff --git a/bundles/org.openhab.binding.amazonechocontrol/src/main/java/org/openhab/binding/amazonechocontrol/internal/smarthome/Constants.java b/bundles/org.openhab.binding.amazonechocontrol/src/main/java/org/openhab/binding/amazonechocontrol/internal/smarthome/Constants.java index 8e18cf91465..8f227c26e48 100644 --- a/bundles/org.openhab.binding.amazonechocontrol/src/main/java/org/openhab/binding/amazonechocontrol/internal/smarthome/Constants.java +++ b/bundles/org.openhab.binding.amazonechocontrol/src/main/java/org/openhab/binding/amazonechocontrol/internal/smarthome/Constants.java @@ -36,6 +36,7 @@ public class Constants { HANDLER_FACTORY.put(HandlerSecurityPanelController.INTERFACE, HandlerSecurityPanelController::new); HANDLER_FACTORY.put(HandlerAcousticEventSensor.INTERFACE, HandlerAcousticEventSensor::new); HANDLER_FACTORY.put(HandlerTemperatureSensor.INTERFACE, HandlerTemperatureSensor::new); + HANDLER_FACTORY.put(HandlerThermostatController.INTERFACE, HandlerThermostatController::new); HANDLER_FACTORY.put(HandlerPercentageController.INTERFACE, HandlerPercentageController::new); HANDLER_FACTORY.put(HandlerPowerLevelController.INTERFACE, HandlerPowerLevelController::new); } diff --git a/bundles/org.openhab.binding.amazonechocontrol/src/main/java/org/openhab/binding/amazonechocontrol/internal/smarthome/HandlerThermostatController.java b/bundles/org.openhab.binding.amazonechocontrol/src/main/java/org/openhab/binding/amazonechocontrol/internal/smarthome/HandlerThermostatController.java new file mode 100644 index 00000000000..3b97a091d9c --- /dev/null +++ b/bundles/org.openhab.binding.amazonechocontrol/src/main/java/org/openhab/binding/amazonechocontrol/internal/smarthome/HandlerThermostatController.java @@ -0,0 +1,105 @@ +/** + * Copyright (c) 2010-2020 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.amazonechocontrol.internal.smarthome; + +import static org.openhab.binding.amazonechocontrol.internal.smarthome.Constants.*; + +import java.io.IOException; +import java.util.List; +import java.util.Locale; + +import javax.measure.quantity.Temperature; + +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; +import org.openhab.binding.amazonechocontrol.internal.Connection; +import org.openhab.binding.amazonechocontrol.internal.jsons.JsonSmartHomeCapabilities.SmartHomeCapability; +import org.openhab.binding.amazonechocontrol.internal.jsons.JsonSmartHomeDevices.SmartHomeDevice; +import org.openhab.core.library.types.QuantityType; +import org.openhab.core.library.unit.ImperialUnits; +import org.openhab.core.library.unit.SIUnits; +import org.openhab.core.types.Command; +import org.openhab.core.types.StateDescription; +import org.openhab.core.types.UnDefType; + +import com.google.gson.JsonObject; + +/** + * The {@link HandlerThermostatController} is responsible for the Alexa.ThermostatControllerInterface + * + * @author Sven Killig - Initial contribution + */ +@NonNullByDefault +public class HandlerThermostatController extends HandlerBase { + // Interface + public static final String INTERFACE = "Alexa.ThermostatController"; + // Channel definitions + private static final ChannelInfo TARGET_SETPOINT = new ChannelInfo("targetSetpoint" /* propertyName */ , + "targetSetpoint" /* ChannelId */, CHANNEL_TYPE_TEMPERATURE /* Channel Type */ , + ITEM_TYPE_NUMBER_TEMPERATURE /* Item Type */); + + @Override + public String[] getSupportedInterface() { + return new String[] { INTERFACE }; + } + + @Override + protected ChannelInfo @Nullable [] findChannelInfos(SmartHomeCapability capability, String property) { + if (TARGET_SETPOINT.propertyName.equals(property)) { + return new ChannelInfo[] { TARGET_SETPOINT }; + } + return null; + } + + @Override + public void updateChannels(String interfaceName, List stateList, UpdateChannelResult result) { + QuantityType temperatureValue = null; + for (JsonObject state : stateList) { + if (TARGET_SETPOINT.propertyName.equals(state.get("name").getAsString())) { + JsonObject value = state.get("value").getAsJsonObject(); + // For groups take the first + if (temperatureValue == null) { + float temperature = value.get("value").getAsFloat(); + String scale = value.get("scale").getAsString().toUpperCase(); + if ("CELSIUS".equals(scale)) { + temperatureValue = new QuantityType(temperature, SIUnits.CELSIUS); + } else { + temperatureValue = new QuantityType(temperature, ImperialUnits.FAHRENHEIT); + } + } + } + } + updateState(TARGET_SETPOINT.channelId, temperatureValue == null ? UnDefType.UNDEF : temperatureValue); + } + + @Override + public boolean handleCommand(Connection connection, SmartHomeDevice shd, String entityId, + SmartHomeCapability[] capabilties, String channelId, Command command) + throws IOException, InterruptedException { + if (channelId.equals(TARGET_SETPOINT.channelId)) { + if (containsCapabilityProperty(capabilties, TARGET_SETPOINT.propertyName)) { + if (command instanceof QuantityType) { + connection.smartHomeCommand(entityId, "setTargetTemperature", "targetTemperature", command); + return true; + } + } + } + return false; + } + + @Override + public @Nullable StateDescription findStateDescription(String channelId, StateDescription originalStateDescription, + @Nullable Locale locale) { + return null; + } +}