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