[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 <cody@cutrer.us>
Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
Cody Cutrer 2024-10-21 23:28:14 -05:00 committed by Ciprian Pascu
parent 0deda9d22b
commit df8d7e28ae
4 changed files with 20 additions and 14 deletions

View File

@ -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<BinarySensor.ChannelConfiguration> {
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<BinarySensor.ChannelConfigur
protected @Nullable String jsonAttributesTopic;
@SerializedName("json_attributes_template")
protected @Nullable String jsonAttributesTemplate;
@SerializedName("json_attributes")
protected @Nullable List<String> jsonAttributes;
}
public BinarySensor(ComponentFactory.ComponentConfiguration componentConfiguration, boolean newStyleChannels) {
@ -77,6 +75,14 @@ public class BinarySensor extends AbstractComponent<BinarySensor.ChannelConfigur
getListener(componentConfiguration, value))
.stateTopic(channelConfiguration.stateTopic, channelConfiguration.getValueTemplate())
.withAutoUpdatePolicy(AutoUpdatePolicy.VETO).build();
if (channelConfiguration.jsonAttributesTopic != null) {
buildChannel(JSON_ATTRIBUTES_CHANNEL_ID, ComponentChannelType.STRING, new TextValue(), "JSON Attributes",
componentConfiguration.getUpdateListener())
.stateTopic(channelConfiguration.jsonAttributesTopic, channelConfiguration.jsonAttributesTemplate)
.withAutoUpdatePolicy(AutoUpdatePolicy.VETO).build();
}
finalizeChannels();
}

View File

@ -18,6 +18,7 @@ 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 org.openhab.core.thing.type.AutoUpdatePolicy;
import com.google.gson.annotations.SerializedName;
@ -57,11 +58,11 @@ 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) {
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();

View File

@ -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<Fan.ChannelConfiguration> 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();

View File

@ -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<Sensor.ChannelConfiguration> {
protected @Nullable String jsonAttributesTopic;
@SerializedName("json_attributes_template")
protected @Nullable String jsonAttributesTemplate;
@SerializedName("json_attributes")
protected @Nullable List<String> jsonAttributes;
}
public Sensor(ComponentFactory.ComponentConfiguration componentConfiguration, boolean newStyleChannels) {
@ -99,11 +97,11 @@ public class Sensor extends AbstractComponent<Sensor.ChannelConfiguration> {
.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();
}