]> git.basschouten.com Git - openhab-addons.git/blob
9d412c1a14ba39f1267a00cd62c921f17c0cd884
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.mqtt.homeassistant.internal.component;
14
15 import java.math.BigDecimal;
16 import java.util.Objects;
17
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;
35
36 /**
37  * A MQTT light, following the https://www.home-assistant.io/components/light.mqtt/ specification.
38  *
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.
41  *
42  * @author Cody Cutrer - Initial contribution
43  */
44 @NonNullByDefault
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";
52
53     protected @Nullable ComponentChannel hsChannel;
54     protected @Nullable ComponentChannel rgbChannel;
55     protected @Nullable ComponentChannel xyChannel;
56
57     public DefaultSchemaLight(ComponentFactory.ComponentConfiguration builder, boolean newStyleChannels) {
58         super(builder, newStyleChannels);
59     }
60
61     @Override
62     protected void buildChannels() {
63         ComponentChannel localOnOffChannel;
64         localOnOffChannel = onOffChannel = buildChannel(ON_OFF_CHANNEL_ID, ComponentChannelType.SWITCH, onOffValue,
65                 "On/Off State", this)
66                 .stateTopic(channelConfiguration.stateTopic, channelConfiguration.stateValueTemplate)
67                 .commandTopic(channelConfiguration.commandTopic, channelConfiguration.isRetain(),
68                         channelConfiguration.getQos())
69                 .commandFilter(this::handleRawOnOffCommand).build(false);
70
71         @Nullable
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);
80         }
81
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();
88         }
89
90         if (channelConfiguration.colorModeStateTopic != null) {
91             buildChannel(COLOR_MODE_CHANNEL_ID, ComponentChannelType.STRING, new TextValue(), "Current color mode",
92                     this)
93                     .stateTopic(channelConfiguration.colorModeStateTopic, channelConfiguration.colorModeValueTemplate)
94                     .build();
95         }
96
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())
102                     .build();
103         }
104
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())
112                     .build();
113         }
114
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())
122                     .build(false));
123         }
124
125         if (channelConfiguration.rgbwStateTopic != null || channelConfiguration.rgbwCommandTopic != null) {
126             hasColorChannel = true;
127             hiddenChannels
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())
132                             .build(false));
133         }
134
135         if (channelConfiguration.rgbwwStateTopic != null || channelConfiguration.rgbwwCommandTopic != null) {
136             hasColorChannel = true;
137             hiddenChannels.add(
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())
142                             .build(false));
143         }
144
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())
152                     .build(false));
153         }
154
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())
162                     .build(false));
163         }
164
165         if (hasColorChannel) {
166             hiddenChannels.add(localOnOffChannel);
167             if (localBrightnessChannel != null) {
168                 hiddenChannels.add(localBrightnessChannel);
169             }
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);
176         } else {
177             channels.put(ON_OFF_CHANNEL_ID, localOnOffChannel);
178         }
179     }
180
181     // all handle*Command methods return false if they've been handled,
182     // or true if default handling should continue
183
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);
192             } else {
193                 brightnessChannel.getState().publishValue((Command) brightnessValue.getChannelState());
194             }
195             return false;
196         }
197
198         return true;
199     }
200
201     // The helper method the other commandFilters call
202     private boolean handleOnOffCommand(Command command) {
203         if (!handleRawOnOffCommand(command)) {
204             return false;
205         }
206
207         // OnOffType commands to go the regular command topic
208         if (command instanceof OnOffType) {
209             onOffChannel.getState().publishValue(command);
210             return false;
211         }
212
213         boolean needsOn = !onOffValue.getChannelState().equals(OnOffType.ON);
214         if (command.equals(PercentType.ZERO) || command.equals(HSBType.BLACK)) {
215             needsOn = false;
216         }
217         if (needsOn) {
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
222             }
223         }
224         return true;
225     }
226
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);
231     }
232
233     private boolean handleColorCommand(Command command) {
234         if (!handleOnOffCommand(command)) {
235             return false;
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
239                 // but don't choke
240                 if (channelConfiguration.brightnessCommandTopic != null) {
241                     brightnessChannel.getState().publishValue(color.getBrightness());
242                 }
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) {
248                 // TODO
249                 // } else if (channelConfiguration.rgbwwCommandTopic != null) {
250                 // TODO
251             } else if (channelConfiguration.xyCommandTopic != null) {
252                 PercentType[] xy = color.toXY();
253                 // If we don't have a brightness channel, something is probably busted
254                 // but don't choke
255                 if (channelConfiguration.brightnessCommandTopic != null) {
256                     brightnessChannel.getState().publishValue(color.getBrightness());
257                 }
258                 String xyString = String.format("%f,%f", xy[0].doubleValue(), xy[1].doubleValue());
259                 xyChannel.getState().publishValue(new StringType(xyString));
260             }
261         } else if (command instanceof PercentType brightness) {
262             if (channelConfiguration.brightnessCommandTopic != null) {
263                 brightnessChannel.getState().publishValue(command);
264             } else {
265                 // No brightness command topic?! must be RGB only
266                 // so re-calculatate
267                 State color = colorValue.getChannelState();
268                 if (color instanceof UnDefType) {
269                     color = HSBType.WHITE;
270                 }
271                 HSBType existingColor = (HSBType) color;
272                 HSBType newCommand = new HSBType(existingColor.getHue(), existingColor.getSaturation(), brightness);
273                 // re-process
274                 handleColorCommand(newCommand);
275             }
276         }
277         return false;
278     }
279
280     @Override
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()
288                             : HSBType.WHITE;
289                     if (state.equals(OnOffType.ON)) {
290                         colorValue.update(newOnState);
291                     }
292
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);
298                 } else {
299                     listener.updateChannelState(channel, state);
300                 }
301                 return;
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()));
309                     } else {
310                         colorValue.update(new HSBType(DecimalType.ZERO, PercentType.ZERO,
311                                 (PercentType) brightnessValue.getChannelState()));
312                     }
313                     listener.updateChannelState(buildChannelUID(COLOR_CHANNEL_ID), colorValue.getChannelState());
314                 } else {
315                     listener.updateChannelState(channel, state);
316                 }
317                 return;
318             case COLOR_TEMP_CHANNEL_ID:
319             case EFFECT_CHANNEL_ID:
320                 // Real channels; pass through
321                 listener.updateChannelState(channel, state);
322                 return;
323             case HS_CHANNEL_ID:
324             case XY_CHANNEL_ID:
325                 if (brightnessValue.getChannelState() instanceof UnDefType) {
326                     brightnessValue.update(PercentType.HUNDRED);
327                 }
328                 String[] split = state.toString().split(",");
329                 if (split.length != 2) {
330                     throw new IllegalArgumentException(state.toString() + " is not a valid string syntax");
331                 }
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));
337                 } else {
338                     HSBType xyColor = HSBType.fromXY(x, y);
339                     colorValue.update(new HSBType(xyColor.getHue(), xyColor.getSaturation(), brightness));
340                 }
341                 listener.updateChannelState(buildChannelUID(COLOR_CHANNEL_ID), colorValue.getChannelState());
342                 return;
343             case RGB_CHANNEL_ID:
344                 colorValue.update((HSBType) state);
345                 listener.updateChannelState(buildChannelUID(COLOR_CHANNEL_ID), colorValue.getChannelState());
346                 break;
347             case RGBW_CHANNEL_ID:
348             case RGBWW_CHANNEL_ID:
349                 // TODO: update color value
350                 break;
351         }
352     }
353 }