Handle template errors on MQTT payload handling (#110180)

* Handle template errors on MQTT payload handling (alt)

* Handle mqtt event en image template errors correctly
This commit is contained in:
Jan Bouwhuis
2024-02-13 10:59:55 +01:00
committed by GitHub
parent ee25f6b960
commit 09f1ec78a5
32 changed files with 859 additions and 22 deletions

View File

@ -1444,3 +1444,73 @@ async def test_skipped_async_ha_write_state(
"""Test a write state command is only called when there is change."""
await mqtt_mock_entry()
await help_test_skipped_async_ha_write_state(hass, topic, payload1, payload2)
VALUE_TEMPLATES_NO_RGB = (
"brightness_template",
"color_temp_template",
"effect_template",
"state_template",
)
@pytest.mark.parametrize(
"hass_config",
[
help_custom_config(
light.DOMAIN,
DEFAULT_CONFIG,
(
{
"state_topic": "test-topic",
value_template: "{{ value_json.some_var * 1 }}",
},
),
)
for value_template in VALUE_TEMPLATES_NO_RGB
],
ids=VALUE_TEMPLATES_NO_RGB,
)
async def test_value_template_fails(
hass: HomeAssistant,
mqtt_mock_entry: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test the rendering of MQTT value template fails."""
await mqtt_mock_entry()
async_fire_mqtt_message(hass, "test-topic", '{"some_var": null }')
assert (
"TypeError: unsupported operand type(s) for *: 'NoneType' and 'int' rendering template"
in caplog.text
)
@pytest.mark.parametrize(
"hass_config",
[
help_custom_config(
light.DOMAIN,
DEFAULT_CONFIG,
(
{
"state_topic": "test-topic",
"red_template": "{{ value_json.r * 1 }}",
"green_template": "{{ value_json.g * 1 }}",
"blue_template": "{{ value_json.b * 1 }}",
},
),
)
],
)
async def test_rgb_value_template_fails(
hass: HomeAssistant,
mqtt_mock_entry: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test the rendering of MQTT value template fails."""
await mqtt_mock_entry()
async_fire_mqtt_message(hass, "test-topic", '{"r": 255, "g": 255, "b": null }')
assert (
"TypeError: unsupported operand type(s) for *: 'NoneType' and 'int' rendering template"
in caplog.text
)