2 * Copyright (c) 2010-2023 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.handler.SmartHomeDeviceHandler;
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;
69 public HandlerColorController(SmartHomeDeviceHandler smartHomeDeviceHandler) {
70 super(smartHomeDeviceHandler);
74 public String[] getSupportedInterface() {
75 return new String[] { INTERFACE, INTERFACE_COLOR_PROPERTIES };
79 protected ChannelInfo @Nullable [] findChannelInfos(SmartHomeCapability capability, String property) {
80 if (COLOR.propertyName.contentEquals(property)) {
81 return new ChannelInfo[] { COLOR, COLOR_PROPERTIES };
87 public void updateChannels(String interfaceName, List<JsonObject> stateList, UpdateChannelResult result) {
88 if (INTERFACE.equals(interfaceName)) {
89 // WRITING TO THIS CHANNEL DOES CURRENTLY NOT WORK, BUT WE LEAVE THE CODE FOR FUTURE USE!
90 HSBType colorValue = null;
91 for (JsonObject state : stateList) {
92 if (COLOR.propertyName.equals(state.get("name").getAsString())) {
93 JsonObject value = state.get("value").getAsJsonObject();
94 // For groups take the maximum
95 if (colorValue == null) {
96 colorValue = new HSBType(new DecimalType(value.get("hue").getAsInt()),
97 new PercentType(value.get("saturation").getAsInt() * 100),
98 new PercentType(value.get("brightness").getAsInt() * 100));
102 if (colorValue != null) {
103 if (!colorValue.equals(lastColor)) {
104 result.needSingleUpdate = true;
105 lastColor = colorValue;
108 updateState(COLOR.channelId, colorValue == null ? UnDefType.UNDEF : colorValue);
110 if (INTERFACE_COLOR_PROPERTIES.equals(interfaceName)) {
111 String colorNameValue = null;
112 for (JsonObject state : stateList) {
113 if (COLOR_PROPERTIES.propertyName.equals(state.get("name").getAsString())) {
114 if (colorNameValue == null) {
115 result.needSingleUpdate = false;
116 colorNameValue = state.get("value").getAsJsonObject().get("name").getAsString();
120 if (colorNameValue == null && lastColorName != null) {
121 colorNameValue = lastColorName;
123 lastColorName = colorNameValue;
124 updateState(COLOR_PROPERTIES.channelId,
125 lastColorName == null ? UnDefType.UNDEF : new StringType(lastColorName));
130 public boolean handleCommand(Connection connection, SmartHomeDevice shd, String entityId,
131 List<SmartHomeCapability> capabilities, String channelId, Command command)
132 throws IOException, InterruptedException {
133 if (channelId.equals(COLOR.channelId)) {
134 if (containsCapabilityProperty(capabilities, COLOR.propertyName)) {
135 if (command instanceof HSBType) {
136 HSBType color = ((HSBType) command);
137 JsonObject colorObject = new JsonObject();
138 colorObject.addProperty("hue", color.getHue());
139 colorObject.addProperty("saturation", color.getSaturation().floatValue() / 100);
140 colorObject.addProperty("brightness", color.getBrightness().floatValue() / 100);
141 connection.smartHomeCommand(entityId, "setColor", "value", colorObject);
145 if (channelId.equals(COLOR_PROPERTIES.channelId)) {
146 if (containsCapabilityProperty(capabilities, COLOR.propertyName)) {
147 if (command instanceof StringType) {
148 String colorName = command.toFullString();
149 if (!colorName.isEmpty()) {
150 lastColorName = colorName;
151 connection.smartHomeCommand(entityId, "setColor", "colorName", colorName);
161 public @Nullable StateDescription findStateDescription(String channelId, StateDescription originalStateDescription,
162 @Nullable Locale locale) {