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;
16 import java.util.concurrent.CompletableFuture;
17 import java.util.concurrent.ScheduledExecutorService;
18 import java.util.function.Predicate;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.mqtt.generic.ChannelConfigBuilder;
23 import org.openhab.binding.mqtt.generic.ChannelState;
24 import org.openhab.binding.mqtt.generic.ChannelStateTransformation;
25 import org.openhab.binding.mqtt.generic.ChannelStateUpdateListener;
26 import org.openhab.binding.mqtt.generic.MqttChannelTypeProvider;
27 import org.openhab.binding.mqtt.generic.TransformationServiceProvider;
28 import org.openhab.binding.mqtt.generic.values.Value;
29 import org.openhab.binding.mqtt.homeassistant.generic.internal.MqttBindingConstants;
30 import org.openhab.binding.mqtt.homeassistant.internal.component.AbstractComponent;
31 import org.openhab.core.config.core.Configuration;
32 import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
33 import org.openhab.core.thing.Channel;
34 import org.openhab.core.thing.ChannelUID;
35 import org.openhab.core.thing.binding.builder.ChannelBuilder;
36 import org.openhab.core.thing.type.ChannelDefinition;
37 import org.openhab.core.thing.type.ChannelDefinitionBuilder;
38 import org.openhab.core.thing.type.ChannelType;
39 import org.openhab.core.thing.type.ChannelTypeBuilder;
40 import org.openhab.core.thing.type.ChannelTypeUID;
41 import org.openhab.core.types.Command;
42 import org.openhab.core.types.StateDescriptionFragment;
45 * An {@link AbstractComponent}s derived class consists of one or multiple channels.
46 * Each component channel consists of the determined channel type, channel type UID and the
47 * channel description itself as well as the the channels state.
49 * After the discovery process has completed and the tree of components and component channels
50 * have been built up, the channel types are registered to a custom channel type provider
51 * before adding the channel descriptions to the Thing themselves.
54 * An object of this class creates the required {@link ChannelType} and {@link ChannelTypeUID} as well
55 * as keeps the {@link ChannelState} and {@link Channel} in one place.
57 * @author David Graeff - Initial contribution
60 public class ComponentChannel {
61 private static final String JINJA = "JINJA";
63 private final ChannelUID channelUID;
64 private final ChannelState channelState;
65 private final Channel channel;
66 private final ChannelType type;
67 private final ChannelTypeUID channelTypeUID;
68 private final ChannelStateUpdateListener channelStateUpdateListener;
70 private ComponentChannel(ChannelUID channelUID, ChannelState channelState, Channel channel, ChannelType type,
71 ChannelTypeUID channelTypeUID, ChannelStateUpdateListener channelStateUpdateListener) {
73 this.channelUID = channelUID;
74 this.channelState = channelState;
75 this.channel = channel;
77 this.channelTypeUID = channelTypeUID;
78 this.channelStateUpdateListener = channelStateUpdateListener;
81 public ChannelUID getChannelUID() {
85 public Channel getChannel() {
89 public ChannelState getState() {
93 public CompletableFuture<@Nullable Void> stop() {
94 return channelState.stop();
97 public CompletableFuture<@Nullable Void> start(MqttBrokerConnection connection, ScheduledExecutorService scheduler,
99 // Make sure we set the callback again which might have been nulled during an stop
100 channelState.setChannelStateUpdateListener(this.channelStateUpdateListener);
102 return channelState.start(connection, scheduler, timeout);
105 public void addChannelTypes(MqttChannelTypeProvider channelTypeProvider) {
106 channelTypeProvider.setChannelType(channelTypeUID, type);
109 public void removeChannelTypes(MqttChannelTypeProvider channelTypeProvider) {
110 channelTypeProvider.removeChannelType(channelTypeUID);
113 public ChannelDefinition type() {
114 return new ChannelDefinitionBuilder(channelUID.getId(), channelTypeUID).build();
117 public void resetState() {
118 channelState.getCache().resetState();
121 public static class Builder {
122 private final AbstractComponent<?> component;
123 private final String channelID;
124 private final Value valueState;
125 private final String label;
126 private final ChannelStateUpdateListener channelStateUpdateListener;
128 private @Nullable String stateTopic;
129 private @Nullable String commandTopic;
130 private boolean retain;
131 private boolean trigger;
132 private boolean isAdvanced;
133 private @Nullable Integer qos;
134 private @Nullable Predicate<Command> commandFilter;
136 private @Nullable String templateIn;
137 private @Nullable String templateOut;
139 public Builder(AbstractComponent<?> component, String channelID, Value valueState, String label,
140 ChannelStateUpdateListener channelStateUpdateListener) {
141 this.component = component;
142 this.channelID = channelID;
143 this.valueState = valueState;
145 this.isAdvanced = false;
146 this.channelStateUpdateListener = channelStateUpdateListener;
149 public Builder stateTopic(@Nullable String stateTopic) {
150 this.stateTopic = stateTopic;
154 public Builder stateTopic(@Nullable String stateTopic, @Nullable String... templates) {
155 this.stateTopic = stateTopic;
156 if (stateTopic != null && !stateTopic.isBlank()) {
157 for (String template : templates) {
158 if (template != null && !template.isBlank()) {
159 this.templateIn = template;
168 * @deprecated use commandTopic(String, boolean, int)
169 * @param commandTopic topic
170 * @param retain retain
174 public Builder commandTopic(@Nullable String commandTopic, boolean retain) {
175 this.commandTopic = commandTopic;
176 this.retain = retain;
180 public Builder commandTopic(@Nullable String commandTopic, boolean retain, int qos) {
181 return commandTopic(commandTopic, retain, qos, null);
184 public Builder commandTopic(@Nullable String commandTopic, boolean retain, int qos, @Nullable String template) {
185 this.commandTopic = commandTopic;
186 this.retain = retain;
188 if (commandTopic != null && !commandTopic.isBlank()) {
189 this.templateOut = template;
194 public Builder trigger(boolean trigger) {
195 this.trigger = trigger;
199 public Builder isAdvanced(boolean advanced) {
200 this.isAdvanced = advanced;
204 public Builder commandFilter(@Nullable Predicate<Command> commandFilter) {
205 this.commandFilter = commandFilter;
209 public ComponentChannel build() {
213 public ComponentChannel build(boolean addToComponent) {
214 ChannelUID channelUID;
215 ChannelState channelState;
218 ChannelTypeUID channelTypeUID;
220 channelUID = new ChannelUID(component.getGroupUID(), channelID);
221 channelTypeUID = new ChannelTypeUID(MqttBindingConstants.BINDING_ID,
222 channelUID.getGroupId() + "_" + channelID);
223 channelState = new HomeAssistantChannelState(
224 ChannelConfigBuilder.create().withRetain(retain).withQos(qos).withStateTopic(stateTopic)
225 .withCommandTopic(commandTopic).makeTrigger(trigger).build(),
226 channelUID, valueState, channelStateUpdateListener, commandFilter);
228 String localStateTopic = stateTopic;
229 if (localStateTopic == null || localStateTopic.isBlank() || this.trigger) {
230 type = ChannelTypeBuilder.trigger(channelTypeUID, label)
231 .withConfigDescriptionURI(URI.create(MqttBindingConstants.CONFIG_HA_CHANNEL))
232 .isAdvanced(isAdvanced).build();
234 StateDescriptionFragment description = valueState.createStateDescription(commandTopic == null).build();
235 type = ChannelTypeBuilder.state(channelTypeUID, label, channelState.getItemType())
236 .withConfigDescriptionURI(URI.create(MqttBindingConstants.CONFIG_HA_CHANNEL))
237 .withStateDescriptionFragment(description).isAdvanced(isAdvanced).build();
240 Configuration configuration = new Configuration();
241 configuration.put("config", component.getChannelConfigurationJson());
242 component.getHaID().toConfig(configuration);
244 channel = ChannelBuilder.create(channelUID, channelState.getItemType()).withType(channelTypeUID)
245 .withKind(type.getKind()).withLabel(label).withConfiguration(configuration).build();
247 ComponentChannel result = new ComponentChannel(channelUID, channelState, channel, type, channelTypeUID,
248 channelStateUpdateListener);
250 TransformationServiceProvider transformationProvider = component.getTransformationServiceProvider();
252 final String templateIn = this.templateIn;
253 if (templateIn != null && transformationProvider != null) {
255 .addTransformation(new ChannelStateTransformation(JINJA, templateIn, transformationProvider));
257 final String templateOut = this.templateOut;
258 if (templateOut != null && transformationProvider != null) {
259 channelState.addTransformationOut(
260 new ChannelStateTransformation(JINJA, templateOut, transformationProvider));
262 if (addToComponent) {
263 component.getChannelMap().put(channelID, result);