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 final ChannelState channelState;
61 private final Channel channel;
62 private final @Nullable StateDescription stateDescription;
63 private final @Nullable CommandDescription commandDescription;
64 private final ChannelStateUpdateListener channelStateUpdateListener;
66 private ComponentChannel(ChannelState channelState, Channel channel, @Nullable StateDescription stateDescription,
67 @Nullable CommandDescription commandDescription, ChannelStateUpdateListener channelStateUpdateListener) {
69 this.channelState = channelState;
70 this.channel = channel;
71 this.stateDescription = stateDescription;
72 this.commandDescription = commandDescription;
73 this.channelStateUpdateListener = channelStateUpdateListener;
76 public Channel getChannel() {
80 public ChannelState getState() {
84 public @Nullable StateDescription getStateDescription() {
85 return stateDescription;
88 public @Nullable CommandDescription getCommandDescription() {
89 return commandDescription;
92 public CompletableFuture<@Nullable Void> stop() {
93 return channelState.stop();
96 public CompletableFuture<@Nullable Void> start(MqttBrokerConnection connection, ScheduledExecutorService scheduler,
98 // Make sure we set the callback again which might have been nulled during a stop
99 channelState.setChannelStateUpdateListener(this.channelStateUpdateListener);
101 return channelState.start(connection, scheduler, timeout);
104 public ChannelDefinition channelDefinition() {
105 return new ChannelDefinitionBuilder(channel.getUID().getId(),
106 Objects.requireNonNull(channel.getChannelTypeUID())).withLabel(channel.getLabel()).build();
109 public void resetState() {
110 channelState.getCache().resetState();
113 public static class Builder {
114 private final AbstractComponent<?> component;
115 private final String channelID;
116 private ChannelTypeUID channelTypeUID;
117 private final Value valueState;
118 private final String label;
119 private final ChannelStateUpdateListener channelStateUpdateListener;
121 private @Nullable String stateTopic;
122 private @Nullable String commandTopic;
123 private boolean retain;
124 private boolean trigger;
125 private boolean isAdvanced;
126 private @Nullable AutoUpdatePolicy autoUpdatePolicy;
127 private @Nullable Integer qos;
128 private @Nullable Predicate<Command> commandFilter;
130 private @Nullable String templateIn;
131 private @Nullable String templateOut;
133 private String format = "%s";
135 public Builder(AbstractComponent<?> component, String channelID, ChannelTypeUID channelTypeUID,
136 Value valueState, String label, ChannelStateUpdateListener channelStateUpdateListener) {
137 this.component = component;
138 this.channelID = channelID;
139 this.channelTypeUID = channelTypeUID;
140 this.valueState = valueState;
142 this.isAdvanced = false;
143 this.channelStateUpdateListener = channelStateUpdateListener;
146 public Builder stateTopic(@Nullable String stateTopic) {
147 this.stateTopic = stateTopic;
151 public Builder stateTopic(@Nullable String stateTopic, @Nullable String... templates) {
152 this.stateTopic = stateTopic;
153 if (stateTopic != null && !stateTopic.isBlank()) {
154 for (String template : templates) {
155 if (template != null && !template.isBlank()) {
156 this.templateIn = template;
165 * @deprecated use commandTopic(String, boolean, int)
166 * @param commandTopic topic
167 * @param retain retain
171 public Builder commandTopic(@Nullable String commandTopic, boolean retain) {
172 this.commandTopic = commandTopic;
173 this.retain = retain;
177 public Builder commandTopic(@Nullable String commandTopic, boolean retain, int qos) {
178 return commandTopic(commandTopic, retain, qos, null);
181 public Builder commandTopic(@Nullable String commandTopic, boolean retain, int qos, @Nullable String template) {
182 this.commandTopic = commandTopic;
183 this.retain = retain;
185 if (commandTopic != null && !commandTopic.isBlank()) {
186 this.templateOut = template;
191 public Builder trigger(boolean trigger) {
192 this.trigger = trigger;
196 public Builder isAdvanced(boolean advanced) {
197 this.isAdvanced = advanced;
201 public Builder withAutoUpdatePolicy(@Nullable AutoUpdatePolicy autoUpdatePolicy) {
202 this.autoUpdatePolicy = autoUpdatePolicy;
206 public Builder commandFilter(@Nullable Predicate<Command> commandFilter) {
207 this.commandFilter = commandFilter;
211 public Builder withFormat(String format) {
212 this.format = format;
216 public ComponentChannel build() {
220 public ComponentChannel build(boolean addToComponent) {
221 ChannelUID channelUID;
222 ChannelState channelState;
224 ChannelTransformation incomingTransformation = null, outgoingTransformation = null;
226 channelUID = component.buildChannelUID(channelID);
227 ChannelConfigBuilder channelConfigBuilder = ChannelConfigBuilder.create().withRetain(retain).withQos(qos)
228 .withStateTopic(stateTopic).withCommandTopic(commandTopic).makeTrigger(trigger)
229 .withFormatter(format);
231 String localTemplateIn = templateIn;
232 if (localTemplateIn != null) {
233 incomingTransformation = new HomeAssistantChannelTransformation(component.getJinjava(), component,
236 String localTemplateOut = templateOut;
237 if (localTemplateOut != null) {
238 outgoingTransformation = new HomeAssistantChannelTransformation(component.getJinjava(), component,
242 channelState = new HomeAssistantChannelState(channelConfigBuilder.build(), channelUID, valueState,
243 channelStateUpdateListener, commandFilter, incomingTransformation, outgoingTransformation);
245 // disabled by default components should always show up as advanced
246 if (!component.isEnabledByDefault()) {
250 channelTypeUID = new ChannelTypeUID(channelTypeUID.getBindingId(),
251 channelTypeUID.getId() + "-advanced");
255 StateDescription stateDescription = null;
256 CommandDescription commandDescription = null;
258 kind = ChannelKind.TRIGGER;
260 kind = ChannelKind.STATE;
261 stateDescription = valueState.createStateDescription(commandTopic == null).build().toStateDescription();
262 commandDescription = valueState.createCommandDescription().build();
265 Configuration configuration = new Configuration();
266 configuration.put("config", component.getChannelConfigurationJson());
267 component.getHaID().toConfig(configuration);
269 channel = ChannelBuilder.create(channelUID, channelState.getItemType()).withType(channelTypeUID)
270 .withKind(kind).withLabel(label).withConfiguration(configuration)
271 .withAutoUpdatePolicy(autoUpdatePolicy).build();
273 ComponentChannel result = new ComponentChannel(channelState, channel, stateDescription, commandDescription,
274 channelStateUpdateListener);
276 if (addToComponent) {
277 component.getChannelMap().put(channelID, result);