Add topic_template for mqtt.publish (#53743)
Co-authored-by: Erik Montnemery <erik@montnemery.com> Co-authored-by: Erik Montnemery <erik@montnemery.com>
This commit is contained in:
parent
8a02d87a17
commit
1bcd62cd32
2 changed files with 137 additions and 10 deletions
|
@ -150,6 +150,103 @@ async def test_service_call_without_topic_does_not_publish(hass, mqtt_mock):
|
|||
assert not mqtt_mock.async_publish.called
|
||||
|
||||
|
||||
async def test_service_call_with_topic_and_topic_template_does_not_publish(
|
||||
hass, mqtt_mock
|
||||
):
|
||||
"""Test the service call with topic/topic template.
|
||||
|
||||
If both 'topic' and 'topic_template' are provided then fail.
|
||||
"""
|
||||
topic = "test/topic"
|
||||
topic_template = "test/{{ 'topic' }}"
|
||||
with pytest.raises(vol.Invalid):
|
||||
await hass.services.async_call(
|
||||
mqtt.DOMAIN,
|
||||
mqtt.SERVICE_PUBLISH,
|
||||
{
|
||||
mqtt.ATTR_TOPIC: topic,
|
||||
mqtt.ATTR_TOPIC_TEMPLATE: topic_template,
|
||||
mqtt.ATTR_PAYLOAD: "payload",
|
||||
},
|
||||
blocking=True,
|
||||
)
|
||||
assert not mqtt_mock.async_publish.called
|
||||
|
||||
|
||||
async def test_service_call_with_invalid_topic_template_does_not_publish(
|
||||
hass, mqtt_mock
|
||||
):
|
||||
"""Test the service call with a problematic topic template."""
|
||||
await hass.services.async_call(
|
||||
mqtt.DOMAIN,
|
||||
mqtt.SERVICE_PUBLISH,
|
||||
{
|
||||
mqtt.ATTR_TOPIC_TEMPLATE: "test/{{ 1 | no_such_filter }}",
|
||||
mqtt.ATTR_PAYLOAD: "payload",
|
||||
},
|
||||
blocking=True,
|
||||
)
|
||||
assert not mqtt_mock.async_publish.called
|
||||
|
||||
|
||||
async def test_service_call_with_template_topic_renders_template(hass, mqtt_mock):
|
||||
"""Test the service call with rendered topic template.
|
||||
|
||||
If 'topic_template' is provided and 'topic' is not, then render it.
|
||||
"""
|
||||
await hass.services.async_call(
|
||||
mqtt.DOMAIN,
|
||||
mqtt.SERVICE_PUBLISH,
|
||||
{
|
||||
mqtt.ATTR_TOPIC_TEMPLATE: "test/{{ 1+1 }}",
|
||||
mqtt.ATTR_PAYLOAD: "payload",
|
||||
},
|
||||
blocking=True,
|
||||
)
|
||||
assert mqtt_mock.async_publish.called
|
||||
assert mqtt_mock.async_publish.call_args[0][0] == "test/2"
|
||||
|
||||
|
||||
async def test_service_call_with_template_topic_renders_invalid_topic(hass, mqtt_mock):
|
||||
"""Test the service call with rendered, invalid topic template.
|
||||
|
||||
If a wildcard topic is rendered, then fail.
|
||||
"""
|
||||
await hass.services.async_call(
|
||||
mqtt.DOMAIN,
|
||||
mqtt.SERVICE_PUBLISH,
|
||||
{
|
||||
mqtt.ATTR_TOPIC_TEMPLATE: "test/{{ '+' if True else 'topic' }}/topic",
|
||||
mqtt.ATTR_PAYLOAD: "payload",
|
||||
},
|
||||
blocking=True,
|
||||
)
|
||||
assert not mqtt_mock.async_publish.called
|
||||
|
||||
|
||||
async def test_service_call_with_invalid_rendered_template_topic_doesnt_render_template(
|
||||
hass, mqtt_mock
|
||||
):
|
||||
"""Test the service call with unrendered template.
|
||||
|
||||
If both 'payload' and 'payload_template' are provided then fail.
|
||||
"""
|
||||
payload = "not a template"
|
||||
payload_template = "a template"
|
||||
with pytest.raises(vol.Invalid):
|
||||
await hass.services.async_call(
|
||||
mqtt.DOMAIN,
|
||||
mqtt.SERVICE_PUBLISH,
|
||||
{
|
||||
mqtt.ATTR_TOPIC: "test/topic",
|
||||
mqtt.ATTR_PAYLOAD: payload,
|
||||
mqtt.ATTR_PAYLOAD_TEMPLATE: payload_template,
|
||||
},
|
||||
blocking=True,
|
||||
)
|
||||
assert not mqtt_mock.async_publish.called
|
||||
|
||||
|
||||
async def test_service_call_with_template_payload_renders_template(hass, mqtt_mock):
|
||||
"""Test the service call with rendered template.
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue