]> git.basschouten.com Git - openhab-addons.git/commitdiff
[mqtt.homeassistant] Fix jinja usage in availability templates (#17400)
authorCody Cutrer <cody@cutrer.us>
Fri, 27 Sep 2024 14:52:31 +0000 (08:52 -0600)
committerGitHub <noreply@github.com>
Fri, 27 Sep 2024 14:52:31 +0000 (16:52 +0200)
* [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 <cody@cutrer.us>
bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/AbstractMQTTThingHandler.java
bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/AvailabilityTracker.java
bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/internal/handler/GenericMQTTThingHandler.java
bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/ComponentChannel.java
bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/component/AbstractComponent.java
bundles/org.openhab.binding.mqtt.homie/src/main/java/org/openhab/binding/mqtt/homie/internal/handler/HomieThingHandler.java

index b43f8fa4cb05232a0ea6feb75ee9b98db8392b43..6c850bbcbf94c78e4c8191443ab8e5fa2e3e1f27 100644 (file)
@@ -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.ThingStatus;
 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 abstract class AbstractMQTTThingHandler extends BaseThingHandler
     @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<String> 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 abstract class AbstractMQTTThingHandler extends BaseThingHandler
                         @Override
                         public void postChannelCommand(ChannelUID channelUID, Command value) {
                         }
-                    });
+                    }, transformation, null);
             MqttBrokerConnection connection = getConnection();
             if (connection != null) {
                 state.start(connection, scheduler, 0);
index cae01fb12b159719a176330c6f0a1e463a2ce4b4..b436097396a97f71b2f85bba183bc3668d67526b 100644 (file)
@@ -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 @@ public interface AvailabilityTracker {
      * @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<String> transformation_pattern);
+            @Nullable ChannelTransformation transformation);
 
     void removeAvailabilityTopic(String availability_topic);
 
index d9ab437f4f41d4339a9c6095d1fa591b134ccd6a..a2cad79fe4fea26d36693381504467b0bf5f2149 100644 (file)
@@ -42,6 +42,7 @@ import org.openhab.core.thing.ThingStatusDetail;
 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 @@ public class GenericMQTTThingHandler extends AbstractMQTTThingHandler implements
 
         if (availabilityTopic != null) {
             addAvailabilityTopic(availabilityTopic, config.payloadAvailable, config.payloadNotAvailable,
-                    config.transformationPattern);
+                    new ChannelTransformation(config.transformationPattern));
         } else {
             clearAllAvailabilityTopics();
         }
index df7b926870b8bebe04ea9532ebd9bdd221b9e642..0131823c4ccdbeec336e49b461ad1a9fa133325a 100644 (file)
@@ -57,8 +57,6 @@ import org.openhab.core.types.StateDescription;
  */
 @NonNullByDefault
 public class ComponentChannel {
-    private static final String JINJA = "JINJA";
-
     private final ChannelState channelState;
     private final Channel channel;
     private final @Nullable StateDescription stateDescription;
index 131cc81d65cf833019f698f8d30afeae31a678f5..111322a25392188b7cf84847ea1ef2859feb7858 100644 (file)
@@ -31,6 +31,7 @@ import org.openhab.binding.mqtt.homeassistant.generic.internal.MqttBindingConsta
 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.io.transport.mqtt.MqttBrokerConnection;
 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 @@ import com.hubspot.jinjava.Jinjava;
  */
 @NonNullByDefault
 public abstract class AbstractComponent<C extends AbstractChannelConfiguration> {
-    private static final String JINJA_PREFIX = "JINJA:";
 
     // Component location fields
     protected final ComponentConfiguration componentConfiguration;
@@ -132,27 +133,24 @@ public abstract class AbstractComponent<C extends AbstractChannelConfiguration>
             componentConfiguration.getTracker().setAvailabilityMode(availabilityTrackerMode);
             for (Availability availability : availabilities) {
                 String availabilityTemplate = availability.getValueTemplate();
-                List<String> 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<String> 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);
             }
         }
     }
index f4e1899b65529bf2916a13824803068a797f43a2..180e928e48e935c278a811114f02ec4b6e1c236f 100644 (file)
@@ -44,6 +44,7 @@ import org.openhab.core.thing.Thing;
 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 @@ public class HomieThingHandler extends AbstractMQTTThingHandler implements Devic
 
         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) {
+    }
 }