Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[amazonechocontrol] HandlerThermostatController #9212

Merged
merged 3 commits into from
Dec 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@
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;
Expand Down Expand Up @@ -104,11 +109,19 @@
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
Expand Down Expand Up @@ -1157,7 +1170,11 @@ public void smartHomeCommand(String entityId, String action, @Nullable String pr
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}