From df8d7e28ae5e231fb6b253776464dc5eda881649 Mon Sep 17 00:00:00 2001 From: Cody Cutrer Date: Mon, 21 Oct 2024 23:28:14 -0500 Subject: [PATCH] [mqtt.homeassistant] JSON Attributes can exist on BinarySensor (#17608) also fix the conditional for JSON attributes on other components, and make the channel read only. Signed-off-by: Cody Cutrer Signed-off-by: Ciprian Pascu --- .../internal/component/BinarySensor.java | 16 +++++++++++----- .../homeassistant/internal/component/Camera.java | 5 +++-- .../homeassistant/internal/component/Fan.java | 5 +++-- .../homeassistant/internal/component/Sensor.java | 8 +++----- 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/component/BinarySensor.java b/bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/component/BinarySensor.java index 9d4c7b5b84c..1c44b33e724 100644 --- a/bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/component/BinarySensor.java +++ b/bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/component/BinarySensor.java @@ -12,12 +12,11 @@ */ package org.openhab.binding.mqtt.homeassistant.internal.component; -import java.util.List; - import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.mqtt.generic.ChannelStateUpdateListener; import org.openhab.binding.mqtt.generic.values.OnOffValue; +import org.openhab.binding.mqtt.generic.values.TextValue; import org.openhab.binding.mqtt.generic.values.Value; import org.openhab.binding.mqtt.homeassistant.internal.ComponentChannelType; import org.openhab.binding.mqtt.homeassistant.internal.config.dto.AbstractChannelConfiguration; @@ -34,7 +33,8 @@ import com.google.gson.annotations.SerializedName; */ @NonNullByDefault public class BinarySensor extends AbstractComponent { - public static final String SENSOR_CHANNEL_ID = "sensor"; // Randomly chosen channel "ID" + public static final String SENSOR_CHANNEL_ID = "sensor"; + public static final String JSON_ATTRIBUTES_CHANNEL_ID = "json-attributes"; /** * Configuration class for MQTT component @@ -64,8 +64,6 @@ public class BinarySensor extends AbstractComponent jsonAttributes; } public BinarySensor(ComponentFactory.ComponentConfiguration componentConfiguration, boolean newStyleChannels) { @@ -77,6 +75,14 @@ public class BinarySensor extends AbstractComponent { buildChannel(CAMERA_CHANNEL_ID, ComponentChannelType.IMAGE, value, getName(), componentConfiguration.getUpdateListener()).stateTopic(channelConfiguration.topic).build(); - if (channelConfiguration.jsonAttributesTemplate != null) { + if (channelConfiguration.jsonAttributesTopic != null) { buildChannel(JSON_ATTRIBUTES_CHANNEL_ID, ComponentChannelType.STRING, new TextValue(), "JSON Attributes", componentConfiguration.getUpdateListener()) .stateTopic(channelConfiguration.jsonAttributesTopic, channelConfiguration.jsonAttributesTemplate) - .build(); + .withAutoUpdatePolicy(AutoUpdatePolicy.VETO).build(); } finalizeChannels(); diff --git a/bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/component/Fan.java b/bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/component/Fan.java index 1fab6a77514..18d1979e64d 100644 --- a/bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/component/Fan.java +++ b/bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/component/Fan.java @@ -28,6 +28,7 @@ import org.openhab.binding.mqtt.homeassistant.internal.config.dto.AbstractChanne import org.openhab.core.library.types.OnOffType; import org.openhab.core.library.types.PercentType; import org.openhab.core.thing.ChannelUID; +import org.openhab.core.thing.type.AutoUpdatePolicy; import org.openhab.core.types.Command; import org.openhab.core.types.State; import org.openhab.core.types.UnDefType; @@ -201,11 +202,11 @@ public class Fan extends AbstractComponent implements .inferOptimistic(channelConfiguration.optimistic).build(); } - if (channelConfiguration.jsonAttributesTemplate != null) { + if (channelConfiguration.jsonAttributesTopic != null) { buildChannel(JSON_ATTRIBUTES_CHANNEL_ID, ComponentChannelType.STRING, new TextValue(), "JSON Attributes", componentConfiguration.getUpdateListener()) .stateTopic(channelConfiguration.jsonAttributesTopic, channelConfiguration.jsonAttributesTemplate) - .build(); + .withAutoUpdatePolicy(AutoUpdatePolicy.VETO).build(); } finalizeChannels(); diff --git a/bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/component/Sensor.java b/bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/component/Sensor.java index 9060e063b5c..f1622647ef4 100644 --- a/bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/component/Sensor.java +++ b/bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/component/Sensor.java @@ -12,7 +12,6 @@ */ package org.openhab.binding.mqtt.homeassistant.internal.component; -import java.util.List; import java.util.regex.Pattern; import org.eclipse.jdt.annotation.NonNullByDefault; @@ -24,6 +23,7 @@ import org.openhab.binding.mqtt.generic.values.Value; import org.openhab.binding.mqtt.homeassistant.internal.ComponentChannelType; import org.openhab.binding.mqtt.homeassistant.internal.config.dto.AbstractChannelConfiguration; import org.openhab.binding.mqtt.homeassistant.internal.listener.ExpireUpdateStateListener; +import org.openhab.core.thing.type.AutoUpdatePolicy; import org.openhab.core.types.util.UnitUtils; import com.google.gson.annotations.SerializedName; @@ -66,8 +66,6 @@ public class Sensor extends AbstractComponent { protected @Nullable String jsonAttributesTopic; @SerializedName("json_attributes_template") protected @Nullable String jsonAttributesTemplate; - @SerializedName("json_attributes") - protected @Nullable List jsonAttributes; } public Sensor(ComponentFactory.ComponentConfiguration componentConfiguration, boolean newStyleChannels) { @@ -99,11 +97,11 @@ public class Sensor extends AbstractComponent { .stateTopic(channelConfiguration.stateTopic, channelConfiguration.getValueTemplate())// .trigger(trigger).build(); - if (channelConfiguration.jsonAttributesTemplate != null) { + if (channelConfiguration.jsonAttributesTopic != null) { buildChannel(JSON_ATTRIBUTES_CHANNEL_ID, ComponentChannelType.STRING, new TextValue(), "JSON Attributes", componentConfiguration.getUpdateListener()) .stateTopic(channelConfiguration.jsonAttributesTopic, channelConfiguration.jsonAttributesTemplate) - .build(); + .withAutoUpdatePolicy(AutoUpdatePolicy.VETO).build(); } finalizeChannels(); }