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.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.amazonechocontrol.internal.AmazonEchoControlBindingConstants;
25 import org.openhab.binding.amazonechocontrol.internal.Connection;
26 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonSmartHomeCapabilities.SmartHomeCapability;
27 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonSmartHomeDevices.SmartHomeDevice;
28 import org.openhab.core.library.types.DecimalType;
29 import org.openhab.core.library.types.HSBType;
30 import org.openhab.core.library.types.PercentType;
31 import org.openhab.core.library.types.StringType;
32 import org.openhab.core.thing.type.ChannelTypeUID;
33 import org.openhab.core.types.Command;
34 import org.openhab.core.types.StateDescription;
35 import org.openhab.core.types.UnDefType;
37 import com.google.gson.JsonObject;
40 * The {@link HandlerColorController} is responsible for the Alexa.ColorTemperatureController
42 * @author Lukas Knoeller - Initial contribution
43 * @author Michael Geramb - Initial contribution
46 public class HandlerColorController extends HandlerBase {
48 public static final String INTERFACE = "Alexa.ColorController";
49 public static final String INTERFACE_COLOR_PROPERTIES = "Alexa.ColorPropertiesController";
52 private static final ChannelTypeUID CHANNEL_TYPE_COLOR_NAME = new ChannelTypeUID(
53 AmazonEchoControlBindingConstants.BINDING_ID, "colorName");
55 private static final ChannelTypeUID CHANNEL_TYPE_COLOR = new ChannelTypeUID(
56 AmazonEchoControlBindingConstants.BINDING_ID, "color");
58 // Channel and Properties
59 private static final ChannelInfo COLOR = new ChannelInfo("color" /* propertyName */, "color" /* ChannelId */,
60 CHANNEL_TYPE_COLOR /* Channel Type */, ITEM_TYPE_COLOR /* Item Type */);
62 private static final ChannelInfo COLOR_PROPERTIES = new ChannelInfo("colorProperties" /* propertyName */,
63 "colorName" /* ChannelId */, CHANNEL_TYPE_COLOR_NAME /* Channel Type */, ITEM_TYPE_STRING /* Item Type */);
65 private @Nullable HSBType lastColor;
66 private @Nullable String lastColorName;
69 public String[] getSupportedInterface() {
70 return new String[] { INTERFACE, INTERFACE_COLOR_PROPERTIES };
74 protected ChannelInfo @Nullable [] findChannelInfos(SmartHomeCapability capability, String property) {
75 if (COLOR.propertyName.contentEquals(property)) {
76 return new ChannelInfo[] { COLOR, COLOR_PROPERTIES };
82 public void updateChannels(String interfaceName, List<JsonObject> stateList, UpdateChannelResult result) {
83 if (INTERFACE.equals(interfaceName)) {
84 // WRITING TO THIS CHANNEL DOES CURRENTLY NOT WORK, BUT WE LEAVE THE CODE FOR FUTURE USE!
85 HSBType colorValue = null;
86 for (JsonObject state : stateList) {
87 if (COLOR.propertyName.equals(state.get("name").getAsString())) {
88 JsonObject value = state.get("value").getAsJsonObject();
89 // For groups take the maximum
90 if (colorValue == null) {
91 colorValue = new HSBType(new DecimalType(value.get("hue").getAsInt()),
92 new PercentType(value.get("saturation").getAsInt() * 100),
93 new PercentType(value.get("brightness").getAsInt() * 100));
97 if (colorValue != null) {
98 if (!colorValue.equals(lastColor)) {
99 result.needSingleUpdate = true;
100 lastColor = colorValue;
103 updateState(COLOR.channelId, colorValue == null ? UnDefType.UNDEF : colorValue);
105 if (INTERFACE_COLOR_PROPERTIES.equals(interfaceName)) {
106 String colorNameValue = null;
107 for (JsonObject state : stateList) {
108 if (COLOR_PROPERTIES.propertyName.equals(state.get("name").getAsString())) {
109 if (colorNameValue == null) {
110 result.needSingleUpdate = false;
111 colorNameValue = state.get("value").getAsJsonObject().get("name").getAsString();
115 if (colorNameValue == null && lastColorName != null) {
116 colorNameValue = lastColorName;
118 lastColorName = colorNameValue;
119 updateState(COLOR_PROPERTIES.channelId,
120 lastColorName == null ? UnDefType.UNDEF : new StringType(lastColorName));
125 public boolean handleCommand(Connection connection, SmartHomeDevice shd, String entityId,
126 SmartHomeCapability[] capabilties, String channelId, Command command)
127 throws IOException, InterruptedException {
128 if (channelId.equals(COLOR.channelId)) {
129 if (containsCapabilityProperty(capabilties, COLOR.propertyName)) {
130 if (command instanceof HSBType) {
131 HSBType color = ((HSBType) command);
132 JsonObject colorObject = new JsonObject();
133 colorObject.addProperty("hue", color.getHue());
134 colorObject.addProperty("saturation", color.getSaturation().floatValue() / 100);
135 colorObject.addProperty("brightness", color.getBrightness().floatValue() / 100);
136 connection.smartHomeCommand(entityId, "setColor", "value", colorObject);
140 if (channelId.equals(COLOR_PROPERTIES.channelId)) {
141 if (containsCapabilityProperty(capabilties, COLOR.propertyName)) {
142 if (command instanceof StringType) {
143 String colorName = command.toFullString();
144 if (!colorName.isEmpty()) {
145 lastColorName = colorName;
146 connection.smartHomeCommand(entityId, "setColor", "colorName", colorName);
156 public @Nullable StateDescription findStateDescription(String channelId, StateDescription originalStateDescription,
157 @Nullable Locale locale) {