]> git.basschouten.com Git - openhab-addons.git/blob
4d1bc3f107d83823a8790748eb610ea7774de6d0
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.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;
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     @Override
70     public String[] getSupportedInterface() {
71         return new String[] { INTERFACE, INTERFACE_COLOR_PROPERTIES };
72     }
73
74     @Override
75     protected ChannelInfo @Nullable [] findChannelInfos(SmartHomeCapability capability, String property) {
76         if (COLOR.propertyName.contentEquals(property)) {
77             return new ChannelInfo[] { COLOR, COLOR_PROPERTIES };
78         }
79         return null;
80     }
81
82     @Override
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));
95                     }
96                 }
97             }
98             if (colorValue != null) {
99                 if (!colorValue.equals(lastColor)) {
100                     result.needSingleUpdate = true;
101                     lastColor = colorValue;
102                 }
103             }
104             updateState(COLOR.channelId, colorValue == null ? UnDefType.UNDEF : colorValue);
105         }
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();
113                     }
114                 }
115             }
116             if (lastColorName == null) {
117                 lastColorName = colorNameValue;
118             } else if (colorNameValue == null && lastColorName != null) {
119                 colorNameValue = lastColorName;
120             }
121             updateState(COLOR_PROPERTIES.channelId,
122                     lastColorName == null ? UnDefType.UNDEF : new StringType(lastColorName));
123         }
124     }
125
126     @Override
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);
138                 }
139             }
140         }
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);
148                         return true;
149                     }
150                 }
151             }
152         }
153         return false;
154     }
155
156     @Override
157     public @Nullable StateDescription findStateDescription(String channelId, StateDescription originalStateDescription,
158             @Nullable Locale locale) {
159         return null;
160     }
161 }