]> git.basschouten.com Git - openhab-addons.git/blob
8e462fd24fb803b86cf22ee27e68d39211c2b241
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.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;
36
37 import com.google.gson.JsonObject;
38
39 /**
40  * The {@link HandlerColorController} is responsible for the Alexa.ColorTemperatureController
41  *
42  * @author Lukas Knoeller - Initial contribution
43  * @author Michael Geramb - Initial contribution
44  */
45 @NonNullByDefault
46 public class HandlerColorController extends HandlerBase {
47     // Interface
48     public static final String INTERFACE = "Alexa.ColorController";
49     public static final String INTERFACE_COLOR_PROPERTIES = "Alexa.ColorPropertiesController";
50
51     // Channel types
52     private static final ChannelTypeUID CHANNEL_TYPE_COLOR_NAME = new ChannelTypeUID(
53             AmazonEchoControlBindingConstants.BINDING_ID, "colorName");
54
55     private static final ChannelTypeUID CHANNEL_TYPE_COLOR = new ChannelTypeUID(
56             AmazonEchoControlBindingConstants.BINDING_ID, "color");
57
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 */);
61
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 */);
64
65     private @Nullable HSBType lastColor;
66     private @Nullable String lastColorName;
67
68     @Override
69     public String[] getSupportedInterface() {
70         return new String[] { INTERFACE, INTERFACE_COLOR_PROPERTIES };
71     }
72
73     @Override
74     protected ChannelInfo @Nullable [] findChannelInfos(SmartHomeCapability capability, String property) {
75         if (COLOR.propertyName.contentEquals(property)) {
76             return new ChannelInfo[] { COLOR, COLOR_PROPERTIES };
77         }
78         return null;
79     }
80
81     @Override
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));
94                     }
95                 }
96             }
97             if (colorValue != null) {
98                 if (!colorValue.equals(lastColor)) {
99                     result.needSingleUpdate = true;
100                     lastColor = colorValue;
101                 }
102             }
103             updateState(COLOR.channelId, colorValue == null ? UnDefType.UNDEF : colorValue);
104         }
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();
112                     }
113                 }
114             }
115             if (colorNameValue == null && lastColorName != null) {
116                 colorNameValue = lastColorName;
117             }
118             lastColorName = colorNameValue;
119             updateState(COLOR_PROPERTIES.channelId,
120                     lastColorName == null ? UnDefType.UNDEF : new StringType(lastColorName));
121         }
122     }
123
124     @Override
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);
137                 }
138             }
139         }
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);
147                         return true;
148                     }
149                 }
150             }
151         }
152         return false;
153     }
154
155     @Override
156     public @Nullable StateDescription findStateDescription(String channelId, StateDescription originalStateDescription,
157             @Nullable Locale locale) {
158         return null;
159     }
160 }