2 * Copyright (c) 2010-2020 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;
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.core.io.transport.mqtt.MqttBrokerConnection;
26 import org.openhab.core.thing.ChannelUID;
27 import org.openhab.core.types.Command;
28 import org.openhab.core.types.State;
31 * A MQTT light, following the https://www.home-assistant.io/components/light.mqtt/ specification.
33 * This class condenses the three state/command topics (for ON/OFF, Brightness, Color) to one
36 * @author David Graeff - Initial contribution
39 public class ComponentLight extends AbstractComponent<ComponentLight.ChannelConfiguration>
40 implements ChannelStateUpdateListener {
41 public static final String switchChannelID = "light"; // Randomly chosen channel "ID"
42 public static final String brightnessChannelID = "brightness"; // Randomly chosen channel "ID"
43 public static final String colorChannelID = "color"; // Randomly chosen channel "ID"
46 * Configuration class for MQTT component
48 static class ChannelConfiguration extends BaseChannelConfiguration {
49 ChannelConfiguration() {
53 protected int brightness_scale = 255;
54 protected boolean optimistic = false;
55 protected @Nullable List<String> effect_list;
57 // Defines when on the payload_on is sent. Using last (the default) will send any style (brightness, color, etc)
58 // topics first and then a payload_on to the command_topic. Using first will send the payload_on and then any
59 // style topics. Using brightness will only send brightness commands instead of the payload_on to turn the light
61 protected String on_command_type = "last";
63 protected @Nullable String state_topic;
64 protected @Nullable String command_topic;
65 protected @Nullable String state_value_template;
67 protected @Nullable String brightness_state_topic;
68 protected @Nullable String brightness_command_topic;
69 protected @Nullable String brightness_value_template;
71 protected @Nullable String color_temp_state_topic;
72 protected @Nullable String color_temp_command_topic;
73 protected @Nullable String color_temp_value_template;
75 protected @Nullable String effect_command_topic;
76 protected @Nullable String effect_state_topic;
77 protected @Nullable String effect_value_template;
79 protected @Nullable String rgb_command_topic;
80 protected @Nullable String rgb_state_topic;
81 protected @Nullable String rgb_value_template;
82 protected @Nullable String rgb_command_template;
84 protected @Nullable String white_value_command_topic;
85 protected @Nullable String white_value_state_topic;
86 protected @Nullable String white_value_template;
88 protected @Nullable String xy_command_topic;
89 protected @Nullable String xy_state_topic;
90 protected @Nullable String xy_value_template;
92 protected String payload_on = "ON";
93 protected String payload_off = "OFF";
96 protected CChannel colorChannel;
97 protected CChannel switchChannel;
98 protected CChannel brightnessChannel;
99 private final @Nullable ChannelStateUpdateListener channelStateUpdateListener;
101 public ComponentLight(CFactory.ComponentConfiguration builder) {
102 super(builder, ChannelConfiguration.class);
103 this.channelStateUpdateListener = builder.getUpdateListener();
104 ColorValue value = new ColorValue(ColorMode.RGB, channelConfiguration.payload_on,
105 channelConfiguration.payload_off, 100);
107 // Create three MQTT subscriptions and use this class object as update listener
108 switchChannel = buildChannel(switchChannelID, value, channelConfiguration.name, this)//
109 // Some lights use the value_template field for the template, most use state_value_template
110 .stateTopic(channelConfiguration.state_topic, channelConfiguration.state_value_template,
111 channelConfiguration.value_template)//
112 .commandTopic(channelConfiguration.command_topic, channelConfiguration.retain)//
115 colorChannel = buildChannel(colorChannelID, value, channelConfiguration.name, this)//
116 .stateTopic(channelConfiguration.rgb_state_topic, channelConfiguration.rgb_value_template)//
117 .commandTopic(channelConfiguration.rgb_command_topic, channelConfiguration.retain)//
120 brightnessChannel = buildChannel(brightnessChannelID, value, channelConfiguration.name, this)//
121 .stateTopic(channelConfiguration.brightness_state_topic, channelConfiguration.brightness_value_template)//
122 .commandTopic(channelConfiguration.brightness_command_topic, channelConfiguration.retain)//
125 channels.put(colorChannelID, colorChannel);
129 public CompletableFuture<@Nullable Void> start(MqttBrokerConnection connection, ScheduledExecutorService scheduler,
131 return Stream.of(switchChannel, brightnessChannel, colorChannel) //
132 .map(v -> v.start(connection, scheduler, timeout)) //
133 .reduce(CompletableFuture.completedFuture(null), (f, v) -> f.thenCompose(b -> v));
137 public CompletableFuture<@Nullable Void> stop() {
138 return Stream.of(switchChannel, brightnessChannel, colorChannel) //
139 .map(v -> v.stop()) //
140 .reduce(CompletableFuture.completedFuture(null), (f, v) -> f.thenCompose(b -> v));
144 * Proxy method to condense all three MQTT subscriptions to one channel
147 public void updateChannelState(ChannelUID channelUID, State value) {
148 ChannelStateUpdateListener listener = channelStateUpdateListener;
149 if (listener != null) {
150 listener.updateChannelState(colorChannel.getChannelUID(), value);
155 * Proxy method to condense all three MQTT subscriptions to one channel
158 public void postChannelCommand(ChannelUID channelUID, Command value) {
159 ChannelStateUpdateListener listener = channelStateUpdateListener;
160 if (listener != null) {
161 listener.postChannelCommand(colorChannel.getChannelUID(), value);
166 * Proxy method to condense all three MQTT subscriptions to one channel
169 public void triggerChannel(ChannelUID channelUID, String eventPayload) {
170 ChannelStateUpdateListener listener = channelStateUpdateListener;
171 if (listener != null) {
172 listener.triggerChannel(colorChannel.getChannelUID(), eventPayload);