2 * Copyright (c) 2010-2024 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.mqtt.homeassistant.internal.component;
15 import java.math.BigDecimal;
16 import java.util.Objects;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.mqtt.generic.ChannelStateUpdateListener;
21 import org.openhab.binding.mqtt.generic.mapping.ColorMode;
22 import org.openhab.binding.mqtt.generic.values.ColorValue;
23 import org.openhab.binding.mqtt.generic.values.TextValue;
24 import org.openhab.binding.mqtt.homeassistant.internal.ComponentChannel;
25 import org.openhab.binding.mqtt.homeassistant.internal.ComponentChannelType;
26 import org.openhab.core.library.types.DecimalType;
27 import org.openhab.core.library.types.HSBType;
28 import org.openhab.core.library.types.OnOffType;
29 import org.openhab.core.library.types.PercentType;
30 import org.openhab.core.library.types.StringType;
31 import org.openhab.core.thing.ChannelUID;
32 import org.openhab.core.types.Command;
33 import org.openhab.core.types.State;
34 import org.openhab.core.types.UnDefType;
37 * A MQTT light, following the https://www.home-assistant.io/components/light.mqtt/ specification.
39 * Specifically, the default schema. This class will present a single channel for color, brightness,
40 * or on/off as appropriate. Additional attributes are still exposed as dedicated channels.
42 * @author Cody Cutrer - Initial contribution
45 public class DefaultSchemaLight extends Light {
46 protected static final String HS_CHANNEL_ID = "hs";
47 protected static final String RGB_CHANNEL_ID = "rgb";
48 protected static final String RGBW_CHANNEL_ID = "rgbw";
49 protected static final String RGBWW_CHANNEL_ID = "rgbww";
50 protected static final String XY_CHANNEL_ID = "xy";
51 protected static final String WHITE_CHANNEL_ID = "white";
53 protected @Nullable ComponentChannel hsChannel;
54 protected @Nullable ComponentChannel rgbChannel;
55 protected @Nullable ComponentChannel xyChannel;
57 public DefaultSchemaLight(ComponentFactory.ComponentConfiguration builder, boolean newStyleChannels) {
58 super(builder, newStyleChannels);
62 protected void buildChannels() {
63 ComponentChannel localOnOffChannel;
64 localOnOffChannel = onOffChannel = buildChannel(ON_OFF_CHANNEL_ID, ComponentChannelType.SWITCH, onOffValue,
66 .stateTopic(channelConfiguration.stateTopic, channelConfiguration.stateValueTemplate)
67 .commandTopic(channelConfiguration.commandTopic, channelConfiguration.isRetain(),
68 channelConfiguration.getQos())
69 .commandFilter(this::handleRawOnOffCommand).build(false);
72 ComponentChannel localBrightnessChannel = null;
73 if (channelConfiguration.brightnessStateTopic != null || channelConfiguration.brightnessCommandTopic != null) {
74 localBrightnessChannel = brightnessChannel = buildChannel(BRIGHTNESS_CHANNEL_ID,
75 ComponentChannelType.DIMMER, brightnessValue, "Brightness", this)
76 .stateTopic(channelConfiguration.brightnessStateTopic, channelConfiguration.brightnessValueTemplate)
77 .commandTopic(channelConfiguration.brightnessCommandTopic, channelConfiguration.isRetain(),
78 channelConfiguration.getQos())
79 .withFormat("%.0f").commandFilter(this::handleBrightnessCommand).build(false);
82 if (channelConfiguration.whiteCommandTopic != null) {
83 buildChannel(WHITE_CHANNEL_ID, ComponentChannelType.DIMMER, brightnessValue,
84 "Go directly to white of a specific brightness", this)
85 .commandTopic(channelConfiguration.whiteCommandTopic, channelConfiguration.isRetain(),
86 channelConfiguration.getQos())
87 .isAdvanced(true).build();
90 if (channelConfiguration.colorModeStateTopic != null) {
91 buildChannel(COLOR_MODE_CHANNEL_ID, ComponentChannelType.STRING, new TextValue(), "Current color mode",
93 .stateTopic(channelConfiguration.colorModeStateTopic, channelConfiguration.colorModeValueTemplate)
97 if (channelConfiguration.colorTempStateTopic != null || channelConfiguration.colorTempCommandTopic != null) {
98 buildChannel(COLOR_TEMP_CHANNEL_ID, ComponentChannelType.NUMBER, colorTempValue, "Color Temperature", this)
99 .stateTopic(channelConfiguration.colorTempStateTopic, channelConfiguration.colorTempValueTemplate)
100 .commandTopic(channelConfiguration.colorTempCommandTopic, channelConfiguration.isRetain(),
101 channelConfiguration.getQos())
105 if (effectValue != null
106 && (channelConfiguration.effectStateTopic != null || channelConfiguration.effectCommandTopic != null)) {
107 buildChannel(EFFECT_CHANNEL_ID, ComponentChannelType.STRING, Objects.requireNonNull(effectValue),
108 "Lighting Effect", this)
109 .stateTopic(channelConfiguration.effectStateTopic, channelConfiguration.effectValueTemplate)
110 .commandTopic(channelConfiguration.effectCommandTopic, channelConfiguration.isRetain(),
111 channelConfiguration.getQos())
115 if (channelConfiguration.rgbStateTopic != null || channelConfiguration.rgbCommandTopic != null) {
116 hasColorChannel = true;
117 hiddenChannels.add(rgbChannel = buildChannel(RGB_CHANNEL_ID, ComponentChannelType.COLOR,
118 new ColorValue(ColorMode.RGB, null, null, 100), "RGB state", this)
119 .stateTopic(channelConfiguration.rgbStateTopic, channelConfiguration.rgbValueTemplate)
120 .commandTopic(channelConfiguration.rgbCommandTopic, channelConfiguration.isRetain(),
121 channelConfiguration.getQos())
125 if (channelConfiguration.rgbwStateTopic != null || channelConfiguration.rgbwCommandTopic != null) {
126 hasColorChannel = true;
128 .add(buildChannel(RGBW_CHANNEL_ID, ComponentChannelType.STRING, new TextValue(), "RGBW state", this)
129 .stateTopic(channelConfiguration.rgbwStateTopic, channelConfiguration.rgbwValueTemplate)
130 .commandTopic(channelConfiguration.rgbwCommandTopic, channelConfiguration.isRetain(),
131 channelConfiguration.getQos())
135 if (channelConfiguration.rgbwwStateTopic != null || channelConfiguration.rgbwwCommandTopic != null) {
136 hasColorChannel = true;
138 buildChannel(RGBWW_CHANNEL_ID, ComponentChannelType.STRING, new TextValue(), "RGBWW state", this)
139 .stateTopic(channelConfiguration.rgbwwStateTopic, channelConfiguration.rgbwwValueTemplate)
140 .commandTopic(channelConfiguration.rgbwwCommandTopic, channelConfiguration.isRetain(),
141 channelConfiguration.getQos())
145 if (channelConfiguration.xyStateTopic != null || channelConfiguration.xyCommandTopic != null) {
146 hasColorChannel = true;
147 hiddenChannels.add(xyChannel = buildChannel(XY_CHANNEL_ID, ComponentChannelType.COLOR,
148 new ColorValue(ColorMode.XYY, null, null, 100), "XY State", this)
149 .stateTopic(channelConfiguration.xyStateTopic, channelConfiguration.xyValueTemplate)
150 .commandTopic(channelConfiguration.xyCommandTopic, channelConfiguration.isRetain(),
151 channelConfiguration.getQos())
155 if (channelConfiguration.hsStateTopic != null || channelConfiguration.hsCommandTopic != null) {
156 hasColorChannel = true;
157 hiddenChannels.add(this.hsChannel = buildChannel(HS_CHANNEL_ID, ComponentChannelType.STRING,
158 new TextValue(), "Hue and Saturation", this)
159 .stateTopic(channelConfiguration.hsStateTopic, channelConfiguration.hsValueTemplate)
160 .commandTopic(channelConfiguration.hsCommandTopic, channelConfiguration.isRetain(),
161 channelConfiguration.getQos())
165 if (hasColorChannel) {
166 hiddenChannels.add(localOnOffChannel);
167 if (localBrightnessChannel != null) {
168 hiddenChannels.add(localBrightnessChannel);
170 buildChannel(COLOR_CHANNEL_ID, ComponentChannelType.COLOR, colorValue, "Color", this)
171 .commandTopic(DUMMY_TOPIC, channelConfiguration.isRetain(), channelConfiguration.getQos())
172 .commandFilter(this::handleColorCommand).build();
173 } else if (localBrightnessChannel != null) {
174 hiddenChannels.add(localOnOffChannel);
175 channels.put(BRIGHTNESS_CHANNEL_ID, localBrightnessChannel);
177 channels.put(ON_OFF_CHANNEL_ID, localOnOffChannel);
181 // all handle*Command methods return false if they've been handled,
182 // or true if default handling should continue
184 // The commandFilter for onOffChannel
185 private boolean handleRawOnOffCommand(Command command) {
186 // on_command_type of brightness is not allowed to send an actual on command
187 if (command.equals(OnOffType.ON) && channelConfiguration.onCommandType.equals(ON_COMMAND_TYPE_BRIGHTNESS)) {
188 // No prior state (or explicit off); set to 100%
189 if (brightnessValue.getChannelState() instanceof UnDefType
190 || brightnessValue.getChannelState().equals(PercentType.ZERO)) {
191 brightnessChannel.getState().publishValue(PercentType.HUNDRED);
193 brightnessChannel.getState().publishValue((Command) brightnessValue.getChannelState());
201 // The helper method the other commandFilters call
202 private boolean handleOnOffCommand(Command command) {
203 if (!handleRawOnOffCommand(command)) {
207 // OnOffType commands to go the regular command topic
208 if (command instanceof OnOffType) {
209 onOffChannel.getState().publishValue(command);
213 boolean needsOn = !onOffValue.getChannelState().equals(OnOffType.ON);
214 if (command.equals(PercentType.ZERO) || command.equals(HSBType.BLACK)) {
218 if (channelConfiguration.onCommandType.equals(ON_COMMAND_TYPE_FIRST)) {
219 onOffChannel.getState().publishValue(OnOffType.ON);
220 } else if (channelConfiguration.onCommandType.equals(ON_COMMAND_TYPE_LAST)) {
221 // TODO: schedule the ON publish for after this is sent
227 private boolean handleBrightnessCommand(Command command) {
228 // if it's OnOffType, it'll get handled by this; otherwise it'll return
229 // true and PercentType will be handled as normal
230 return handleOnOffCommand(command);
233 private boolean handleColorCommand(Command command) {
234 if (!handleOnOffCommand(command)) {
236 } else if (command instanceof HSBType color) {
237 if (channelConfiguration.hsCommandTopic != null) {
238 // If we don't have a brightness channel, something is probably busted
240 if (channelConfiguration.brightnessCommandTopic != null) {
241 brightnessChannel.getState().publishValue(color.getBrightness());
243 String hs = String.format("%d,%d", color.getHue().intValue(), color.getSaturation().intValue());
244 hsChannel.getState().publishValue(new StringType(hs));
245 } else if (channelConfiguration.rgbCommandTopic != null) {
246 rgbChannel.getState().publishValue(command);
247 // } else if (channelConfiguration.rgbwCommandTopic != null) {
249 // } else if (channelConfiguration.rgbwwCommandTopic != null) {
251 } else if (channelConfiguration.xyCommandTopic != null) {
252 PercentType[] xy = color.toXY();
253 // If we don't have a brightness channel, something is probably busted
255 if (channelConfiguration.brightnessCommandTopic != null) {
256 brightnessChannel.getState().publishValue(color.getBrightness());
258 String xyString = String.format("%f,%f", xy[0].doubleValue(), xy[1].doubleValue());
259 xyChannel.getState().publishValue(new StringType(xyString));
261 } else if (command instanceof PercentType brightness) {
262 if (channelConfiguration.brightnessCommandTopic != null) {
263 brightnessChannel.getState().publishValue(command);
265 // No brightness command topic?! must be RGB only
267 State color = colorValue.getChannelState();
268 if (color instanceof UnDefType) {
269 color = HSBType.WHITE;
271 HSBType existingColor = (HSBType) color;
272 HSBType newCommand = new HSBType(existingColor.getHue(), existingColor.getSaturation(), brightness);
274 handleColorCommand(newCommand);
281 public void updateChannelState(ChannelUID channel, State state) {
282 ChannelStateUpdateListener listener = this.channelStateUpdateListener;
283 switch (channel.getIdWithoutGroup()) {
284 case ON_OFF_CHANNEL_ID:
285 if (hasColorChannel) {
286 HSBType newOnState = colorValue.getChannelState() instanceof HSBType
287 ? (HSBType) colorValue.getChannelState()
289 if (state.equals(OnOffType.ON)) {
290 colorValue.update(newOnState);
293 listener.updateChannelState(buildChannelUID(COLOR_CHANNEL_ID),
294 state.equals(OnOffType.ON) ? newOnState : HSBType.BLACK);
295 } else if (brightnessChannel != null) {
296 listener.updateChannelState(new ChannelUID(channel.getThingUID(), BRIGHTNESS_CHANNEL_ID),
297 state.equals(OnOffType.ON) ? brightnessValue.getChannelState() : PercentType.ZERO);
299 listener.updateChannelState(channel, state);
302 case BRIGHTNESS_CHANNEL_ID:
303 onOffValue.update(Objects.requireNonNull(state.as(OnOffType.class)));
304 if (hasColorChannel) {
305 if (colorValue.getChannelState() instanceof HSBType) {
306 HSBType hsb = (HSBType) (colorValue.getChannelState());
307 colorValue.update(new HSBType(hsb.getHue(), hsb.getSaturation(),
308 (PercentType) brightnessValue.getChannelState()));
310 colorValue.update(new HSBType(DecimalType.ZERO, PercentType.ZERO,
311 (PercentType) brightnessValue.getChannelState()));
313 listener.updateChannelState(buildChannelUID(COLOR_CHANNEL_ID), colorValue.getChannelState());
315 listener.updateChannelState(channel, state);
318 case COLOR_TEMP_CHANNEL_ID:
319 case EFFECT_CHANNEL_ID:
320 // Real channels; pass through
321 listener.updateChannelState(channel, state);
325 if (brightnessValue.getChannelState() instanceof UnDefType) {
326 brightnessValue.update(PercentType.HUNDRED);
328 String[] split = state.toString().split(",");
329 if (split.length != 2) {
330 throw new IllegalArgumentException(state.toString() + " is not a valid string syntax");
332 float x = Float.parseFloat(split[0]);
333 float y = Float.parseFloat(split[1]);
334 PercentType brightness = (PercentType) brightnessValue.getChannelState();
335 if (channel.getIdWithoutGroup().equals(HS_CHANNEL_ID)) {
336 colorValue.update(new HSBType(new DecimalType(x), new PercentType(new BigDecimal(y)), brightness));
338 HSBType xyColor = HSBType.fromXY(x, y);
339 colorValue.update(new HSBType(xyColor.getHue(), xyColor.getSaturation(), brightness));
341 listener.updateChannelState(buildChannelUID(COLOR_CHANNEL_ID), colorValue.getChannelState());
344 colorValue.update((HSBType) state);
345 listener.updateChannelState(buildChannelUID(COLOR_CHANNEL_ID), colorValue.getChannelState());
347 case RGBW_CHANNEL_ID:
348 case RGBWW_CHANNEL_ID:
349 // TODO: update color value