]> git.basschouten.com Git - openhab-addons.git/blob
c817539ffc601460463acbb950e8e5201cdc14dc
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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                 // 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)//
113                 .build(false);
114
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)//
118                 .build(false);
119
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)//
123                 .build(false);
124
125         channels.put(colorChannelID, colorChannel);
126     }
127
128     @Override
129     public CompletableFuture<@Nullable Void> start(MqttBrokerConnection connection, ScheduledExecutorService scheduler,
130             int timeout) {
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));
134     }
135
136     @Override
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));
141     }
142
143     /**
144      * Proxy method to condense all three MQTT subscriptions to one channel
145      */
146     @Override
147     public void updateChannelState(ChannelUID channelUID, State value) {
148         ChannelStateUpdateListener listener = channelStateUpdateListener;
149         if (listener != null) {
150             listener.updateChannelState(colorChannel.getChannelUID(), value);
151         }
152     }
153
154     /**
155      * Proxy method to condense all three MQTT subscriptions to one channel
156      */
157     @Override
158     public void postChannelCommand(ChannelUID channelUID, Command value) {
159         ChannelStateUpdateListener listener = channelStateUpdateListener;
160         if (listener != null) {
161             listener.postChannelCommand(colorChannel.getChannelUID(), value);
162         }
163     }
164
165     /**
166      * Proxy method to condense all three MQTT subscriptions to one channel
167      */
168     @Override
169     public void triggerChannel(ChannelUID channelUID, String eventPayload) {
170         ChannelStateUpdateListener listener = channelStateUpdateListener;
171         if (listener != null) {
172             listener.triggerChannel(colorChannel.getChannelUID(), eventPayload);
173         }
174     }
175 }