]> git.basschouten.com Git - openhab-addons.git/blob
701d8629775e641f1be9fcad8031028f4d4cf7b1
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.amazonechocontrol.internal.smarthome;
14
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;
17
18 import java.io.IOException;
19 import java.util.List;
20 import java.util.Locale;
21
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;
37
38 import com.google.gson.JsonObject;
39
40 /**
41  * The {@link HandlerColorController} is responsible for the Alexa.ColorTemperatureController
42  *
43  * @author Lukas Knoeller - Initial contribution
44  * @author Michael Geramb - Initial contribution
45  */
46 @NonNullByDefault
47 public class HandlerColorController extends HandlerBase {
48     // Interface
49     public static final String INTERFACE = "Alexa.ColorController";
50     public static final String INTERFACE_COLOR_PROPERTIES = "Alexa.ColorPropertiesController";
51
52     // Channel types
53     private static final ChannelTypeUID CHANNEL_TYPE_COLOR_NAME = new ChannelTypeUID(
54             AmazonEchoControlBindingConstants.BINDING_ID, "colorName");
55
56     private static final ChannelTypeUID CHANNEL_TYPE_COLOR = new ChannelTypeUID(
57             AmazonEchoControlBindingConstants.BINDING_ID, "color");
58
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 */);
62
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 */);
65
66     private @Nullable HSBType lastColor;
67     private @Nullable String lastColorName;
68
69     public HandlerColorController(SmartHomeDeviceHandler smartHomeDeviceHandler) {
70         super(smartHomeDeviceHandler);
71     }
72
73     @Override
74     public String[] getSupportedInterface() {
75         return new String[] { INTERFACE, INTERFACE_COLOR_PROPERTIES };
76     }
77
78     @Override
79     protected ChannelInfo @Nullable [] findChannelInfos(SmartHomeCapability capability, String property) {
80         if (COLOR.propertyName.contentEquals(property)) {
81             return new ChannelInfo[] { COLOR, COLOR_PROPERTIES };
82         }
83         return null;
84     }
85
86     @Override
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));
99                     }
100                 }
101             }
102             if (colorValue != null) {
103                 if (!colorValue.equals(lastColor)) {
104                     result.needSingleUpdate = true;
105                     lastColor = colorValue;
106                 }
107             }
108             updateState(COLOR.channelId, colorValue == null ? UnDefType.UNDEF : colorValue);
109         }
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();
117                     }
118                 }
119             }
120             if (colorNameValue == null && lastColorName != null) {
121                 colorNameValue = lastColorName;
122             }
123             lastColorName = colorNameValue;
124             updateState(COLOR_PROPERTIES.channelId,
125                     lastColorName == null ? UnDefType.UNDEF : new StringType(lastColorName));
126         }
127     }
128
129     @Override
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);
142                 }
143             }
144         }
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);
152                         return true;
153                     }
154                 }
155             }
156         }
157         return false;
158     }
159
160     @Override
161     public @Nullable StateDescription findStateDescription(String channelId, StateDescription originalStateDescription,
162             @Nullable Locale locale) {
163         return null;
164     }
165 }