]> git.basschouten.com Git - openhab-addons.git/blob
55d0b310e6c978044c99b0753602943c4df37bbf
[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.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.core.library.types.DecimalType;
26 import org.openhab.core.library.types.HSBType;
27 import org.openhab.core.library.types.OnOffType;
28 import org.openhab.core.library.types.PercentType;
29 import org.openhab.core.library.types.StringType;
30 import org.openhab.core.thing.ChannelUID;
31 import org.openhab.core.types.Command;
32 import org.openhab.core.types.State;
33 import org.openhab.core.types.UnDefType;
34
35 /**
36  * A MQTT light, following the https://www.home-assistant.io/components/light.mqtt/ specification.
37  *
38  * Specifically, the default schema. This class will present a single channel for color, brightness,
39  * or on/off as appropriate. Additional attributes are still exposed as dedicated channels.
40  *
41  * @author Cody Cutrer - Initial contribution
42  */
43 @NonNullByDefault
44 public class DefaultSchemaLight extends Light {
45     protected static final String HS_CHANNEL_ID = "hs";
46     protected static final String RGB_CHANNEL_ID = "rgb";
47     protected static final String RGBW_CHANNEL_ID = "rgbw";
48     protected static final String RGBWW_CHANNEL_ID = "rgbww";
49     protected static final String XY_CHANNEL_ID = "xy";
50     protected static final String WHITE_CHANNEL_ID = "white";
51
52     protected @Nullable ComponentChannel hsChannel;
53     protected @Nullable ComponentChannel rgbChannel;
54     protected @Nullable ComponentChannel xyChannel;
55
56     public DefaultSchemaLight(ComponentFactory.ComponentConfiguration builder) {
57         super(builder);
58     }
59
60     @Override
61     protected void buildChannels() {
62         ComponentChannel localOnOffChannel;
63         localOnOffChannel = onOffChannel = buildChannel(ON_OFF_CHANNEL_ID, onOffValue, "On/Off State", this)
64                 .stateTopic(channelConfiguration.stateTopic, channelConfiguration.stateValueTemplate)
65                 .commandTopic(channelConfiguration.commandTopic, channelConfiguration.isRetain(),
66                         channelConfiguration.getQos())
67                 .commandFilter(this::handleRawOnOffCommand).build(false);
68
69         @Nullable
70         ComponentChannel localBrightnessChannel = null;
71         if (channelConfiguration.brightnessStateTopic != null || channelConfiguration.brightnessCommandTopic != null) {
72             localBrightnessChannel = brightnessChannel = buildChannel(BRIGHTNESS_CHANNEL_ID, brightnessValue,
73                     "Brightness", this)
74                     .stateTopic(channelConfiguration.brightnessStateTopic, channelConfiguration.brightnessValueTemplate)
75                     .commandTopic(channelConfiguration.brightnessCommandTopic, channelConfiguration.isRetain(),
76                             channelConfiguration.getQos())
77                     .withFormat("%.0f").commandFilter(this::handleBrightnessCommand).build(false);
78         }
79
80         if (channelConfiguration.whiteCommandTopic != null) {
81             buildChannel(WHITE_CHANNEL_ID, brightnessValue, "Go directly to white of a specific brightness", this)
82                     .commandTopic(channelConfiguration.whiteCommandTopic, channelConfiguration.isRetain(),
83                             channelConfiguration.getQos())
84                     .isAdvanced(true).build();
85         }
86
87         if (channelConfiguration.colorModeStateTopic != null) {
88             buildChannel(COLOR_MODE_CHANNEL_ID, new TextValue(), "Current color mode", this)
89                     .stateTopic(channelConfiguration.colorModeStateTopic, channelConfiguration.colorModeValueTemplate)
90                     .build();
91         }
92
93         if (channelConfiguration.colorTempStateTopic != null || channelConfiguration.colorTempCommandTopic != null) {
94             buildChannel(COLOR_TEMP_CHANNEL_ID, colorTempValue, "Color Temperature", this)
95                     .stateTopic(channelConfiguration.colorTempStateTopic, channelConfiguration.colorTempValueTemplate)
96                     .commandTopic(channelConfiguration.colorTempCommandTopic, channelConfiguration.isRetain(),
97                             channelConfiguration.getQos())
98                     .build();
99         }
100
101         if (channelConfiguration.effectStateTopic != null || channelConfiguration.effectCommandTopic != null) {
102             buildChannel(EFFECT_CHANNEL_ID, effectValue, "Lighting effect", this)
103                     .stateTopic(channelConfiguration.effectStateTopic, channelConfiguration.effectValueTemplate)
104                     .commandTopic(channelConfiguration.effectCommandTopic, channelConfiguration.isRetain(),
105                             channelConfiguration.getQos())
106                     .build();
107         }
108
109         if (channelConfiguration.rgbStateTopic != null || channelConfiguration.rgbCommandTopic != null) {
110             hasColorChannel = true;
111             hiddenChannels.add(rgbChannel = buildChannel(RGB_CHANNEL_ID, new ColorValue(ColorMode.RGB, null, null, 100),
112                     "RGB state", this)
113                     .stateTopic(channelConfiguration.rgbStateTopic, channelConfiguration.rgbValueTemplate)
114                     .commandTopic(channelConfiguration.rgbCommandTopic, channelConfiguration.isRetain(),
115                             channelConfiguration.getQos())
116                     .build(false));
117         }
118
119         if (channelConfiguration.rgbwStateTopic != null || channelConfiguration.rgbwCommandTopic != null) {
120             hasColorChannel = true;
121             hiddenChannels.add(buildChannel(RGBW_CHANNEL_ID, new TextValue(), "RGBW state", this)
122                     .stateTopic(channelConfiguration.rgbwStateTopic, channelConfiguration.rgbwValueTemplate)
123                     .commandTopic(channelConfiguration.rgbwCommandTopic, channelConfiguration.isRetain(),
124                             channelConfiguration.getQos())
125                     .build(false));
126         }
127
128         if (channelConfiguration.rgbwwStateTopic != null || channelConfiguration.rgbwwCommandTopic != null) {
129             hasColorChannel = true;
130             hiddenChannels.add(buildChannel(RGBWW_CHANNEL_ID, new TextValue(), "RGBWW state", this)
131                     .stateTopic(channelConfiguration.rgbwwStateTopic, channelConfiguration.rgbwwValueTemplate)
132                     .commandTopic(channelConfiguration.rgbwwCommandTopic, channelConfiguration.isRetain(),
133                             channelConfiguration.getQos())
134                     .build(false));
135         }
136
137         if (channelConfiguration.xyStateTopic != null || channelConfiguration.xyCommandTopic != null) {
138             hasColorChannel = true;
139             hiddenChannels.add(
140                     xyChannel = buildChannel(XY_CHANNEL_ID, new ColorValue(ColorMode.XYY, null, null, 100), "XY State",
141                             this).stateTopic(channelConfiguration.xyStateTopic, channelConfiguration.xyValueTemplate)
142                             .commandTopic(channelConfiguration.xyCommandTopic, channelConfiguration.isRetain(),
143                                     channelConfiguration.getQos())
144                             .build(false));
145         }
146
147         if (channelConfiguration.hsStateTopic != null || channelConfiguration.hsCommandTopic != null) {
148             hasColorChannel = true;
149             hiddenChannels.add(this.hsChannel = buildChannel(HS_CHANNEL_ID, new TextValue(), "Hue and Saturation", this)
150                     .stateTopic(channelConfiguration.hsStateTopic, channelConfiguration.hsValueTemplate)
151                     .commandTopic(channelConfiguration.hsCommandTopic, channelConfiguration.isRetain(),
152                             channelConfiguration.getQos())
153                     .build(false));
154         }
155
156         if (hasColorChannel) {
157             hiddenChannels.add(localOnOffChannel);
158             if (localBrightnessChannel != null) {
159                 hiddenChannels.add(localBrightnessChannel);
160             }
161             buildChannel(COLOR_CHANNEL_ID, colorValue, "Color", this)
162                     .commandTopic(DUMMY_TOPIC, channelConfiguration.isRetain(), channelConfiguration.getQos())
163                     .commandFilter(this::handleColorCommand).build();
164         } else if (localBrightnessChannel != null) {
165             hiddenChannels.add(localOnOffChannel);
166             channels.put(BRIGHTNESS_CHANNEL_ID, localBrightnessChannel);
167         } else {
168             channels.put(ON_OFF_CHANNEL_ID, localOnOffChannel);
169         }
170     }
171
172     // all handle*Command methods return false if they've been handled,
173     // or true if default handling should continue
174
175     // The commandFilter for onOffChannel
176     private boolean handleRawOnOffCommand(Command command) {
177         // on_command_type of brightness is not allowed to send an actual on command
178         if (command.equals(OnOffType.ON) && channelConfiguration.onCommandType.equals(ON_COMMAND_TYPE_BRIGHTNESS)) {
179             // No prior state (or explicit off); set to 100%
180             if (brightnessValue.getChannelState() instanceof UnDefType
181                     || brightnessValue.getChannelState().equals(PercentType.ZERO)) {
182                 brightnessChannel.getState().publishValue(PercentType.HUNDRED);
183             } else {
184                 brightnessChannel.getState().publishValue((Command) brightnessValue.getChannelState());
185             }
186             return false;
187         }
188
189         return true;
190     }
191
192     // The helper method the other commandFilters call
193     private boolean handleOnOffCommand(Command command) {
194         if (!handleRawOnOffCommand(command)) {
195             return false;
196         }
197
198         // OnOffType commands to go the regular command topic
199         if (command instanceof OnOffType) {
200             onOffChannel.getState().publishValue(command);
201             return false;
202         }
203
204         boolean needsOn = !onOffValue.getChannelState().equals(OnOffType.ON);
205         if (command.equals(PercentType.ZERO) || command.equals(HSBType.BLACK)) {
206             needsOn = false;
207         }
208         if (needsOn) {
209             if (channelConfiguration.onCommandType.equals(ON_COMMAND_TYPE_FIRST)) {
210                 onOffChannel.getState().publishValue(OnOffType.ON);
211             } else if (channelConfiguration.onCommandType.equals(ON_COMMAND_TYPE_LAST)) {
212                 // TODO: schedule the ON publish for after this is sent
213             }
214         }
215         return true;
216     }
217
218     private boolean handleBrightnessCommand(Command command) {
219         // if it's OnOffType, it'll get handled by this; otherwise it'll return
220         // true and PercentType will be handled as normal
221         return handleOnOffCommand(command);
222     }
223
224     private boolean handleColorCommand(Command command) {
225         if (!handleOnOffCommand(command)) {
226             return false;
227         } else if (command instanceof HSBType) {
228             HSBType color = (HSBType) command;
229             if (channelConfiguration.hsCommandTopic != null) {
230                 // If we don't have a brightness channel, something is probably busted
231                 // but don't choke
232                 if (channelConfiguration.brightnessCommandTopic != null) {
233                     brightnessChannel.getState().publishValue(color.getBrightness());
234                 }
235                 String hs = String.format("%d,%d", color.getHue().intValue(), color.getSaturation().intValue());
236                 hsChannel.getState().publishValue(new StringType(hs));
237             } else if (channelConfiguration.rgbCommandTopic != null) {
238                 rgbChannel.getState().publishValue(command);
239                 // } else if (channelConfiguration.rgbwCommandTopic != null) {
240                 // TODO
241                 // } else if (channelConfiguration.rgbwwCommandTopic != null) {
242                 // TODO
243             } else if (channelConfiguration.xyCommandTopic != null) {
244                 PercentType[] xy = color.toXY();
245                 // If we don't have a brightness channel, something is probably busted
246                 // but don't choke
247                 if (channelConfiguration.brightnessCommandTopic != null) {
248                     brightnessChannel.getState().publishValue(color.getBrightness());
249                 }
250                 String xyString = String.format("%f,%f", xy[0].doubleValue(), xy[1].doubleValue());
251                 xyChannel.getState().publishValue(new StringType(xyString));
252             }
253         } else if (command instanceof PercentType) {
254             if (channelConfiguration.brightnessCommandTopic != null) {
255                 brightnessChannel.getState().publishValue(command);
256             } else {
257                 // No brightness command topic?! must be RGB only
258                 // so re-calculatate
259                 State color = colorValue.getChannelState();
260                 if (color instanceof UnDefType) {
261                     color = HSBType.WHITE;
262                 }
263                 HSBType existingColor = (HSBType) color;
264                 HSBType newCommand = new HSBType(existingColor.getHue(), existingColor.getSaturation(),
265                         (PercentType) command);
266                 // re-process
267                 handleColorCommand(newCommand);
268             }
269         }
270         return false;
271     }
272
273     @Override
274     public void updateChannelState(ChannelUID channel, State state) {
275         ChannelStateUpdateListener listener = this.channelStateUpdateListener;
276         switch (channel.getIdWithoutGroup()) {
277             case ON_OFF_CHANNEL_ID:
278                 if (hasColorChannel) {
279                     HSBType newOnState = colorValue.getChannelState() instanceof HSBType
280                             ? (HSBType) colorValue.getChannelState()
281                             : HSBType.WHITE;
282                     if (state.equals(OnOffType.ON)) {
283                         colorValue.update(newOnState);
284                     }
285
286                     listener.updateChannelState(new ChannelUID(getGroupUID(), COLOR_CHANNEL_ID),
287                             state.equals(OnOffType.ON) ? newOnState : HSBType.BLACK);
288                 } else if (brightnessChannel != null) {
289                     listener.updateChannelState(new ChannelUID(channel.getThingUID(), BRIGHTNESS_CHANNEL_ID),
290                             state.equals(OnOffType.ON) ? brightnessValue.getChannelState() : PercentType.ZERO);
291                 } else {
292                     listener.updateChannelState(channel, state);
293                 }
294                 return;
295             case BRIGHTNESS_CHANNEL_ID:
296                 onOffValue.update(Objects.requireNonNull(state.as(OnOffType.class)));
297                 if (hasColorChannel) {
298                     if (colorValue.getChannelState() instanceof HSBType) {
299                         HSBType hsb = (HSBType) (colorValue.getChannelState());
300                         colorValue.update(new HSBType(hsb.getHue(), hsb.getSaturation(),
301                                 (PercentType) brightnessValue.getChannelState()));
302                     } else {
303                         colorValue.update(new HSBType(DecimalType.ZERO, PercentType.ZERO,
304                                 (PercentType) brightnessValue.getChannelState()));
305                     }
306                     listener.updateChannelState(new ChannelUID(getGroupUID(), COLOR_CHANNEL_ID),
307                             colorValue.getChannelState());
308                 } else {
309                     listener.updateChannelState(channel, state);
310                 }
311                 return;
312             case COLOR_TEMP_CHANNEL_ID:
313             case EFFECT_CHANNEL_ID:
314                 // Real channels; pass through
315                 listener.updateChannelState(channel, state);
316                 return;
317             case HS_CHANNEL_ID:
318             case XY_CHANNEL_ID:
319                 if (brightnessValue.getChannelState() instanceof UnDefType) {
320                     brightnessValue.update(PercentType.HUNDRED);
321                 }
322                 String[] split = state.toString().split(",");
323                 if (split.length != 2) {
324                     throw new IllegalArgumentException(state.toString() + " is not a valid string syntax");
325                 }
326                 float x = Float.parseFloat(split[0]);
327                 float y = Float.parseFloat(split[1]);
328                 PercentType brightness = (PercentType) brightnessValue.getChannelState();
329                 if (channel.getIdWithoutGroup().equals(HS_CHANNEL_ID)) {
330                     colorValue.update(new HSBType(new DecimalType(x), new PercentType(new BigDecimal(y)), brightness));
331                 } else {
332                     HSBType xyColor = HSBType.fromXY(x, y);
333                     colorValue.update(new HSBType(xyColor.getHue(), xyColor.getSaturation(), brightness));
334                 }
335                 listener.updateChannelState(new ChannelUID(getGroupUID(), COLOR_CHANNEL_ID),
336                         colorValue.getChannelState());
337                 return;
338             case RGB_CHANNEL_ID:
339                 colorValue.update((HSBType) state);
340                 listener.updateChannelState(new ChannelUID(getGroupUID(), COLOR_CHANNEL_ID),
341                         colorValue.getChannelState());
342                 break;
343             case RGBW_CHANNEL_ID:
344             case RGBWW_CHANNEL_ID:
345                 // TODO: update color value
346                 break;
347         }
348     }
349 }