Add mqtt encoding support for publishing (#62739)

* encoding support for mqtt publishing - todo tests

* signature allows None values for qos and retain

* common test for mqtt publishing encoding

* better test with command templates

* more tests

* fix tests alarm control panel+tests light basic

* tests light json and template

* add tests vacuum and fix tests light_template
This commit is contained in:
Jan Bouwhuis 2022-01-03 09:03:47 +01:00 committed by GitHub
parent 2cc4d9846b
commit d0c4f0fec4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
33 changed files with 1283 additions and 27 deletions

View file

@ -87,6 +87,7 @@ light:
brightness: true
brightness_scale: 99
"""
import copy
import json
from unittest.mock import call, patch
@ -122,6 +123,7 @@ from .test_common import (
help_test_entity_device_info_with_identifier,
help_test_entity_id_update_discovery_update,
help_test_entity_id_update_subscriptions,
help_test_publishing_with_custom_encoding,
help_test_setting_attribute_via_mqtt_json_message,
help_test_setting_attribute_with_template,
help_test_setting_blocked_attribute_via_mqtt_json_message,
@ -1902,3 +1904,62 @@ async def test_max_mireds(hass, mqtt_mock):
state = hass.states.get("light.test")
assert state.attributes.get("min_mireds") == 153
assert state.attributes.get("max_mireds") == 370
@pytest.mark.parametrize(
"service,topic,parameters,payload,template,tpl_par,tpl_output",
[
(
light.SERVICE_TURN_ON,
"command_topic",
None,
'{"state": "ON"}',
None,
None,
None,
),
(
light.SERVICE_TURN_OFF,
"command_topic",
None,
'{"state": "OFF"}',
None,
None,
None,
),
],
)
async def test_publishing_with_custom_encoding(
hass,
mqtt_mock,
caplog,
service,
topic,
parameters,
payload,
template,
tpl_par,
tpl_output,
):
"""Test publishing MQTT payload with different encoding."""
domain = light.DOMAIN
config = copy.deepcopy(DEFAULT_CONFIG[domain])
if topic == "effect_command_topic":
config["effect_list"] = ["random", "color_loop"]
elif topic == "white_command_topic":
config["rgb_command_topic"] = "some-cmd-topic"
await help_test_publishing_with_custom_encoding(
hass,
mqtt_mock,
caplog,
domain,
config,
service,
topic,
parameters,
payload,
template,
tpl_par=tpl_par,
tpl_output=tpl_output,
)