2 * Copyright (c) 2010-2023 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.homie.internal.homie300;
15 import java.math.BigDecimal;
16 import java.math.MathContext;
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.concurrent.CompletableFuture;
21 import java.util.concurrent.ScheduledExecutorService;
22 import java.util.stream.Collectors;
23 import java.util.stream.Stream;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.openhab.binding.mqtt.generic.ChannelConfigBuilder;
28 import org.openhab.binding.mqtt.generic.ChannelState;
29 import org.openhab.binding.mqtt.generic.mapping.AbstractMqttAttributeClass.AttributeChanged;
30 import org.openhab.binding.mqtt.generic.mapping.ColorMode;
31 import org.openhab.binding.mqtt.generic.values.ColorValue;
32 import org.openhab.binding.mqtt.generic.values.DateTimeValue;
33 import org.openhab.binding.mqtt.generic.values.NumberValue;
34 import org.openhab.binding.mqtt.generic.values.OnOffValue;
35 import org.openhab.binding.mqtt.generic.values.PercentageValue;
36 import org.openhab.binding.mqtt.generic.values.TextValue;
37 import org.openhab.binding.mqtt.generic.values.Value;
38 import org.openhab.binding.mqtt.homie.generic.internal.MqttBindingConstants;
39 import org.openhab.binding.mqtt.homie.internal.homie300.PropertyAttributes.DataTypeEnum;
40 import org.openhab.core.config.core.Configuration;
41 import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
42 import org.openhab.core.thing.Channel;
43 import org.openhab.core.thing.ChannelUID;
44 import org.openhab.core.thing.DefaultSystemChannelTypeProvider;
45 import org.openhab.core.thing.binding.builder.ChannelBuilder;
46 import org.openhab.core.thing.type.AutoUpdatePolicy;
47 import org.openhab.core.thing.type.ChannelType;
48 import org.openhab.core.thing.type.ChannelTypeBuilder;
49 import org.openhab.core.thing.type.ChannelTypeUID;
50 import org.openhab.core.types.util.UnitUtils;
51 import org.openhab.core.util.UIDUtils;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
56 * A homie Property (which translates into a channel).
58 * @author David Graeff - Initial contribution
61 public class Property implements AttributeChanged {
62 private final Logger logger = LoggerFactory.getLogger(Property.class);
64 public final PropertyAttributes attributes;
65 public final Node parentNode;
66 public final String propertyID;
68 protected @Nullable ChannelState channelState;
69 public final ChannelUID channelUID;
70 public final ChannelTypeUID channelTypeUID;
71 private ChannelType type;
72 private Channel channel;
73 private final String topic;
74 private final DeviceCallback callback;
75 protected boolean initialized = false;
78 * Creates a Homie Property.
80 * @param topic The base topic for this property (e.g. "homie/device/node")
81 * @param node The parent Homie Node.
82 * @param propertyID The unique property ID (among all properties on this Node).
84 public Property(String topic, Node node, String propertyID, DeviceCallback callback,
85 PropertyAttributes attributes) {
86 this.callback = callback;
87 this.attributes = attributes;
88 this.topic = topic + "/" + propertyID;
89 this.parentNode = node;
90 this.propertyID = propertyID;
91 channelUID = new ChannelUID(node.uid(), UIDUtils.encode(propertyID));
92 channelTypeUID = new ChannelTypeUID(MqttBindingConstants.BINDING_ID, UIDUtils.encode(this.topic));
93 type = ChannelTypeBuilder.trigger(channelTypeUID, "dummy").build(); // Dummy value
94 channel = ChannelBuilder.create(channelUID, "dummy").build();// Dummy value
98 * Subscribe to property attributes. This will not subscribe
99 * to the property value though. Call
100 * {@link Device#startChannels(MqttBrokerConnection, ScheduledExecutorService, int, HomieThingHandler)}
103 * @return Returns a future that completes as soon as all attribute values have been received or requests have timed
106 public CompletableFuture<@Nullable Void> subscribe(MqttBrokerConnection connection,
107 ScheduledExecutorService scheduler, int timeout) {
108 return attributes.subscribeAndReceive(connection, scheduler, topic, this, timeout)
109 // On success, create the channel and tell the handler about this property
110 .thenRun(this::attributesReceived)
111 // No matter if values have been received or not -> the subscriptions have been performed
112 .whenComplete((r, e) -> {
117 private @Nullable BigDecimal convertFromString(String value) {
119 return new BigDecimal(value);
120 } catch (NumberFormatException ignore) {
121 logger.debug("Cannot convert {} to a number", value);
127 * As soon as subscribing succeeded and corresponding MQTT values have been received, the ChannelType and
128 * ChannelState are determined.
130 public void attributesReceived() {
131 createChannelFromAttribute();
132 callback.propertyAddedOrChanged(this);
136 * Creates the ChannelType of the Homie property.
138 * @param attributes Attributes of the property.
139 * @param channelState ChannelState of the property.
141 * @return Returns the ChannelType to be used to build the Channel.
143 private ChannelType createChannelType(PropertyAttributes attributes, ChannelState channelState) {
144 // Retained property -> State channel
145 if (attributes.retained) {
146 return ChannelTypeBuilder.state(channelTypeUID, attributes.name, channelState.getItemType())
147 .withConfigDescriptionURI(URI.create(MqttBindingConstants.CONFIG_HOMIE_CHANNEL))
148 .withStateDescriptionFragment(
149 channelState.getCache().createStateDescription(!attributes.settable).build())
152 // Non-retained and settable property -> State channel
153 if (attributes.settable) {
154 return ChannelTypeBuilder.state(channelTypeUID, attributes.name, channelState.getItemType())
155 .withConfigDescriptionURI(URI.create(MqttBindingConstants.CONFIG_HOMIE_CHANNEL))
156 .withCommandDescription(channelState.getCache().createCommandDescription().build())
157 .withAutoUpdatePolicy(AutoUpdatePolicy.VETO).build();
159 // Non-retained and non settable property -> Trigger channel
160 if (attributes.datatype.equals(DataTypeEnum.enum_)) {
161 if (attributes.format.contains("PRESSED") && attributes.format.contains("RELEASED")) {
162 return DefaultSystemChannelTypeProvider.SYSTEM_RAWBUTTON;
163 } else if (attributes.format.contains("SHORT_PRESSED") && attributes.format.contains("LONG_PRESSED")
164 && attributes.format.contains("DOUBLE_PRESSED")) {
165 return DefaultSystemChannelTypeProvider.SYSTEM_BUTTON;
166 } else if (attributes.format.contains("DIR1_PRESSED") && attributes.format.contains("DIR1_RELEASED")
167 && attributes.format.contains("DIR2_PRESSED") && attributes.format.contains("DIR2_RELEASED")) {
168 return DefaultSystemChannelTypeProvider.SYSTEM_RAWROCKER;
171 return ChannelTypeBuilder.trigger(channelTypeUID, attributes.name)
172 .withConfigDescriptionURI(URI.create(MqttBindingConstants.CONFIG_HOMIE_CHANNEL)).build();
176 public void createChannelFromAttribute() {
177 final String commandTopic = topic + "/set";
178 final String stateTopic = topic;
181 Boolean isDecimal = null;
183 if (attributes.name.isEmpty()) {
184 attributes.name = propertyID;
187 switch (attributes.datatype) {
189 value = new OnOffValue("true", "false");
192 if ("hsv".equals(attributes.format)) {
193 value = new ColorValue(ColorMode.HSB, null, null, 100);
194 } else if ("rgb".equals(attributes.format)) {
195 value = new ColorValue(ColorMode.RGB, null, null, 100);
197 logger.warn("Non supported color format: '{}'. Only 'hsv' and 'rgb' are supported",
199 value = new TextValue();
203 String[] enumValues = attributes.format.split(",");
204 value = new TextValue(enumValues);
208 isDecimal = attributes.datatype == DataTypeEnum.float_;
209 String[] s = attributes.format.split("\\:");
210 BigDecimal min = s.length == 2 ? convertFromString(s[0]) : null;
211 BigDecimal max = s.length == 2 ? convertFromString(s[1]) : null;
212 BigDecimal step = (min != null && max != null)
213 ? max.subtract(min).divide(new BigDecimal(100.0), new MathContext(isDecimal ? 2 : 0))
215 if (step != null && !isDecimal && step.intValue() <= 0) {
216 step = new BigDecimal(1);
218 if (attributes.unit.contains("%") && attributes.settable) {
219 value = new PercentageValue(min, max, step, null, null);
221 value = new NumberValue(min, max, step, UnitUtils.parseUnit(attributes.unit));
225 value = new DateTimeValue();
230 value = new TextValue();
234 ChannelConfigBuilder b = ChannelConfigBuilder.create().makeTrigger(!attributes.retained)
235 .withStateTopic(stateTopic);
237 if (isDecimal != null && !isDecimal) {
238 b = b.withFormatter("%d"); // Apply formatter to only publish integers
241 if (attributes.settable) {
242 b = b.withCommandTopic(commandTopic).withRetain(false);
245 final ChannelState channelState = new ChannelState(b.build(), channelUID, value, callback);
246 this.channelState = channelState;
248 final ChannelType type = createChannelType(attributes, channelState);
251 this.channel = ChannelBuilder.create(channelUID, type.getItemType()).withType(type.getUID())
252 .withKind(type.getKind()).withLabel(attributes.name)
253 .withConfiguration(new Configuration(attributes.asMap())).build();
257 * Unsubscribe from all property attributes and the property value.
259 * @return Returns a future that completes as soon as all unsubscriptions have been performed.
261 public CompletableFuture<@Nullable Void> stop() {
262 final ChannelState channelState = this.channelState;
263 if (channelState != null) {
264 return channelState.stop().thenCompose(b -> attributes.unsubscribe());
266 return attributes.unsubscribe();
270 * @return Returns the channelState. You should have called
271 * {@link Property#subscribe(MqttBrokerConnection, ScheduledExecutorService, int)}
272 * and waited for the future to complete before calling this Getter.
274 public @Nullable ChannelState getChannelState() {
279 * Subscribes to the state topic on the given connection and informs about updates on the given listener.
281 * @param connection A broker connection
282 * @param scheduler A scheduler to realize the timeout
283 * @param timeout A timeout in milliseconds. Can be 0 to disable the timeout and let the future return earlier.
284 * @return A future that completes with true if the subscribing worked and false and/or exceptionally otherwise.
286 public CompletableFuture<@Nullable Void> startChannel(MqttBrokerConnection connection,
287 ScheduledExecutorService scheduler, int timeout) {
288 final ChannelState channelState = this.channelState;
289 if (channelState == null) {
290 CompletableFuture<@Nullable Void> f = new CompletableFuture<>();
291 f.completeExceptionally(new IllegalStateException("Attributes not yet received!"));
294 // Make sure we set the callback again which might have been nulled during a stop
295 channelState.setChannelStateUpdateListener(this.callback);
296 return channelState.start(connection, scheduler, timeout);
300 * @return Returns the channel type of this property.
301 * The type is a dummy only if {@link #channelState} has not been set yet.
303 public ChannelType getType() {
308 * @return Returns the channel of this property.
309 * The channel is a dummy only if {@link #channelState} has not been set yet.
311 public Channel getChannel() {
316 public String toString() {
317 return channelUID.toString();
321 * Because the remote device could change any of the property attributes in-between,
322 * whenever that happens, we re-create the channel, channel-type and channelState.
325 public void attributeChanged(String name, Object value, MqttBrokerConnection connection,
326 ScheduledExecutorService scheduler, boolean allMandatoryFieldsReceived) {
327 if (!initialized || !allMandatoryFieldsReceived) {
330 attributesReceived();
334 * Creates a list of retained topics related to the property
336 * @return Returns a list of relative topics
338 public List<String> getRetainedTopics() {
339 List<String> topics = new ArrayList<>();
341 topics.addAll(Stream.of(this.attributes.getClass().getDeclaredFields())
342 .map(f -> String.format("%s/$%s", this.propertyID, f.getName())).collect(Collectors.toList()));
344 // All exceptions can be ignored because the 'retained' attribute of the PropertyAttributes class
345 // is public, is a boolean variable and has a default value (true)
347 if (attributes.getClass().getDeclaredField("retained").getBoolean(attributes)) {
348 topics.add(this.propertyID);
350 } catch (NoSuchFieldException ignored) {
351 } catch (SecurityException ignored) {
352 } catch (IllegalArgumentException ignored) {
353 } catch (IllegalAccessException ignored) {