]> git.basschouten.com Git - openhab-addons.git/blob
cf7f962c862d27aba586c7a1f391e3fc9ec76131
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.util.List;
16 import java.util.concurrent.CompletableFuture;
17 import java.util.concurrent.ScheduledExecutorService;
18 import java.util.stream.Stream;
19
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;
31
32 /**
33  * A MQTT light, following the https://www.home-assistant.io/components/light.mqtt/ specification.
34  *
35  * This class condenses the three state/command topics (for ON/OFF, Brightness, Color) to one
36  * color channel.
37  *
38  * @author David Graeff - Initial contribution
39  */
40 @NonNullByDefault
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"
45
46     /**
47      * Configuration class for MQTT component
48      */
49     static class ChannelConfiguration extends AbstractChannelConfiguration {
50         ChannelConfiguration() {
51             super("MQTT Light");
52         }
53
54         protected int brightness_scale = 255;
55         protected boolean optimistic = false;
56         protected @Nullable List<String> effect_list;
57
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
61         // on.
62         protected String on_command_type = "last";
63
64         protected @Nullable String state_topic;
65         protected @Nullable String command_topic;
66         protected @Nullable String state_value_template;
67
68         protected @Nullable String brightness_state_topic;
69         protected @Nullable String brightness_command_topic;
70         protected @Nullable String brightness_value_template;
71
72         protected @Nullable String color_temp_state_topic;
73         protected @Nullable String color_temp_command_topic;
74         protected @Nullable String color_temp_value_template;
75
76         protected @Nullable String effect_command_topic;
77         protected @Nullable String effect_state_topic;
78         protected @Nullable String effect_value_template;
79
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;
84
85         protected @Nullable String white_value_command_topic;
86         protected @Nullable String white_value_state_topic;
87         protected @Nullable String white_value_template;
88
89         protected @Nullable String xy_command_topic;
90         protected @Nullable String xy_state_topic;
91         protected @Nullable String xy_value_template;
92
93         protected String payload_on = "ON";
94         protected String payload_off = "OFF";
95     }
96
97     protected ComponentChannel colorChannel;
98     protected ComponentChannel switchChannel;
99     protected ComponentChannel brightnessChannel;
100     private final @Nullable ChannelStateUpdateListener channelStateUpdateListener;
101
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);
107
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())
114                 .build(false);
115
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())
120                 .build(false);
121
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())
126                 .build(false);
127
128         channels.put(colorChannelID, colorChannel);
129     }
130
131     @Override
132     public CompletableFuture<@Nullable Void> start(MqttBrokerConnection connection, ScheduledExecutorService scheduler,
133             int timeout) {
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));
137     }
138
139     @Override
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));
144     }
145
146     /**
147      * Proxy method to condense all three MQTT subscriptions to one channel
148      */
149     @Override
150     public void updateChannelState(ChannelUID channelUID, State value) {
151         ChannelStateUpdateListener listener = channelStateUpdateListener;
152         if (listener != null) {
153             listener.updateChannelState(colorChannel.getChannelUID(), value);
154         }
155     }
156
157     /**
158      * Proxy method to condense all three MQTT subscriptions to one channel
159      */
160     @Override
161     public void postChannelCommand(ChannelUID channelUID, Command value) {
162         ChannelStateUpdateListener listener = channelStateUpdateListener;
163         if (listener != null) {
164             listener.postChannelCommand(colorChannel.getChannelUID(), value);
165         }
166     }
167
168     /**
169      * Proxy method to condense all three MQTT subscriptions to one channel
170      */
171     @Override
172     public void triggerChannel(ChannelUID channelUID, String eventPayload) {
173         ChannelStateUpdateListener listener = channelStateUpdateListener;
174         if (listener != null) {
175             listener.triggerChannel(colorChannel.getChannelUID(), eventPayload);
176         }
177     }
178 }