2 * Copyright (c) 2010-2021 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.util.List;
16 import java.util.concurrent.CompletableFuture;
17 import java.util.concurrent.ScheduledExecutorService;
18 import java.util.stream.Stream;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.mqtt.generic.ChannelStateUpdateListener;
23 import org.openhab.binding.mqtt.generic.mapping.ColorMode;
24 import org.openhab.binding.mqtt.generic.values.ColorValue;
25 import org.openhab.binding.mqtt.homeassistant.internal.ComponentChannel;
26 import org.openhab.binding.mqtt.homeassistant.internal.config.dto.AbstractChannelConfiguration;
27 import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
28 import org.openhab.core.thing.ChannelUID;
29 import org.openhab.core.types.Command;
30 import org.openhab.core.types.State;
33 * A MQTT light, following the https://www.home-assistant.io/components/light.mqtt/ specification.
35 * This class condenses the three state/command topics (for ON/OFF, Brightness, Color) to one
38 * @author David Graeff - Initial contribution
41 public class Light extends AbstractComponent<Light.ChannelConfiguration> implements ChannelStateUpdateListener {
42 public static final String switchChannelID = "light"; // Randomly chosen channel "ID"
43 public static final String brightnessChannelID = "brightness"; // Randomly chosen channel "ID"
44 public static final String colorChannelID = "color"; // Randomly chosen channel "ID"
47 * Configuration class for MQTT component
49 static class ChannelConfiguration extends AbstractChannelConfiguration {
50 ChannelConfiguration() {
54 protected int brightness_scale = 255;
55 protected boolean optimistic = false;
56 protected @Nullable List<String> effect_list;
58 // Defines when on the payload_on is sent. Using last (the default) will send any style (brightness, color, etc)
59 // topics first and then a payload_on to the command_topic. Using first will send the payload_on and then any
60 // style topics. Using brightness will only send brightness commands instead of the payload_on to turn the light
62 protected String on_command_type = "last";
64 protected @Nullable String state_topic;
65 protected @Nullable String command_topic;
66 protected @Nullable String state_value_template;
68 protected @Nullable String brightness_state_topic;
69 protected @Nullable String brightness_command_topic;
70 protected @Nullable String brightness_value_template;
72 protected @Nullable String color_temp_state_topic;
73 protected @Nullable String color_temp_command_topic;
74 protected @Nullable String color_temp_value_template;
76 protected @Nullable String effect_command_topic;
77 protected @Nullable String effect_state_topic;
78 protected @Nullable String effect_value_template;
80 protected @Nullable String rgb_command_topic;
81 protected @Nullable String rgb_state_topic;
82 protected @Nullable String rgb_value_template;
83 protected @Nullable String rgb_command_template;
85 protected @Nullable String white_value_command_topic;
86 protected @Nullable String white_value_state_topic;
87 protected @Nullable String white_value_template;
89 protected @Nullable String xy_command_topic;
90 protected @Nullable String xy_state_topic;
91 protected @Nullable String xy_value_template;
93 protected String payload_on = "ON";
94 protected String payload_off = "OFF";
97 protected ComponentChannel colorChannel;
98 protected ComponentChannel switchChannel;
99 protected ComponentChannel brightnessChannel;
100 private final @Nullable ChannelStateUpdateListener channelStateUpdateListener;
102 public Light(ComponentFactory.ComponentConfiguration builder) {
103 super(builder, ChannelConfiguration.class);
104 this.channelStateUpdateListener = builder.getUpdateListener();
105 ColorValue value = new ColorValue(ColorMode.RGB, channelConfiguration.payload_on,
106 channelConfiguration.payload_off, 100);
108 // Create three MQTT subscriptions and use this class object as update listener
109 switchChannel = buildChannel(switchChannelID, value, channelConfiguration.getName(), this)
110 .stateTopic(channelConfiguration.state_topic, channelConfiguration.state_value_template,
111 channelConfiguration.getValueTemplate())
112 .commandTopic(channelConfiguration.command_topic, channelConfiguration.isRetain(),
113 channelConfiguration.getQos())
116 colorChannel = buildChannel(colorChannelID, value, channelConfiguration.getName(), this)
117 .stateTopic(channelConfiguration.rgb_state_topic, channelConfiguration.rgb_value_template)
118 .commandTopic(channelConfiguration.rgb_command_topic, channelConfiguration.isRetain(),
119 channelConfiguration.getQos())
122 brightnessChannel = buildChannel(brightnessChannelID, value, channelConfiguration.getName(), this)
123 .stateTopic(channelConfiguration.brightness_state_topic, channelConfiguration.brightness_value_template)
124 .commandTopic(channelConfiguration.brightness_command_topic, channelConfiguration.isRetain(),
125 channelConfiguration.getQos())
128 channels.put(colorChannelID, colorChannel);
132 public CompletableFuture<@Nullable Void> start(MqttBrokerConnection connection, ScheduledExecutorService scheduler,
134 return Stream.of(switchChannel, brightnessChannel, colorChannel) //
135 .map(v -> v.start(connection, scheduler, timeout)) //
136 .reduce(CompletableFuture.completedFuture(null), (f, v) -> f.thenCompose(b -> v));
140 public CompletableFuture<@Nullable Void> stop() {
141 return Stream.of(switchChannel, brightnessChannel, colorChannel) //
142 .map(v -> v.stop()) //
143 .reduce(CompletableFuture.completedFuture(null), (f, v) -> f.thenCompose(b -> v));
147 * Proxy method to condense all three MQTT subscriptions to one channel
150 public void updateChannelState(ChannelUID channelUID, State value) {
151 ChannelStateUpdateListener listener = channelStateUpdateListener;
152 if (listener != null) {
153 listener.updateChannelState(colorChannel.getChannelUID(), value);
158 * Proxy method to condense all three MQTT subscriptions to one channel
161 public void postChannelCommand(ChannelUID channelUID, Command value) {
162 ChannelStateUpdateListener listener = channelStateUpdateListener;
163 if (listener != null) {
164 listener.postChannelCommand(colorChannel.getChannelUID(), value);
169 * Proxy method to condense all three MQTT subscriptions to one channel
172 public void triggerChannel(ChannelUID channelUID, String eventPayload) {
173 ChannelStateUpdateListener listener = channelStateUpdateListener;
174 if (listener != null) {
175 listener.triggerChannel(colorChannel.getChannelUID(), eventPayload);