]> git.basschouten.com Git - openhab-addons.git/blob
0de442d3deafb4d0c6dd6f8e46f7f47a85a55426
[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_NUMBER;
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.StringType;
30 import org.openhab.core.thing.type.ChannelTypeUID;
31 import org.openhab.core.types.Command;
32 import org.openhab.core.types.StateDescription;
33 import org.openhab.core.types.UnDefType;
34
35 import com.google.gson.JsonObject;
36
37 /**
38  * The {@link HandlerColorTemperatureController} is responsible for the Alexa.ColorTemperatureController
39  *
40  * @author Lukas Knoeller - Initial contribution
41  * @author Michael Geramb - Initial contribution
42  */
43 @NonNullByDefault
44 public class HandlerColorTemperatureController extends HandlerBase {
45     // Interface
46     public static final String INTERFACE = "Alexa.ColorTemperatureController";
47     public static final String INTERFACE_COLOR_PROPERTIES = "Alexa.ColorPropertiesController";
48
49     // Channel types
50     private static final ChannelTypeUID CHANNEL_TYPE_COLOR_TEMPERATURE_NAME = new ChannelTypeUID(
51             AmazonEchoControlBindingConstants.BINDING_ID, "colorTemperatureName");
52
53     private static final ChannelTypeUID CHANNEL_TYPE_COLOR_TEPERATURE_IN_KELVIN = new ChannelTypeUID(
54             AmazonEchoControlBindingConstants.BINDING_ID, "colorTemperatureInKelvin");
55
56     // Channel and Properties
57     private static final ChannelInfo COLOR_TEMPERATURE_IN_KELVIN = new ChannelInfo(
58             "colorTemperatureInKelvin" /* propertyName */ , "colorTemperatureInKelvin" /* ChannelId */,
59             CHANNEL_TYPE_COLOR_TEPERATURE_IN_KELVIN /* Channel Type */ , ITEM_TYPE_NUMBER /* Item Type */);
60
61     private static final ChannelInfo COLOR_TEMPERATURE_NAME = new ChannelInfo("colorProperties" /* propertyName */ ,
62             "colorTemperatureName" /* ChannelId */, CHANNEL_TYPE_COLOR_TEMPERATURE_NAME /* Channel Type */ ,
63             ITEM_TYPE_STRING /* Item Type */);
64
65     private @Nullable Integer lastColorTemperature;
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_TEMPERATURE_IN_KELVIN.propertyName.contentEquals(property)) {
76             return new ChannelInfo[] { COLOR_TEMPERATURE_IN_KELVIN, COLOR_TEMPERATURE_NAME };
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             Integer colorTemperatureInKelvinValue = null;
85             for (JsonObject state : stateList) {
86                 if (COLOR_TEMPERATURE_IN_KELVIN.propertyName.equals(state.get("name").getAsString())) {
87                     int value = state.get("value").getAsInt();
88                     // For groups take the maximum
89                     if (colorTemperatureInKelvinValue == null) {
90                         colorTemperatureInKelvinValue = value;
91                     }
92                 }
93             }
94             if (colorTemperatureInKelvinValue != null && !colorTemperatureInKelvinValue.equals(lastColorTemperature)) {
95                 lastColorTemperature = colorTemperatureInKelvinValue;
96                 result.needSingleUpdate = true;
97             }
98             updateState(COLOR_TEMPERATURE_IN_KELVIN.channelId, colorTemperatureInKelvinValue == null ? UnDefType.UNDEF
99                     : new DecimalType(colorTemperatureInKelvinValue));
100         }
101         if (INTERFACE_COLOR_PROPERTIES.equals(interfaceName)) {
102             String colorTemperatureNameValue = null;
103             for (JsonObject state : stateList) {
104                 if (COLOR_TEMPERATURE_NAME.propertyName.equals(state.get("name").getAsString())) {
105                     if (colorTemperatureNameValue == null) {
106                         result.needSingleUpdate = false;
107                         colorTemperatureNameValue = state.get("value").getAsJsonObject().get("name").getAsString();
108                     }
109                 }
110             }
111             if (lastColorName == null) {
112                 lastColorName = colorTemperatureNameValue;
113             } else if (colorTemperatureNameValue == null && lastColorName != null) {
114                 colorTemperatureNameValue = lastColorName;
115             }
116             updateState(COLOR_TEMPERATURE_NAME.channelId,
117                     colorTemperatureNameValue == null ? UnDefType.UNDEF : new StringType(colorTemperatureNameValue));
118         }
119     }
120
121     @Override
122     public boolean handleCommand(Connection connection, SmartHomeDevice shd, String entityId,
123             SmartHomeCapability[] capabilties, String channelId, Command command)
124             throws IOException, InterruptedException {
125         if (channelId.equals(COLOR_TEMPERATURE_IN_KELVIN.channelId)) {
126             // WRITING TO THIS CHANNEL DOES CURRENTLY NOT WORK, BUT WE LEAVE THE CODE FOR FUTURE USE!
127             if (containsCapabilityProperty(capabilties, COLOR_TEMPERATURE_IN_KELVIN.propertyName)) {
128                 if (command instanceof DecimalType) {
129                     int intValue = ((DecimalType) command).intValue();
130                     if (intValue < 1000) {
131                         intValue = 1000;
132                     }
133                     if (intValue > 10000) {
134                         intValue = 10000;
135                     }
136                     connection.smartHomeCommand(entityId, "setColorTemperature", "colorTemperatureInKelvin", intValue);
137                     return true;
138                 }
139             }
140         }
141         if (channelId.equals(COLOR_TEMPERATURE_NAME.channelId)) {
142             if (containsCapabilityProperty(capabilties, COLOR_TEMPERATURE_IN_KELVIN.propertyName)) {
143                 if (command instanceof StringType) {
144                     String colorTemperatureName = command.toFullString();
145                     if (!colorTemperatureName.isEmpty()) {
146                         lastColorName = colorTemperatureName;
147                         connection.smartHomeCommand(entityId, "setColorTemperature", "colorTemperatureName",
148                                 colorTemperatureName);
149                         return true;
150                     }
151                 }
152             }
153         }
154         return false;
155     }
156
157     @Override
158     public @Nullable StateDescription findStateDescription(String channelUID, StateDescription originalStateDescription,
159             @Nullable Locale locale) {
160         return null;
161     }
162 }