Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mqtt.homeassistant] Add JSON attributes channel to several components #17605

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions bundles/org.openhab.binding.mqtt.homeassistant/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -26,7 +30,8 @@
*/
@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
Expand All @@ -37,6 +42,11 @@ static class ChannelConfiguration extends AbstractChannelConfiguration {
}

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) {
Expand All @@ -46,6 +56,14 @@ public Camera(ComponentFactory.ComponentConfiguration componentConfiguration, bo

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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -115,6 +116,10 @@ static class ChannelConfiguration extends AbstractChannelConfiguration {
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;
Expand Down Expand Up @@ -195,6 +200,14 @@ public Fan(ComponentFactory.ComponentConfiguration componentConfiguration, boole
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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
@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).*$");

/**
Expand Down Expand Up @@ -96,6 +98,13 @@ public Sensor(ComponentFactory.ComponentConfiguration componentConfiguration, bo
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();
}

Expand Down