Add topic_template for mqtt.publish#53743
Conversation
|
Hey there @emontnemery, mind taking a look at this pull request as it has been labeled with an integration ( |
This documents the feature from home-assistant/core#53743
This documents the feature from home-assistant/core#53743 and adds an example of its usage.
? Can you explain why a Here's a simple example I recently posted in the community forum. - alias: 433 TempChannel Multiplexer
trigger:
- platform: mqtt
topic: home/433
action:
- service: mqtt.publish
data:
topic: 'home/sensor{{ trigger.payload_json.channel }}'
payload: '{{ trigger.payload }}'When tested with the following payload: {"time":"2021-07-28 21:27:42","protocol":113,"model":"AmbientWeather-WH31E","id":238,"channel":6,"battery_ok":1,"temperature_F":55.58,"humidity":44,"data":"7600000000","mic":"CRC","mod":"FSK","freq1":433.9768,"freq2":433.84298,"rssi":-0.13023,"snr":13.01577,"noise":-13.146}it correctly re-publishes the payload to the topic Unless I have misunderstood the purpose of this PR, it seems to me there's no need for FWIW, |
|
@tdejneka This is a different context; templating is supported within automation actions, where the action's variables get rendered to include the trigger data, like in your example. Where type: button
name: Set power on behavior
tap_action:
action: call-service
service: mqtt.publish
service_data:
topic_template: 'zigbee2mqtt/{{ states("input_select.light_entity_id") }}/set' # Supported by this PR
payload_template: '{{ states("sensor.hue_power_on_behavior_payload") }}'and for this to work, I need both topic and payload to be templated.
I agree a similar cleanup would be good to see, just have to bear in mind breaking backwards compatibility as someone may have inadvertently have Jinja syntax in their MQTT service call. Separately, I imagine the current situation around templating could lead to unexpected results if someone tries to use |
emontnemery
left a comment
There was a problem hiding this comment.
This looks good overall @davidjb, thanks! 👍
I left a few minor comments.
| parse_result=False | ||
| ) | ||
| ) | ||
| except template.jinja2.TemplateError as exc: |
There was a problem hiding this comment.
We need to catch homeassistant.exception.TemplateError too
Co-authored-by: Erik Montnemery <erik@montnemery.com>
9b8ebf8 to
116178e
Compare
Thanks @emontnemery - all of your suggestions are merged + added the additional test + am catching both types of template error now. Let me know if anything else's needed to merge. Thanks for the review and feedback! |
|
Are you aware of the test failures since this was merged in? |
|
See https://github.com/home-assistant/core/runs/4226958170?check_suite_focus=true |
Co-authored-by: Erik Montnemery <erik@montnemery.com> Co-authored-by: Erik Montnemery <erik@montnemery.com>
Proposed change
Adds ability to template the topic for the
mqtt.publishservice, by way of an additional optiontopic_template; this is implemented in the same vein aspayloadvspayload_template.Being able to template the topic allows an MQTT endpoint to be dynamically specified, such as through state, input entities and any other source available in templates. As a worked example, this feature allows me to create an
input_selectwith sensor entity IDs and then publish to MQTT tohome/dynamic_entity_id/set; prior to this PR, the topic always needed to be hardcoded.In terms of implementation, the schema is set to require one of either
topicortopic_templateand checks validity of rendered output coming from thetopic_templateto ensure it's suitable for MQTT. Tests have been added to test this and test the rendering of the topic.Type of change
Additional information
Checklist
black --fast homeassistant tests)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest.requirements_all.txt.Updated by running
python3 -m script.gen_requirements_all..coveragerc.The integration reached or maintains the following Integration Quality Scale:
To help with the load of incoming pull requests: