2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.wled.internal.handlers;
15 import static org.openhab.binding.wled.internal.WLedBindingConstants.*;
17 import java.math.BigDecimal;
18 import java.util.ArrayList;
19 import java.util.List;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.binding.wled.internal.WLedSegmentConfiguration;
23 import org.openhab.binding.wled.internal.api.ApiException;
24 import org.openhab.binding.wled.internal.api.WledApi;
25 import org.openhab.core.library.types.DecimalType;
26 import org.openhab.core.library.types.HSBType;
27 import org.openhab.core.library.types.IncreaseDecreaseType;
28 import org.openhab.core.library.types.OnOffType;
29 import org.openhab.core.library.types.PercentType;
30 import org.openhab.core.thing.Bridge;
31 import org.openhab.core.thing.Channel;
32 import org.openhab.core.thing.ChannelUID;
33 import org.openhab.core.thing.Thing;
34 import org.openhab.core.thing.ThingStatus;
35 import org.openhab.core.thing.ThingStatusDetail;
36 import org.openhab.core.thing.binding.BaseThingHandler;
37 import org.openhab.core.thing.binding.builder.ThingBuilder;
38 import org.openhab.core.types.Command;
39 import org.openhab.core.types.RefreshType;
40 import org.openhab.core.types.State;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * The {@link WLedSegmentHandler} is responsible for handling only a single segment from a WLED device.
47 * @author Matthew Skinner - Initial contribution
51 public class WLedSegmentHandler extends BaseThingHandler {
52 private final Logger logger = LoggerFactory.getLogger(getClass());
53 private WLedSegmentConfiguration config = new WLedSegmentConfiguration();
54 private BigDecimal masterBrightness255 = BigDecimal.ZERO;
55 private HSBType primaryColor = new HSBType();
56 private HSBType secondaryColor = new HSBType();
57 private HSBType thirdColor = new HSBType();
59 public WLedSegmentHandler(Thing thing) {
63 public void update(String channelID, State state) {
64 updateState(channelID, state);
67 private void removeWhiteChannels() {
68 List<Channel> removeChannels = new ArrayList<>();
69 Channel channel = getThing().getChannel(CHANNEL_PRIMARY_WHITE);
70 if (channel != null) {
71 removeChannels.add(channel);
73 channel = getThing().getChannel(CHANNEL_SECONDARY_WHITE);
74 if (channel != null) {
75 removeChannels.add(channel);
77 channel = getThing().getChannel(CHANNEL_THIRD_WHITE);
78 if (channel != null) {
79 removeChannels.add(channel);
81 removeChannels(removeChannels);
84 private void removeChannels(List<Channel> removeChannels) {
85 if (!removeChannels.isEmpty()) {
86 ThingBuilder thingBuilder = editThing();
87 thingBuilder.withoutChannels(removeChannels);
88 updateThing(thingBuilder.build());
93 public void handleCommand(ChannelUID channelUID, Command command) {
94 Bridge bridge = getBridge();
98 WLedBridgeHandler bridgeHandler = (WLedBridgeHandler) bridge.getHandler();
99 if (bridgeHandler == null) {
102 WledApi localApi = bridgeHandler.api;
103 if (localApi == null) {
106 if (command instanceof RefreshType) {
107 return;// no need to check for refresh below
109 logger.debug("command {} sent to {}", command, channelUID.getId());
111 switch (channelUID.getId()) {
112 case CHANNEL_SEGMENT_BRIGHTNESS:
113 if (command instanceof OnOffType) {
114 localApi.setMasterOn(OnOffType.ON.equals(command), config.segmentIndex);
115 } else if (command instanceof PercentType) {
116 if (PercentType.ZERO.equals(command)) {
117 localApi.setMasterOn(false, config.segmentIndex);
120 localApi.setMasterBrightness((PercentType) command, config.segmentIndex);
124 localApi.setMirror(OnOffType.ON.equals(command), config.segmentIndex);
126 case CHANNEL_SPACING:
127 if (command instanceof DecimalType) {
128 localApi.setSpacing(((DecimalType) command).intValue(), config.segmentIndex);
131 case CHANNEL_GROUPING:
132 if (command instanceof DecimalType) {
133 localApi.setGrouping(((DecimalType) command).intValue(), config.segmentIndex);
136 case CHANNEL_REVERSE:
137 localApi.setReverse(OnOffType.ON.equals(command), config.segmentIndex);
139 case CHANNEL_PRIMARY_WHITE:
140 if (command instanceof PercentType) {
141 localApi.sendGetRequest(
142 "/win&W=" + ((PercentType) command).toBigDecimal().multiply(BIG_DECIMAL_2_55));
145 case CHANNEL_SECONDARY_WHITE:
146 if (command instanceof PercentType) {
147 localApi.sendGetRequest(
148 "/win&W2=" + ((PercentType) command).toBigDecimal().multiply(BIG_DECIMAL_2_55));
151 case CHANNEL_MASTER_CONTROLS:
152 if (command instanceof OnOffType) {
153 if (OnOffType.ON.equals(command)) {
154 // global may be off, but we don't want to switch global off and affect other segments
155 localApi.setGlobalOn(true);
157 localApi.setMasterOn(OnOffType.ON.equals(command), config.segmentIndex);
158 } else if (command instanceof IncreaseDecreaseType) {
159 if (IncreaseDecreaseType.INCREASE.equals(command)) {
160 if (masterBrightness255.intValue() < 240) {
161 localApi.sendGetRequest("/win&TT=1000&A=~15"); // 255 divided by 15 = 17 levels
163 localApi.sendGetRequest("/win&TT=1000&A=255");
166 if (masterBrightness255.intValue() > 15) {
167 localApi.sendGetRequest("/win&TT=1000&A=~-15");
169 localApi.sendGetRequest("/win&TT=1000&A=0");
172 } else if (command instanceof HSBType) {
173 if ((((HSBType) command).getBrightness()).equals(PercentType.ZERO)) {
174 localApi.setMasterOn(false, config.segmentIndex);
177 localApi.setGlobalOn(true);
178 primaryColor = (HSBType) command;
179 if (primaryColor.getSaturation().intValue() < bridgeHandler.config.saturationThreshold
180 && bridgeHandler.hasWhite) {
181 localApi.setWhiteOnly((PercentType) command, config.segmentIndex);
182 } else if (primaryColor.getSaturation().intValue() == 32
183 && primaryColor.getHue().intValue() == 36 && bridgeHandler.hasWhite) {
184 localApi.setWhiteOnly((PercentType) command, config.segmentIndex);
186 localApi.setMasterHSB((HSBType) command, config.segmentIndex);
188 } else if (command instanceof PercentType) {
189 localApi.setMasterBrightness((PercentType) command, config.segmentIndex);
192 case CHANNEL_PRIMARY_COLOR:
193 if (command instanceof HSBType) {
194 primaryColor = (HSBType) command;
195 } else if (command instanceof PercentType) {
196 primaryColor = new HSBType(primaryColor.getHue(), primaryColor.getSaturation(),
197 ((PercentType) command));
199 localApi.setPrimaryColor(primaryColor, config.segmentIndex);
201 case CHANNEL_SECONDARY_COLOR:
202 if (command instanceof HSBType) {
203 secondaryColor = (HSBType) command;
204 } else if (command instanceof PercentType) {
205 secondaryColor = new HSBType(secondaryColor.getHue(), secondaryColor.getSaturation(),
206 ((PercentType) command));
208 localApi.setSecondaryColor(secondaryColor, config.segmentIndex);
210 case CHANNEL_THIRD_COLOR:
211 if (command instanceof HSBType) {
212 thirdColor = (HSBType) command;
213 } else if (command instanceof PercentType) {
214 thirdColor = new HSBType(thirdColor.getHue(), thirdColor.getSaturation(),
215 ((PercentType) command));
217 localApi.setTertiaryColor(thirdColor, config.segmentIndex);
219 case CHANNEL_PALETTES:
220 localApi.setPalette(command.toString(), config.segmentIndex);
223 localApi.setEffect(command.toString(), config.segmentIndex);
226 localApi.setFxSpeed((PercentType) command, config.segmentIndex);
228 case CHANNEL_INTENSITY:
229 localApi.setFxIntencity((PercentType) command, config.segmentIndex);
232 } catch (ApiException e) {
233 logger.debug("Exception occured:{}", e.getMessage());
238 public void initialize() {
239 config = getConfigAs(WLedSegmentConfiguration.class);
240 Bridge bridge = getBridge();
241 if (bridge == null) {
242 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No bridge is selected.");
244 WLedBridgeHandler localBridgeHandler = (WLedBridgeHandler) bridge.getHandler();
245 if (localBridgeHandler == null) {
246 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED);
249 WledApi localAPI = localBridgeHandler.api;
250 if (localAPI != null) {
251 updateStatus(ThingStatus.ONLINE);
252 localBridgeHandler.stateDescriptionProvider
253 .setStateOptions(new ChannelUID(getThing().getUID(), CHANNEL_FX), localAPI.getUpdatedFxList());
254 localBridgeHandler.stateDescriptionProvider.setStateOptions(
255 new ChannelUID(getThing().getUID(), CHANNEL_PALETTES), localAPI.getUpdatedPaletteList());
256 if (!localBridgeHandler.hasWhite) {
257 logger.debug("WLED is not setup to use RGBW, so removing un-needed white channels");
258 removeWhiteChannels();
261 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED);