From 601dddb456286babd911a5b715a964da6a136b94 Mon Sep 17 00:00:00 2001 From: Cody Cutrer Date: Tue, 10 Sep 2024 10:10:55 -0600 Subject: [PATCH 1/2] [mqtt.homeassistant] Fix jinja usage in availability templates Use the local Jinjava, instead of implicitly depending on the Jinja transformation service. Signed-off-by: Cody Cutrer --- .../mqtt/generic/AbstractMQTTThingHandler.java | 12 +++++------- .../mqtt/generic/AvailabilityTracker.java | 8 ++++---- .../handler/GenericMQTTThingHandler.java | 3 ++- .../internal/ComponentChannel.java | 2 -- .../internal/component/AbstractComponent.java | 18 ++++++++---------- 5 files changed, 19 insertions(+), 24 deletions(-) diff --git a/bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/AbstractMQTTThingHandler.java b/bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/AbstractMQTTThingHandler.java index b43f8fa4cb052..6c850bbcbf94c 100644 --- a/bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/AbstractMQTTThingHandler.java +++ b/bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/AbstractMQTTThingHandler.java @@ -13,7 +13,6 @@ package org.openhab.binding.mqtt.generic; import java.util.HashSet; -import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; @@ -40,6 +39,7 @@ import org.openhab.core.thing.ThingStatusDetail; import org.openhab.core.thing.ThingStatusInfo; import org.openhab.core.thing.binding.BaseThingHandler; +import org.openhab.core.thing.binding.generic.ChannelTransformation; import org.openhab.core.types.Command; import org.openhab.core.types.RefreshType; import org.openhab.core.types.State; @@ -302,19 +302,17 @@ public void setAvailabilityMode(AvailabilityMode mode) { @Override public void addAvailabilityTopic(String availability_topic, String payload_available, String payload_not_available) { - addAvailabilityTopic(availability_topic, payload_available, payload_not_available, List.of()); + addAvailabilityTopic(availability_topic, payload_available, payload_not_available, null); } @Override public void addAvailabilityTopic(String availability_topic, String payload_available, String payload_not_available, - List transformation_pattern) { + @Nullable ChannelTransformation transformation) { availabilityStates.computeIfAbsent(availability_topic, topic -> { Value value = new OnOffValue(payload_available, payload_not_available); ChannelGroupUID groupUID = new ChannelGroupUID(getThing().getUID(), "availability"); ChannelUID channelUID = new ChannelUID(groupUID, UIDUtils.encode(topic)); - ChannelState state = new ChannelState( - ChannelConfigBuilder.create().withStateTopic(topic) - .withTransformationPattern(transformation_pattern).build(), + ChannelState state = new ChannelState(ChannelConfigBuilder.create().withStateTopic(topic).build(), channelUID, value, new ChannelStateUpdateListener() { @Override public void updateChannelState(ChannelUID channelUID, State value) { @@ -329,7 +327,7 @@ public void triggerChannel(ChannelUID channelUID, String eventPayload) { @Override public void postChannelCommand(ChannelUID channelUID, Command value) { } - }); + }, transformation, null); MqttBrokerConnection connection = getConnection(); if (connection != null) { state.start(connection, scheduler, 0); diff --git a/bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/AvailabilityTracker.java b/bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/AvailabilityTracker.java index cae01fb12b159..b436097396a97 100644 --- a/bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/AvailabilityTracker.java +++ b/bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/AvailabilityTracker.java @@ -12,9 +12,9 @@ */ package org.openhab.binding.mqtt.generic; -import java.util.List; - import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; +import org.openhab.core.thing.binding.generic.ChannelTransformation; /** * Interface to keep track of the availability of device using an availability topic or messages received @@ -71,11 +71,11 @@ enum AvailabilityMode { * @param availability_topic The MQTT topic where availability is published to. * @param payload_available The value for the topic to indicate the device is online. * @param payload_not_available The value for the topic to indicate the device is offline. - * @param transformation_pattern A transformation pattern to process the value before comparing to + * @param transformation A transformation to process the value before comparing to * payload_available/payload_not_available. */ void addAvailabilityTopic(String availability_topic, String payload_available, String payload_not_available, - List transformation_pattern); + @Nullable ChannelTransformation transformation); void removeAvailabilityTopic(String availability_topic); diff --git a/bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/internal/handler/GenericMQTTThingHandler.java b/bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/internal/handler/GenericMQTTThingHandler.java index d9ab437f4f41d..a2cad79fe4fea 100644 --- a/bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/internal/handler/GenericMQTTThingHandler.java +++ b/bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/internal/handler/GenericMQTTThingHandler.java @@ -42,6 +42,7 @@ import org.openhab.core.thing.binding.ThingHandlerCallback; import org.openhab.core.thing.binding.builder.ChannelBuilder; import org.openhab.core.thing.binding.builder.ThingBuilder; +import org.openhab.core.thing.binding.generic.ChannelTransformation; import org.openhab.core.thing.type.ChannelTypeUID; import org.openhab.core.types.StateDescription; import org.openhab.core.types.util.UnitUtils; @@ -219,7 +220,7 @@ private void initializeAvailabilityTopicsFromConfig() { if (availabilityTopic != null) { addAvailabilityTopic(availabilityTopic, config.payloadAvailable, config.payloadNotAvailable, - config.transformationPattern); + new ChannelTransformation(config.transformationPattern)); } else { clearAllAvailabilityTopics(); } diff --git a/bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/ComponentChannel.java b/bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/ComponentChannel.java index df7b926870b8b..0131823c4ccdb 100644 --- a/bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/ComponentChannel.java +++ b/bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/ComponentChannel.java @@ -57,8 +57,6 @@ */ @NonNullByDefault public class ComponentChannel { - private static final String JINJA = "JINJA"; - private final ChannelState channelState; private final Channel channel; private final @Nullable StateDescription stateDescription; diff --git a/bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/component/AbstractComponent.java b/bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/component/AbstractComponent.java index 131cc81d65cf8..111322a253921 100644 --- a/bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/component/AbstractComponent.java +++ b/bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/component/AbstractComponent.java @@ -31,6 +31,7 @@ import org.openhab.binding.mqtt.homeassistant.internal.ComponentChannel; import org.openhab.binding.mqtt.homeassistant.internal.ComponentChannelType; import org.openhab.binding.mqtt.homeassistant.internal.HaID; +import org.openhab.binding.mqtt.homeassistant.internal.HomeAssistantChannelTransformation; import org.openhab.binding.mqtt.homeassistant.internal.component.ComponentFactory.ComponentConfiguration; import org.openhab.binding.mqtt.homeassistant.internal.config.dto.AbstractChannelConfiguration; import org.openhab.binding.mqtt.homeassistant.internal.config.dto.Availability; @@ -40,6 +41,7 @@ import org.openhab.core.thing.Channel; import org.openhab.core.thing.ChannelGroupUID; import org.openhab.core.thing.ChannelUID; +import org.openhab.core.thing.binding.generic.ChannelTransformation; import org.openhab.core.thing.type.ChannelDefinition; import org.openhab.core.thing.type.ChannelGroupDefinition; import org.openhab.core.thing.type.ChannelGroupType; @@ -60,7 +62,6 @@ */ @NonNullByDefault public abstract class AbstractComponent { - private static final String JINJA_PREFIX = "JINJA:"; // Component location fields protected final ComponentConfiguration componentConfiguration; @@ -132,27 +133,24 @@ public AbstractComponent(ComponentFactory.ComponentConfiguration componentConfig componentConfiguration.getTracker().setAvailabilityMode(availabilityTrackerMode); for (Availability availability : availabilities) { String availabilityTemplate = availability.getValueTemplate(); - List availabilityTemplates = List.of(); + ChannelTransformation transformation = null; if (availabilityTemplate != null) { - availabilityTemplate = JINJA_PREFIX + availabilityTemplate; - availabilityTemplates = List.of(availabilityTemplate); + transformation = new HomeAssistantChannelTransformation(getJinjava(), this, availabilityTemplate); } componentConfiguration.getTracker().addAvailabilityTopic(availability.getTopic(), - availability.getPayloadAvailable(), availability.getPayloadNotAvailable(), - availabilityTemplates); + availability.getPayloadAvailable(), availability.getPayloadNotAvailable(), transformation); } } else { String availabilityTopic = this.channelConfiguration.getAvailabilityTopic(); if (availabilityTopic != null) { String availabilityTemplate = this.channelConfiguration.getAvailabilityTemplate(); - List availabilityTemplates = List.of(); + ChannelTransformation transformation = null; if (availabilityTemplate != null) { - availabilityTemplate = JINJA_PREFIX + availabilityTemplate; - availabilityTemplates = List.of(availabilityTemplate); + transformation = new HomeAssistantChannelTransformation(getJinjava(), this, availabilityTemplate); } componentConfiguration.getTracker().addAvailabilityTopic(availabilityTopic, this.channelConfiguration.getPayloadAvailable(), - this.channelConfiguration.getPayloadNotAvailable(), availabilityTemplates); + this.channelConfiguration.getPayloadNotAvailable(), transformation); } } } From 2675e26de78e781c77bf6441b5284e8ea890145e Mon Sep 17 00:00:00 2001 From: Cody Cutrer Date: Fri, 27 Sep 2024 08:26:42 -0600 Subject: [PATCH 2/2] resolve compilation issue for Homie itests Signed-off-by: Cody Cutrer --- .../mqtt/homie/internal/handler/HomieThingHandler.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/bundles/org.openhab.binding.mqtt.homie/src/main/java/org/openhab/binding/mqtt/homie/internal/handler/HomieThingHandler.java b/bundles/org.openhab.binding.mqtt.homie/src/main/java/org/openhab/binding/mqtt/homie/internal/handler/HomieThingHandler.java index f4e1899b65529..180e928e48e93 100644 --- a/bundles/org.openhab.binding.mqtt.homie/src/main/java/org/openhab/binding/mqtt/homie/internal/handler/HomieThingHandler.java +++ b/bundles/org.openhab.binding.mqtt.homie/src/main/java/org/openhab/binding/mqtt/homie/internal/handler/HomieThingHandler.java @@ -44,6 +44,7 @@ import org.openhab.core.thing.ThingStatus; import org.openhab.core.thing.ThingStatusDetail; import org.openhab.core.thing.ThingTypeUID; +import org.openhab.core.thing.binding.generic.ChannelTransformation; import org.openhab.core.thing.type.ChannelGroupDefinition; import org.openhab.core.thing.type.ChannelTypeRegistry; import org.openhab.core.thing.type.ThingType; @@ -327,4 +328,11 @@ private Collection nodeOrder() { return device.nodes.keySet(); } + + // This odd method resolves a compilation issue (possibly with Mockito?) where for some reason + // it doesn't realize it needs to import this class which is used by AvailabilityTracker, but + // not directly from this bundle + // See https://github.com/openhab/openhab-addons/pull/17400 + private void doNothing(ChannelTransformation transform) { + } }