Fix MQTT publish from a script with templates (#42398)
This commit is contained in:
1 parent
810ef349c3
commit
6380ebd1eb
7 files changed
+126
-44
No files matched your search
@@ -122,7 +122,7 @@ async def test_service_call_with_template_payload_renders_template(hass, mqtt_mo
|
||||
mqtt.async_publish_template(hass, "test/topic", "{{ 1+1 }}")
|
||||
await hass.async_block_till_done()
|
||||
assert mqtt_mock.async_publish.called
|
||||
assert mqtt_mock.async_publish.call_args[0][1] == 2
|
||||
assert mqtt_mock.async_publish.call_args[0][1] == "2"
|
||||
|
||||
|
||||
async def test_service_call_with_payload_doesnt_render_template(hass, mqtt_mock):
|
||||
@@ -1497,3 +1497,46 @@ async def test_debug_info_qos_retain(hass, mqtt_mock):
|
||||
"time": start_dt,
|
||||
"topic": "sensor/abc",
|
||||
} in debug_info_data["entities"][0]["subscriptions"][0]["messages"]
|
||||
|
||||
|
||||
async def test_publish_json_from_template(hass, mqtt_mock):
|
||||
"""Test the publishing of call to services."""
|
||||
test_str = "{'valid': 'python', 'invalid': 'json'}"
|
||||
|
||||
await async_setup_component(
|
||||
hass,
|
||||
"script",
|
||||
{
|
||||
"script": {
|
||||
"test_script_payload": {
|
||||
"sequence": {
|
||||
"service": "mqtt.publish",
|
||||
"data": {"topic": "test-topic", "payload": test_str},
|
||||
}
|
||||
},
|
||||
"test_script_payload_template": {
|
||||
"sequence": {
|
||||
"service": "mqtt.publish",
|
||||
"data": {"topic": "test-topic", "payload_template": test_str},
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
await hass.services.async_call("script", "test_script_payload", blocking=True)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert mqtt_mock.async_publish.called
|
||||
assert mqtt_mock.async_publish.call_args[0][1] == test_str
|
||||
|
||||
mqtt_mock.async_publish.reset_mock()
|
||||
assert not mqtt_mock.async_publish.called
|
||||
|
||||
await hass.services.async_call(
|
||||
"script", "test_script_payload_template", blocking=True
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert mqtt_mock.async_publish.called
|
||||
assert mqtt_mock.async_publish.call_args[0][1] == test_str
|
||||
Reference in new issue
Block a user