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_COLOR;
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.HSBType;
31 import org.openhab.core.library.types.PercentType;
32 import org.openhab.core.library.types.StringType;
33 import org.openhab.core.thing.type.ChannelTypeUID;
34 import org.openhab.core.types.Command;
35 import org.openhab.core.types.StateDescription;
36 import org.openhab.core.types.UnDefType;
38 import com.google.gson.JsonObject;
41 * The {@link HandlerColorController} is responsible for the Alexa.ColorTemperatureController
43 * @author Lukas Knoeller - Initial contribution
44 * @author Michael Geramb - Initial contribution
47 public class HandlerColorController extends HandlerBase {
49 public static final String INTERFACE = "Alexa.ColorController";
50 public static final String INTERFACE_COLOR_PROPERTIES = "Alexa.ColorPropertiesController";
53 private static final ChannelTypeUID CHANNEL_TYPE_COLOR_NAME = new ChannelTypeUID(
54 AmazonEchoControlBindingConstants.BINDING_ID, "colorName");
56 private static final ChannelTypeUID CHANNEL_TYPE_COLOR = new ChannelTypeUID(
57 AmazonEchoControlBindingConstants.BINDING_ID, "color");
59 // Channel and Properties
60 private static final ChannelInfo COLOR = new ChannelInfo("color" /* propertyName */, "color" /* ChannelId */,
61 CHANNEL_TYPE_COLOR /* Channel Type */, ITEM_TYPE_COLOR /* Item Type */);
63 private static final ChannelInfo COLOR_PROPERTIES = new ChannelInfo("colorProperties" /* propertyName */,
64 "colorName" /* ChannelId */, CHANNEL_TYPE_COLOR_NAME /* Channel Type */, ITEM_TYPE_STRING /* Item Type */);
66 private @Nullable HSBType lastColor;
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.propertyName.contentEquals(property)) {
77 return new ChannelInfo[] { COLOR, COLOR_PROPERTIES };
83 public void updateChannels(String interfaceName, List<JsonObject> stateList, UpdateChannelResult result) {
84 if (INTERFACE.equals(interfaceName)) {
85 // WRITING TO THIS CHANNEL DOES CURRENTLY NOT WORK, BUT WE LEAVE THE CODE FOR FUTURE USE!
86 HSBType colorValue = null;
87 for (JsonObject state : stateList) {
88 if (COLOR.propertyName.equals(state.get("name").getAsString())) {
89 JsonObject value = state.get("value").getAsJsonObject();
90 // For groups take the maximum
91 if (colorValue == null) {
92 colorValue = new HSBType(new DecimalType(value.get("hue").getAsInt()),
93 new PercentType(value.get("saturation").getAsInt() * 100),
94 new PercentType(value.get("brightness").getAsInt() * 100));
98 if (colorValue != null) {
99 if (!colorValue.equals(lastColor)) {
100 result.needSingleUpdate = true;
101 lastColor = colorValue;
104 updateState(COLOR.channelId, colorValue == null ? UnDefType.UNDEF : colorValue);
106 if (INTERFACE_COLOR_PROPERTIES.equals(interfaceName)) {
107 String colorNameValue = null;
108 for (JsonObject state : stateList) {
109 if (COLOR_PROPERTIES.propertyName.equals(state.get("name").getAsString())) {
110 if (colorNameValue == null) {
111 result.needSingleUpdate = false;
112 colorNameValue = state.get("value").getAsJsonObject().get("name").getAsString();
116 if (lastColorName == null) {
117 lastColorName = colorNameValue;
118 } else if (colorNameValue == null && lastColorName != null) {
119 colorNameValue = lastColorName;
121 updateState(COLOR_PROPERTIES.channelId,
122 lastColorName == null ? UnDefType.UNDEF : new StringType(lastColorName));
127 public boolean handleCommand(Connection connection, SmartHomeDevice shd, String entityId,
128 SmartHomeCapability[] capabilties, String channelId, Command command) throws IOException {
129 if (channelId.equals(COLOR.channelId)) {
130 if (containsCapabilityProperty(capabilties, COLOR.propertyName)) {
131 if (command instanceof HSBType) {
132 HSBType color = ((HSBType) command);
133 JsonObject colorObject = new JsonObject();
134 colorObject.addProperty("hue", color.getHue());
135 colorObject.addProperty("saturation", color.getSaturation().floatValue() / 100);
136 colorObject.addProperty("brightness", color.getBrightness().floatValue() / 100);
137 connection.smartHomeCommand(entityId, "setColor", "color", colorObject);
141 if (channelId.equals(COLOR_PROPERTIES.channelId)) {
142 if (containsCapabilityProperty(capabilties, COLOR.propertyName)) {
143 if (command instanceof StringType) {
144 String colorName = ((StringType) command).toFullString();
145 if (StringUtils.isNotEmpty(colorName)) {
146 lastColorName = colorName;
147 connection.smartHomeCommand(entityId, "setColor", "colorName", colorName);
157 public @Nullable StateDescription findStateDescription(String channelId, StateDescription originalStateDescription,
158 @Nullable Locale locale) {