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.ITEM_TYPE_NUMBER;
16 import static org.openhab.binding.amazonechocontrol.internal.smarthome.Constants.ITEM_TYPE_STRING;
18 import java.io.IOException;
19 import java.util.List;
20 import java.util.Locale;
22 import org.apache.commons.lang.StringUtils;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.amazonechocontrol.internal.AmazonEchoControlBindingConstants;
26 import org.openhab.binding.amazonechocontrol.internal.Connection;
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.DecimalType;
30 import org.openhab.core.library.types.StringType;
31 import org.openhab.core.thing.type.ChannelTypeUID;
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 HandlerColorTemperatureController} is responsible for the Alexa.ColorTemperatureController
41 * @author Lukas Knoeller - Initial contribution
42 * @author Michael Geramb - Initial contribution
45 public class HandlerColorTemperatureController extends HandlerBase {
47 public static final String INTERFACE = "Alexa.ColorTemperatureController";
48 public static final String INTERFACE_COLOR_PROPERTIES = "Alexa.ColorPropertiesController";
51 private static final ChannelTypeUID CHANNEL_TYPE_COLOR_TEMPERATURE_NAME = new ChannelTypeUID(
52 AmazonEchoControlBindingConstants.BINDING_ID, "colorTemperatureName");
54 private static final ChannelTypeUID CHANNEL_TYPE_COLOR_TEPERATURE_IN_KELVIN = new ChannelTypeUID(
55 AmazonEchoControlBindingConstants.BINDING_ID, "colorTemperatureInKelvin");
57 // Channel and Properties
58 private static final ChannelInfo COLOR_TEMPERATURE_IN_KELVIN = new ChannelInfo(
59 "colorTemperatureInKelvin" /* propertyName */ , "colorTemperatureInKelvin" /* ChannelId */,
60 CHANNEL_TYPE_COLOR_TEPERATURE_IN_KELVIN /* Channel Type */ , ITEM_TYPE_NUMBER /* Item Type */);
62 private static final ChannelInfo COLOR_TEMPERATURE_NAME = new ChannelInfo("colorProperties" /* propertyName */ ,
63 "colorTemperatureName" /* ChannelId */, CHANNEL_TYPE_COLOR_TEMPERATURE_NAME /* Channel Type */ ,
64 ITEM_TYPE_STRING /* Item Type */);
66 private @Nullable Integer lastColorTemperature;
67 private @Nullable String lastColorName;
70 public String[] getSupportedInterface() {
71 return new String[] { INTERFACE, INTERFACE_COLOR_PROPERTIES };
75 protected ChannelInfo @Nullable [] findChannelInfos(SmartHomeCapability capability, String property) {
76 if (COLOR_TEMPERATURE_IN_KELVIN.propertyName.contentEquals(property)) {
77 return new ChannelInfo[] { COLOR_TEMPERATURE_IN_KELVIN, COLOR_TEMPERATURE_NAME };
83 public void updateChannels(String interfaceName, List<JsonObject> stateList, UpdateChannelResult result) {
84 if (INTERFACE.equals(interfaceName)) {
85 Integer colorTemperatureInKelvinValue = null;
86 for (JsonObject state : stateList) {
87 if (COLOR_TEMPERATURE_IN_KELVIN.propertyName.equals(state.get("name").getAsString())) {
88 int value = state.get("value").getAsInt();
89 // For groups take the maximum
90 if (colorTemperatureInKelvinValue == null) {
91 colorTemperatureInKelvinValue = value;
95 if (colorTemperatureInKelvinValue != null && !colorTemperatureInKelvinValue.equals(lastColorTemperature)) {
96 lastColorTemperature = colorTemperatureInKelvinValue;
97 result.needSingleUpdate = true;
99 updateState(COLOR_TEMPERATURE_IN_KELVIN.channelId, colorTemperatureInKelvinValue == null ? UnDefType.UNDEF
100 : new DecimalType(colorTemperatureInKelvinValue));
102 if (INTERFACE_COLOR_PROPERTIES.equals(interfaceName)) {
103 String colorTemperatureNameValue = null;
104 for (JsonObject state : stateList) {
105 if (COLOR_TEMPERATURE_NAME.propertyName.equals(state.get("name").getAsString())) {
106 if (colorTemperatureNameValue == null) {
107 result.needSingleUpdate = false;
108 colorTemperatureNameValue = state.get("value").getAsJsonObject().get("name").getAsString();
112 if (lastColorName == null) {
113 lastColorName = colorTemperatureNameValue;
114 } else if (colorTemperatureNameValue == null && lastColorName != null) {
115 colorTemperatureNameValue = lastColorName;
117 updateState(COLOR_TEMPERATURE_NAME.channelId,
118 colorTemperatureNameValue == null ? UnDefType.UNDEF : new StringType(colorTemperatureNameValue));
123 public boolean handleCommand(Connection connection, SmartHomeDevice shd, String entityId,
124 SmartHomeCapability[] capabilties, String channelId, Command command) throws IOException {
125 if (channelId.equals(COLOR_TEMPERATURE_IN_KELVIN.channelId)) {
126 // WRITING TO THIS CHANNEL DOES CURRENTLY NOT WORK, BUT WE LEAVE THE CODE FOR FUTURE USE!
127 if (containsCapabilityProperty(capabilties, COLOR_TEMPERATURE_IN_KELVIN.propertyName)) {
128 if (command instanceof DecimalType) {
129 int intValue = ((DecimalType) command).intValue();
130 if (intValue < 1000) {
133 if (intValue > 10000) {
136 connection.smartHomeCommand(entityId, "setColorTemperature", "colorTemperatureInKelvin", intValue);
141 if (channelId.equals(COLOR_TEMPERATURE_NAME.channelId)) {
142 if (containsCapabilityProperty(capabilties, COLOR_TEMPERATURE_IN_KELVIN.propertyName)) {
143 if (command instanceof StringType) {
144 String colorTemperatureName = ((StringType) command).toFullString();
145 if (StringUtils.isNotEmpty(colorTemperatureName)) {
146 lastColorName = colorTemperatureName;
147 connection.smartHomeCommand(entityId, "setColorTemperature", "colorTemperatureName",
148 colorTemperatureName);
158 public @Nullable StateDescription findStateDescription(String channelUID, StateDescription originalStateDescription,
159 @Nullable Locale locale) {