2 * Copyright (c) 2010-2024 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.Objects;
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.ChannelStateUpdateListener;
25 import org.openhab.binding.mqtt.generic.values.Value;
26 import org.openhab.binding.mqtt.homeassistant.internal.component.AbstractComponent;
27 import org.openhab.core.config.core.Configuration;
28 import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
29 import org.openhab.core.thing.Channel;
30 import org.openhab.core.thing.ChannelUID;
31 import org.openhab.core.thing.binding.builder.ChannelBuilder;
32 import org.openhab.core.thing.binding.generic.ChannelTransformation;
33 import org.openhab.core.thing.type.AutoUpdatePolicy;
34 import org.openhab.core.thing.type.ChannelDefinition;
35 import org.openhab.core.thing.type.ChannelDefinitionBuilder;
36 import org.openhab.core.thing.type.ChannelKind;
37 import org.openhab.core.thing.type.ChannelType;
38 import org.openhab.core.thing.type.ChannelTypeUID;
39 import org.openhab.core.types.Command;
40 import org.openhab.core.types.CommandDescription;
41 import org.openhab.core.types.StateDescription;
44 * An {@link AbstractComponent}s derived class consists of one or multiple channels.
45 * Each component channel consists of the determined channel type, channel type UID and the
46 * channel description itself as well as the the channels state.
48 * After the discovery process has completed and the tree of components and component channels
49 * have been built up, the channel types are registered to a custom channel type provider
50 * before adding the channel descriptions to the Thing themselves.
53 * An object of this class creates the required {@link ChannelType} and {@link ChannelTypeUID} as well
54 * as keeps the {@link ChannelState} and {@link Channel} in one place.
56 * @author David Graeff - Initial contribution
59 public class ComponentChannel {
60 private static final String JINJA = "JINJA";
62 private final ChannelState channelState;
63 private final Channel channel;
64 private final @Nullable StateDescription stateDescription;
65 private final @Nullable CommandDescription commandDescription;
66 private final ChannelStateUpdateListener channelStateUpdateListener;
68 private ComponentChannel(ChannelState channelState, Channel channel, @Nullable StateDescription stateDescription,
69 @Nullable CommandDescription commandDescription, ChannelStateUpdateListener channelStateUpdateListener) {
71 this.channelState = channelState;
72 this.channel = channel;
73 this.stateDescription = stateDescription;
74 this.commandDescription = commandDescription;
75 this.channelStateUpdateListener = channelStateUpdateListener;
78 public Channel getChannel() {
82 public ChannelState getState() {
86 public @Nullable StateDescription getStateDescription() {
87 return stateDescription;
90 public @Nullable CommandDescription getCommandDescription() {
91 return commandDescription;
94 public CompletableFuture<@Nullable Void> stop() {
95 return channelState.stop();
98 public CompletableFuture<@Nullable Void> start(MqttBrokerConnection connection, ScheduledExecutorService scheduler,
100 // Make sure we set the callback again which might have been nulled during a stop
101 channelState.setChannelStateUpdateListener(this.channelStateUpdateListener);
103 return channelState.start(connection, scheduler, timeout);
106 public ChannelDefinition channelDefinition() {
107 return new ChannelDefinitionBuilder(channel.getUID().getId(),
108 Objects.requireNonNull(channel.getChannelTypeUID())).withLabel(channel.getLabel()).build();
111 public void resetState() {
112 channelState.getCache().resetState();
115 public static class Builder {
116 private final AbstractComponent<?> component;
117 private final String channelID;
118 private ChannelTypeUID channelTypeUID;
119 private final Value valueState;
120 private final String label;
121 private final ChannelStateUpdateListener channelStateUpdateListener;
123 private @Nullable String stateTopic;
124 private @Nullable String commandTopic;
125 private boolean retain;
126 private boolean trigger;
127 private boolean isAdvanced;
128 private @Nullable AutoUpdatePolicy autoUpdatePolicy;
129 private @Nullable Integer qos;
130 private @Nullable Predicate<Command> commandFilter;
132 private @Nullable String templateIn;
133 private @Nullable String templateOut;
135 private String format = "%s";
137 public Builder(AbstractComponent<?> component, String channelID, ChannelTypeUID channelTypeUID,
138 Value valueState, String label, ChannelStateUpdateListener channelStateUpdateListener) {
139 this.component = component;
140 this.channelID = channelID;
141 this.channelTypeUID = channelTypeUID;
142 this.valueState = valueState;
144 this.isAdvanced = false;
145 this.channelStateUpdateListener = channelStateUpdateListener;
148 public Builder stateTopic(@Nullable String stateTopic) {
149 this.stateTopic = stateTopic;
153 public Builder stateTopic(@Nullable String stateTopic, @Nullable String... templates) {
154 this.stateTopic = stateTopic;
155 if (stateTopic != null && !stateTopic.isBlank()) {
156 for (String template : templates) {
157 if (template != null && !template.isBlank()) {
158 this.templateIn = template;
167 * @deprecated use commandTopic(String, boolean, int)
168 * @param commandTopic topic
169 * @param retain retain
173 public Builder commandTopic(@Nullable String commandTopic, boolean retain) {
174 this.commandTopic = commandTopic;
175 this.retain = retain;
179 public Builder commandTopic(@Nullable String commandTopic, boolean retain, int qos) {
180 return commandTopic(commandTopic, retain, qos, null);
183 public Builder commandTopic(@Nullable String commandTopic, boolean retain, int qos, @Nullable String template) {
184 this.commandTopic = commandTopic;
185 this.retain = retain;
187 if (commandTopic != null && !commandTopic.isBlank()) {
188 this.templateOut = template;
193 public Builder trigger(boolean trigger) {
194 this.trigger = trigger;
198 public Builder isAdvanced(boolean advanced) {
199 this.isAdvanced = advanced;
203 public Builder withAutoUpdatePolicy(@Nullable AutoUpdatePolicy autoUpdatePolicy) {
204 this.autoUpdatePolicy = autoUpdatePolicy;
208 public Builder commandFilter(@Nullable Predicate<Command> commandFilter) {
209 this.commandFilter = commandFilter;
213 public Builder withFormat(String format) {
214 this.format = format;
218 public ComponentChannel build() {
222 public ComponentChannel build(boolean addToComponent) {
223 ChannelUID channelUID;
224 ChannelState channelState;
226 ChannelTransformation incomingTransformation = null, outgoingTransformation = null;
228 channelUID = component.buildChannelUID(channelID);
229 ChannelConfigBuilder channelConfigBuilder = ChannelConfigBuilder.create().withRetain(retain).withQos(qos)
230 .withStateTopic(stateTopic).withCommandTopic(commandTopic).makeTrigger(trigger)
231 .withFormatter(format);
233 String localTemplateIn = templateIn;
234 if (localTemplateIn != null) {
235 incomingTransformation = new HomeAssistantChannelTransformation(component.getJinjava(), component,
238 String localTemplateOut = templateOut;
239 if (localTemplateOut != null) {
240 outgoingTransformation = new HomeAssistantChannelTransformation(component.getJinjava(), component,
244 channelState = new HomeAssistantChannelState(channelConfigBuilder.build(), channelUID, valueState,
245 channelStateUpdateListener, commandFilter, incomingTransformation, outgoingTransformation);
247 // disabled by default components should always show up as advanced
248 if (!component.isEnabledByDefault()) {
252 channelTypeUID = new ChannelTypeUID(channelTypeUID.getBindingId(),
253 channelTypeUID.getId() + "-advanced");
257 StateDescription stateDescription = null;
258 CommandDescription commandDescription = null;
260 kind = ChannelKind.TRIGGER;
262 kind = ChannelKind.STATE;
263 stateDescription = valueState.createStateDescription(commandTopic == null).build().toStateDescription();
264 commandDescription = valueState.createCommandDescription().build();
267 Configuration configuration = new Configuration();
268 configuration.put("config", component.getChannelConfigurationJson());
269 component.getHaID().toConfig(configuration);
271 channel = ChannelBuilder.create(channelUID, channelState.getItemType()).withType(channelTypeUID)
272 .withKind(kind).withLabel(label).withConfiguration(configuration)
273 .withAutoUpdatePolicy(autoUpdatePolicy).build();
275 ComponentChannel result = new ComponentChannel(channelState, channel, stateDescription, commandDescription,
276 channelStateUpdateListener);
278 if (addToComponent) {
279 component.getChannelMap().put(channelID, result);