]> git.basschouten.com Git - openhab-addons.git/commitdiff
[mqtt.homeassistant] add JSON attributes channel to several components (#17605)
authorCody Cutrer <cody@cutrer.us>
Mon, 21 Oct 2024 20:12:21 +0000 (15:12 -0500)
committerGitHub <noreply@github.com>
Mon, 21 Oct 2024 20:12:21 +0000 (22:12 +0200)
Signed-off-by: Cody Cutrer <cody@cutrer.us>
bundles/org.openhab.binding.mqtt.homeassistant/README.md
bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/component/Camera.java
bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/component/Fan.java
bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/component/Sensor.java

index 6a6a74f6617445486557ee3fea46651b20c387e2..ff5fb7cc4f889083dd97c3d4000aa3e33f72ab62 100644 (file)
@@ -17,19 +17,17 @@ You can also manually create a Thing, and provide the individual component topic
 - [Binary Sensor](https://www.home-assistant.io/integrations/binary_sensor.mqtt/)
 - [Button](https://www.home-assistant.io/integrations/button.mqtt/)
 - [Camera](https://www.home-assistant.io/integrations/camera.mqtt/)<br>
-  JSON attributes and Base64 encoding are not supported.
+  Base64 encoding is not supported.
 - [Climate](https://www.home-assistant.io/integrations/climate.mqtt/)
 - [Cover](https://www.home-assistant.io/integrations/cover.mqtt/)
 - [Device Trigger](https://www.home-assistant.io/integrations/device_trigger.mqtt/)
-- [Fan](https://www.home-assistant.io/integrations/fan.mqtt/)<br>
-  JSON attributes are not supported.
+- [Fan](https://www.home-assistant.io/integrations/fan.mqtt/)
 - [Light](https://www.home-assistant.io/integrations/light.mqtt/)
 - [Lock](https://www.home-assistant.io/integrations/lock.mqtt/)
 - [Number](https://www.home-assistant.io/integrations/number.mqtt/)
 - [Scene](https://www.home-assistant.io/integrations/scene.mqtt/)
 - [Select](https://www.home-assistant.io/integrations/select.mqtt/)
-- [Sensor](https://www.home-assistant.io/integrations/sensor.mqtt/)<br>
-  JSON attributes are not supported.
+- [Sensor](https://www.home-assistant.io/integrations/sensor.mqtt/)
 - [Switch](https://www.home-assistant.io/integrations/switch.mqtt/)
 - [Update](https://www.home-assistant.io/integrations/update.mqtt/)<br>
   This is a special component, that will show up as additional properties on the Thing, and add a button on the Thing to initiate an OTA update.
index 6bfe3ad98b2bbd2c837fa89bd8b35140d6bae03c..58351a2eee2e7b2360623c12a51f76c1a871e82f 100644 (file)
 package org.openhab.binding.mqtt.homeassistant.internal.component;
 
 import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
 import org.openhab.binding.mqtt.generic.values.ImageValue;
+import org.openhab.binding.mqtt.generic.values.TextValue;
 import org.openhab.binding.mqtt.homeassistant.internal.ComponentChannelType;
 import org.openhab.binding.mqtt.homeassistant.internal.config.dto.AbstractChannelConfiguration;
 
+import com.google.gson.annotations.SerializedName;
+
 /**
  * A MQTT camera, following the https://www.home-assistant.io/components/camera.mqtt/ specification.
  *
@@ -26,7 +30,8 @@ import org.openhab.binding.mqtt.homeassistant.internal.config.dto.AbstractChanne
  */
 @NonNullByDefault
 public class Camera extends AbstractComponent<Camera.ChannelConfiguration> {
-    public static final String CAMERA_CHANNEL_ID = "camera"; // Randomly chosen channel "ID"
+    public static final String CAMERA_CHANNEL_ID = "camera";
+    public static final String JSON_ATTRIBUTES_CHANNEL_ID = "json-attributes";
 
     /**
      * Configuration class for MQTT component
@@ -37,6 +42,11 @@ public class Camera extends AbstractComponent<Camera.ChannelConfiguration> {
         }
 
         protected String topic = "";
+
+        @SerializedName("json_attributes_template")
+        protected @Nullable String jsonAttributesTemplate;
+        @SerializedName("json_attributes_topic")
+        protected @Nullable String jsonAttributesTopic;
     }
 
     public Camera(ComponentFactory.ComponentConfiguration componentConfiguration, boolean newStyleChannels) {
@@ -46,6 +56,14 @@ public class Camera extends AbstractComponent<Camera.ChannelConfiguration> {
 
         buildChannel(CAMERA_CHANNEL_ID, ComponentChannelType.IMAGE, value, getName(),
                 componentConfiguration.getUpdateListener()).stateTopic(channelConfiguration.topic).build();
+
+        if (channelConfiguration.jsonAttributesTemplate != null) {
+            buildChannel(JSON_ATTRIBUTES_CHANNEL_ID, ComponentChannelType.STRING, new TextValue(), "JSON Attributes",
+                    componentConfiguration.getUpdateListener())
+                    .stateTopic(channelConfiguration.jsonAttributesTopic, channelConfiguration.jsonAttributesTemplate)
+                    .build();
+        }
+
         finalizeChannels();
     }
 }
index ec10103926e809d0e9113e91d9d2dde2772c187f..1fab6a775142d0cf64972b872afad31ad9365c2a 100644 (file)
@@ -48,6 +48,7 @@ public class Fan extends AbstractComponent<Fan.ChannelConfiguration> implements
     public static final String PRESET_MODE_CHANNEL_ID = "preset-mode";
     public static final String OSCILLATION_CHANNEL_ID = "oscillation";
     public static final String DIRECTION_CHANNEL_ID = "direction";
+    public static final String JSON_ATTRIBUTES_CHANNEL_ID = "json-attributes";
 
     /**
      * Configuration class for MQTT component
@@ -115,6 +116,10 @@ public class Fan extends AbstractComponent<Fan.ChannelConfiguration> implements
         protected int speedRangeMax = 100;
         @SerializedName("speed_range_min")
         protected int speedRangeMin = 1;
+        @SerializedName("json_attributes_template")
+        protected @Nullable String jsonAttributesTemplate;
+        @SerializedName("json_attributes_topic")
+        protected @Nullable String jsonAttributesTopic;
     }
 
     private final OnOffValue onOffValue;
@@ -195,6 +200,14 @@ public class Fan extends AbstractComponent<Fan.ChannelConfiguration> implements
                             channelConfiguration.getQos(), channelConfiguration.directionCommandTemplate)
                     .inferOptimistic(channelConfiguration.optimistic).build();
         }
+
+        if (channelConfiguration.jsonAttributesTemplate != null) {
+            buildChannel(JSON_ATTRIBUTES_CHANNEL_ID, ComponentChannelType.STRING, new TextValue(), "JSON Attributes",
+                    componentConfiguration.getUpdateListener())
+                    .stateTopic(channelConfiguration.jsonAttributesTopic, channelConfiguration.jsonAttributesTemplate)
+                    .build();
+        }
+
         finalizeChannels();
     }
 
index 2320019b871d8e6c7f56059cbea9c83dec3aef6c..9060e063b5c1a1ef4692db898ef6582f97de2b05 100644 (file)
@@ -36,6 +36,8 @@ import com.google.gson.annotations.SerializedName;
 @NonNullByDefault
 public class Sensor extends AbstractComponent<Sensor.ChannelConfiguration> {
     public static final String SENSOR_CHANNEL_ID = "sensor"; // Randomly chosen channel "ID"
+    public static final String JSON_ATTRIBUTES_CHANNEL_ID = "json-attributes";
+
     private static final Pattern TRIGGER_ICONS = Pattern.compile("^mdi:(toggle|gesture).*$");
 
     /**
@@ -96,6 +98,13 @@ public class Sensor extends AbstractComponent<Sensor.ChannelConfiguration> {
         buildChannel(SENSOR_CHANNEL_ID, type, value, getName(), getListener(componentConfiguration, value))
                 .stateTopic(channelConfiguration.stateTopic, channelConfiguration.getValueTemplate())//
                 .trigger(trigger).build();
+
+        if (channelConfiguration.jsonAttributesTemplate != null) {
+            buildChannel(JSON_ATTRIBUTES_CHANNEL_ID, ComponentChannelType.STRING, new TextValue(), "JSON Attributes",
+                    componentConfiguration.getUpdateListener())
+                    .stateTopic(channelConfiguration.jsonAttributesTopic, channelConfiguration.jsonAttributesTemplate)
+                    .build();
+        }
         finalizeChannels();
     }