2 * Copyright (c) 2010-2020 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;
19 import org.apache.commons.lang.StringUtils;
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.CFactory.ComponentConfiguration;
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.StateDescriptionFragment;
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 CChannel {
60 private static final String JINJA = "JINJA";
62 private final ChannelUID channelUID;
63 private final ChannelState channelState;
64 private final Channel channel;
65 private final ChannelType type;
66 private final ChannelTypeUID channelTypeUID;
67 private final ChannelStateUpdateListener channelStateUpdateListener;
69 private CChannel(ChannelUID channelUID, ChannelState channelState, Channel channel, ChannelType type,
70 ChannelTypeUID channelTypeUID, ChannelStateUpdateListener channelStateUpdateListener) {
72 this.channelUID = channelUID;
73 this.channelState = channelState;
74 this.channel = channel;
76 this.channelTypeUID = channelTypeUID;
77 this.channelStateUpdateListener = channelStateUpdateListener;
80 public ChannelUID getChannelUID() {
84 public Channel getChannel() {
88 public ChannelState getState() {
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 an stop
99 channelState.setChannelStateUpdateListener(this.channelStateUpdateListener);
101 return channelState.start(connection, scheduler, timeout);
104 public void addChannelTypes(MqttChannelTypeProvider channelTypeProvider) {
105 channelTypeProvider.setChannelType(channelTypeUID, type);
108 public void removeChannelTypes(MqttChannelTypeProvider channelTypeProvider) {
109 channelTypeProvider.removeChannelType(channelTypeUID);
112 public ChannelDefinition type() {
113 return new ChannelDefinitionBuilder(channelUID.getId(), channelTypeUID).build();
116 public void resetState() {
117 channelState.getCache().resetState();
120 public static class Builder {
121 private AbstractComponent<?> component;
122 private ComponentConfiguration componentConfiguration;
123 private String channelID;
124 private Value valueState;
125 private String label;
126 private @Nullable String state_topic;
127 private @Nullable String command_topic;
128 private boolean retain;
129 private boolean trigger;
130 private @Nullable Integer qos;
131 private ChannelStateUpdateListener channelStateUpdateListener;
133 private @Nullable String templateIn;
135 public Builder(AbstractComponent<?> component, ComponentConfiguration componentConfiguration, String channelID,
136 Value valueState, String label, ChannelStateUpdateListener channelStateUpdateListener) {
137 this.component = component;
138 this.componentConfiguration = componentConfiguration;
139 this.channelID = channelID;
140 this.valueState = valueState;
142 this.channelStateUpdateListener = channelStateUpdateListener;
145 public Builder stateTopic(@Nullable String state_topic) {
146 this.state_topic = state_topic;
150 public Builder stateTopic(@Nullable String state_topic, @Nullable String... templates) {
151 this.state_topic = state_topic;
152 if (StringUtils.isNotBlank(state_topic)) {
153 for (String template : templates) {
154 if (StringUtils.isNotBlank(template)) {
155 this.templateIn = template;
164 * @deprecated use commandTopic(String, boolean, int)
165 * @param command_topic
170 public Builder commandTopic(@Nullable String command_topic, boolean retain) {
171 this.command_topic = command_topic;
172 this.retain = retain;
176 public Builder commandTopic(@Nullable String command_topic, boolean retain, int qos) {
177 this.command_topic = command_topic;
178 this.retain = retain;
183 public Builder trigger(boolean trigger) {
184 this.trigger = trigger;
188 public CChannel build() {
192 public CChannel build(boolean addToComponent) {
193 ChannelUID channelUID;
194 ChannelState channelState;
197 ChannelTypeUID channelTypeUID;
199 channelUID = new ChannelUID(component.channelGroupUID, channelID);
200 channelTypeUID = new ChannelTypeUID(MqttBindingConstants.BINDING_ID,
201 channelUID.getGroupId() + "_" + channelID);
202 channelState = new ChannelState(
203 ChannelConfigBuilder.create().withRetain(retain).withQos(qos).withStateTopic(state_topic)
204 .withCommandTopic(command_topic).makeTrigger(trigger).build(),
205 channelUID, valueState, channelStateUpdateListener);
207 if (StringUtils.isBlank(state_topic) || this.trigger) {
208 type = ChannelTypeBuilder.trigger(channelTypeUID, label)
209 .withConfigDescriptionURI(URI.create(MqttBindingConstants.CONFIG_HA_CHANNEL)).build();
211 StateDescriptionFragment description = valueState.createStateDescription(command_topic == null).build();
212 type = ChannelTypeBuilder.state(channelTypeUID, label, channelState.getItemType())
213 .withConfigDescriptionURI(URI.create(MqttBindingConstants.CONFIG_HA_CHANNEL))
214 .withStateDescriptionFragment(description).build();
217 Configuration configuration = new Configuration();
218 configuration.put("config", component.channelConfigurationJson);
219 component.haID.toConfig(configuration);
221 channel = ChannelBuilder.create(channelUID, channelState.getItemType()).withType(channelTypeUID)
222 .withKind(type.getKind()).withLabel(label).withConfiguration(configuration).build();
224 CChannel result = new CChannel(channelUID, channelState, channel, type, channelTypeUID,
225 channelStateUpdateListener);
228 TransformationServiceProvider transformationProvider = componentConfiguration
229 .getTransformationServiceProvider();
231 final String templateIn = this.templateIn;
232 if (templateIn != null && transformationProvider != null) {
234 .addTransformation(new ChannelStateTransformation(JINJA, templateIn, transformationProvider));
236 if (addToComponent) {
237 component.channels.put(channelID, result);