[amazonechocontrol] HandlerThermostatController (#9212)

* HandlerThermostatController

Signed-off-by: Sven Killig <sven@killig.de>
This commit is contained in:
Sven Killig 2020-12-05 17:20:17 +01:00 committed by GitHub
parent b8bc020348
commit 68dd5569f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 126 additions and 3 deletions

View File

@ -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);

View File

@ -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);
}

View File

@ -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<JsonObject> stateList, UpdateChannelResult result) {
QuantityType<Temperature> 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>(temperature, SIUnits.CELSIUS);
} else {
temperatureValue = new QuantityType<Temperature>(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;
}
}