Allow to omit the payload attribute to MQTT publish action to allow an empty payload to be sent by default (#137595)

Allow to omit the payload attribute to MQTT publish actionto allow an empty payload to be sent by default
pull/137688/head
Jan Bouwhuis 2025-02-06 21:11:39 +01:00 committed by Franck Nijhof
parent 7b20299de7
commit e09ae1c83d
No known key found for this signature in database
GPG Key ID: D62583BA8AB11CA3
4 changed files with 21 additions and 7 deletions

View File

@ -236,7 +236,7 @@ CONFIG_SCHEMA = vol.Schema(
MQTT_PUBLISH_SCHEMA = vol.Schema( MQTT_PUBLISH_SCHEMA = vol.Schema(
{ {
vol.Required(ATTR_TOPIC): valid_publish_topic, vol.Required(ATTR_TOPIC): valid_publish_topic,
vol.Required(ATTR_PAYLOAD): cv.string, vol.Required(ATTR_PAYLOAD, default=None): vol.Any(cv.string, None),
vol.Optional(ATTR_EVALUATE_PAYLOAD): cv.boolean, vol.Optional(ATTR_EVALUATE_PAYLOAD): cv.boolean,
vol.Optional(ATTR_QOS, default=DEFAULT_QOS): valid_qos_schema, vol.Optional(ATTR_QOS, default=DEFAULT_QOS): valid_qos_schema,
vol.Optional(ATTR_RETAIN, default=DEFAULT_RETAIN): cv.boolean, vol.Optional(ATTR_RETAIN, default=DEFAULT_RETAIN): cv.boolean,

View File

@ -8,7 +8,6 @@ publish:
selector: selector:
text: text:
payload: payload:
required: true
example: "The temperature is {{ states('sensor.temperature') }}" example: "The temperature is {{ states('sensor.temperature') }}"
selector: selector:
template: template:

View File

@ -246,11 +246,7 @@
}, },
"payload": { "payload": {
"name": "Payload", "name": "Payload",
"description": "The payload to publish." "description": "The payload to publish. Publishes an empty message if not provided."
},
"payload_template": {
"name": "Payload template",
"description": "Template to render as a payload value. If a payload is provided, the template is ignored."
}, },
"qos": { "qos": {
"name": "QoS", "name": "QoS",

View File

@ -391,6 +391,25 @@ async def test_service_call_with_ascii_qos_retain_flags(
blocking=True, blocking=True,
) )
assert mqtt_mock.async_publish.called assert mqtt_mock.async_publish.called
assert mqtt_mock.async_publish.call_args[0][1] == ""
assert mqtt_mock.async_publish.call_args[0][2] == 2
assert not mqtt_mock.async_publish.call_args[0][3]
mqtt_mock.reset_mock()
# Test service call without payload
await hass.services.async_call(
mqtt.DOMAIN,
mqtt.SERVICE_PUBLISH,
{
mqtt.ATTR_TOPIC: "test/topic",
mqtt.ATTR_QOS: "2",
mqtt.ATTR_RETAIN: "no",
},
blocking=True,
)
assert mqtt_mock.async_publish.called
assert mqtt_mock.async_publish.call_args[0][1] is None
assert mqtt_mock.async_publish.call_args[0][2] == 2 assert mqtt_mock.async_publish.call_args[0][2] == 2
assert not mqtt_mock.async_publish.call_args[0][3] assert not mqtt_mock.async_publish.call_args[0][3]