From: Cody Cutrer Date: Tue, 22 Oct 2024 04:28:14 +0000 (-0500) Subject: [mqtt.homeassistant] JSON Attributes can exist on BinarySensor (#17608) X-Git-Url: https://git.basschouten.com/?a=commitdiff_plain;h=c620b527e59dea4b42541fcbec11fd1b8cf5bbf2;p=openhab-addons.git [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 --- 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 9d4c7b5b84..1c44b33e72 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 1fab6a7751..18d1979e64 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 9060e063b5..f1622647ef 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(); }