]> git.basschouten.com Git - openhab-addons.git/blob
e42ed29bbce4737b8a82ff3d396c62d6bfc1b7f5
[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;
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.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;
29
30 /**
31  * A MQTT light, following the https://www.home-assistant.io/components/light.mqtt/ specification.
32  *
33  * This class condenses the three state/command topics (for ON/OFF, Brightness, Color) to one
34  * color channel.
35  *
36  * @author David Graeff - Initial contribution
37  */
38 @NonNullByDefault
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"
44
45     /**
46      * Configuration class for MQTT component
47      */
48     static class ChannelConfiguration extends BaseChannelConfiguration {
49         ChannelConfiguration() {
50             super("MQTT Light");
51         }
52
53         protected int brightness_scale = 255;
54         protected boolean optimistic = false;
55         protected @Nullable List<String> effect_list;
56
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
60         // on.
61         protected String on_command_type = "last";
62
63         protected @Nullable String state_topic;
64         protected @Nullable String command_topic;
65         protected @Nullable String state_value_template;
66
67         protected @Nullable String brightness_state_topic;
68         protected @Nullable String brightness_command_topic;
69         protected @Nullable String brightness_value_template;
70
71         protected @Nullable String color_temp_state_topic;
72         protected @Nullable String color_temp_command_topic;
73         protected @Nullable String color_temp_value_template;
74
75         protected @Nullable String effect_command_topic;
76         protected @Nullable String effect_state_topic;
77         protected @Nullable String effect_value_template;
78
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;
83
84         protected @Nullable String white_value_command_topic;
85         protected @Nullable String white_value_state_topic;
86         protected @Nullable String white_value_template;
87
88         protected @Nullable String xy_command_topic;
89         protected @Nullable String xy_state_topic;
90         protected @Nullable String xy_value_template;
91
92         protected String payload_on = "ON";
93         protected String payload_off = "OFF";
94     }
95
96     protected CChannel colorChannel;
97     protected CChannel switchChannel;
98     protected CChannel brightnessChannel;
99     private final @Nullable ChannelStateUpdateListener channelStateUpdateListener;
100
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);
106
107         // Create three MQTT subscriptions and use this class object as update listener
108         switchChannel = buildChannel(switchChannelID, value, channelConfiguration.name, this)
109                 .stateTopic(channelConfiguration.state_topic, channelConfiguration.state_value_template,
110                         channelConfiguration.value_template)
111                 .commandTopic(channelConfiguration.command_topic, channelConfiguration.retain).build(false);
112
113         colorChannel = buildChannel(colorChannelID, value, channelConfiguration.name, this)
114                 .stateTopic(channelConfiguration.rgb_state_topic, channelConfiguration.rgb_value_template)
115                 .commandTopic(channelConfiguration.rgb_command_topic, channelConfiguration.retain).build(false);
116
117         brightnessChannel = buildChannel(brightnessChannelID, value, channelConfiguration.name, this)
118                 .stateTopic(channelConfiguration.brightness_state_topic, channelConfiguration.brightness_value_template)
119                 .commandTopic(channelConfiguration.brightness_command_topic, channelConfiguration.retain).build(false);
120
121         channels.put(colorChannelID, colorChannel);
122     }
123
124     @Override
125     public CompletableFuture<@Nullable Void> start(MqttBrokerConnection connection, ScheduledExecutorService scheduler,
126             int timeout) {
127         return Stream.of(switchChannel, brightnessChannel, colorChannel) //
128                 .map(v -> v.start(connection, scheduler, timeout)) //
129                 .reduce(CompletableFuture.completedFuture(null), (f, v) -> f.thenCompose(b -> v));
130     }
131
132     @Override
133     public CompletableFuture<@Nullable Void> stop() {
134         return Stream.of(switchChannel, brightnessChannel, colorChannel) //
135                 .map(v -> v.stop()) //
136                 .reduce(CompletableFuture.completedFuture(null), (f, v) -> f.thenCompose(b -> v));
137     }
138
139     /**
140      * Proxy method to condense all three MQTT subscriptions to one channel
141      */
142     @Override
143     public void updateChannelState(ChannelUID channelUID, State value) {
144         ChannelStateUpdateListener listener = channelStateUpdateListener;
145         if (listener != null) {
146             listener.updateChannelState(colorChannel.getChannelUID(), value);
147         }
148     }
149
150     /**
151      * Proxy method to condense all three MQTT subscriptions to one channel
152      */
153     @Override
154     public void postChannelCommand(ChannelUID channelUID, Command value) {
155         ChannelStateUpdateListener listener = channelStateUpdateListener;
156         if (listener != null) {
157             listener.postChannelCommand(colorChannel.getChannelUID(), value);
158         }
159     }
160
161     /**
162      * Proxy method to condense all three MQTT subscriptions to one channel
163      */
164     @Override
165     public void triggerChannel(ChannelUID channelUID, String eventPayload) {
166         ChannelStateUpdateListener listener = channelStateUpdateListener;
167         if (listener != null) {
168             listener.triggerChannel(colorChannel.getChannelUID(), eventPayload);
169         }
170     }
171 }