]> git.basschouten.com Git - openhab-addons.git/blob
0131823c4ccdbeec336e49b461ad1a9fa133325a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.Objects;
16 import java.util.concurrent.CompletableFuture;
17 import java.util.concurrent.ScheduledExecutorService;
18 import java.util.function.Predicate;
19
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;
42
43 /**
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.
47  *
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.
51  * <br>
52  * <br>
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.
55  *
56  * @author David Graeff - Initial contribution
57  */
58 @NonNullByDefault
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;
65
66     private ComponentChannel(ChannelState channelState, Channel channel, @Nullable StateDescription stateDescription,
67             @Nullable CommandDescription commandDescription, ChannelStateUpdateListener channelStateUpdateListener) {
68         super();
69         this.channelState = channelState;
70         this.channel = channel;
71         this.stateDescription = stateDescription;
72         this.commandDescription = commandDescription;
73         this.channelStateUpdateListener = channelStateUpdateListener;
74     }
75
76     public Channel getChannel() {
77         return channel;
78     }
79
80     public ChannelState getState() {
81         return channelState;
82     }
83
84     public @Nullable StateDescription getStateDescription() {
85         return stateDescription;
86     }
87
88     public @Nullable CommandDescription getCommandDescription() {
89         return commandDescription;
90     }
91
92     public CompletableFuture<@Nullable Void> stop() {
93         return channelState.stop();
94     }
95
96     public CompletableFuture<@Nullable Void> start(MqttBrokerConnection connection, ScheduledExecutorService scheduler,
97             int timeout) {
98         // Make sure we set the callback again which might have been nulled during a stop
99         channelState.setChannelStateUpdateListener(this.channelStateUpdateListener);
100
101         return channelState.start(connection, scheduler, timeout);
102     }
103
104     public ChannelDefinition channelDefinition() {
105         return new ChannelDefinitionBuilder(channel.getUID().getId(),
106                 Objects.requireNonNull(channel.getChannelTypeUID())).withLabel(channel.getLabel()).build();
107     }
108
109     public void resetState() {
110         channelState.getCache().resetState();
111     }
112
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;
120
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;
129
130         private @Nullable String templateIn;
131         private @Nullable String templateOut;
132
133         private String format = "%s";
134
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;
141             this.label = label;
142             this.isAdvanced = false;
143             this.channelStateUpdateListener = channelStateUpdateListener;
144         }
145
146         public Builder stateTopic(@Nullable String stateTopic) {
147             this.stateTopic = stateTopic;
148             return this;
149         }
150
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;
157                         break;
158                     }
159                 }
160             }
161             return this;
162         }
163
164         /**
165          * @deprecated use commandTopic(String, boolean, int)
166          * @param commandTopic topic
167          * @param retain retain
168          * @return this
169          */
170         @Deprecated
171         public Builder commandTopic(@Nullable String commandTopic, boolean retain) {
172             this.commandTopic = commandTopic;
173             this.retain = retain;
174             return this;
175         }
176
177         public Builder commandTopic(@Nullable String commandTopic, boolean retain, int qos) {
178             return commandTopic(commandTopic, retain, qos, null);
179         }
180
181         public Builder commandTopic(@Nullable String commandTopic, boolean retain, int qos, @Nullable String template) {
182             this.commandTopic = commandTopic;
183             this.retain = retain;
184             this.qos = qos;
185             if (commandTopic != null && !commandTopic.isBlank()) {
186                 this.templateOut = template;
187             }
188             return this;
189         }
190
191         public Builder trigger(boolean trigger) {
192             this.trigger = trigger;
193             return this;
194         }
195
196         public Builder isAdvanced(boolean advanced) {
197             this.isAdvanced = advanced;
198             return this;
199         }
200
201         public Builder withAutoUpdatePolicy(@Nullable AutoUpdatePolicy autoUpdatePolicy) {
202             this.autoUpdatePolicy = autoUpdatePolicy;
203             return this;
204         }
205
206         public Builder commandFilter(@Nullable Predicate<Command> commandFilter) {
207             this.commandFilter = commandFilter;
208             return this;
209         }
210
211         public Builder withFormat(String format) {
212             this.format = format;
213             return this;
214         }
215
216         public ComponentChannel build() {
217             return build(true);
218         }
219
220         public ComponentChannel build(boolean addToComponent) {
221             ChannelUID channelUID;
222             ChannelState channelState;
223             Channel channel;
224             ChannelTransformation incomingTransformation = null, outgoingTransformation = null;
225
226             channelUID = component.buildChannelUID(channelID);
227             ChannelConfigBuilder channelConfigBuilder = ChannelConfigBuilder.create().withRetain(retain).withQos(qos)
228                     .withStateTopic(stateTopic).withCommandTopic(commandTopic).makeTrigger(trigger)
229                     .withFormatter(format);
230
231             String localTemplateIn = templateIn;
232             if (localTemplateIn != null) {
233                 incomingTransformation = new HomeAssistantChannelTransformation(component.getJinjava(), component,
234                         localTemplateIn);
235             }
236             String localTemplateOut = templateOut;
237             if (localTemplateOut != null) {
238                 outgoingTransformation = new HomeAssistantChannelTransformation(component.getJinjava(), component,
239                         localTemplateOut);
240             }
241
242             channelState = new HomeAssistantChannelState(channelConfigBuilder.build(), channelUID, valueState,
243                     channelStateUpdateListener, commandFilter, incomingTransformation, outgoingTransformation);
244
245             // disabled by default components should always show up as advanced
246             if (!component.isEnabledByDefault()) {
247                 isAdvanced = true;
248             }
249             if (isAdvanced) {
250                 channelTypeUID = new ChannelTypeUID(channelTypeUID.getBindingId(),
251                         channelTypeUID.getId() + "-advanced");
252             }
253
254             ChannelKind kind;
255             StateDescription stateDescription = null;
256             CommandDescription commandDescription = null;
257             if (this.trigger) {
258                 kind = ChannelKind.TRIGGER;
259             } else {
260                 kind = ChannelKind.STATE;
261                 stateDescription = valueState.createStateDescription(commandTopic == null).build().toStateDescription();
262                 commandDescription = valueState.createCommandDescription().build();
263             }
264
265             Configuration configuration = new Configuration();
266             configuration.put("config", component.getChannelConfigurationJson());
267             component.getHaID().toConfig(configuration);
268
269             channel = ChannelBuilder.create(channelUID, channelState.getItemType()).withType(channelTypeUID)
270                     .withKind(kind).withLabel(label).withConfiguration(configuration)
271                     .withAutoUpdatePolicy(autoUpdatePolicy).build();
272
273             ComponentChannel result = new ComponentChannel(channelState, channel, stateDescription, commandDescription,
274                     channelStateUpdateListener);
275
276             if (addToComponent) {
277                 component.getChannelMap().put(channelID, result);
278             }
279             return result;
280         }
281     }
282 }