[mqtt.homeassistant] add JSON attributes channel to several components (#17605)

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 15:12:21 -05:00 committed by Ciprian Pascu
parent 40805e9c94
commit 356aa13fb7
4 changed files with 44 additions and 6 deletions

View 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.

View File

@ -13,10 +13,14 @@
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();
}
}

View 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();
}

View 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();
}