Log entity_id payload and template on MQTT value template error (#98353)
* Log entity_id payload and template on error * Also handle cases with default values. * Do not log payload twice Co-authored-by: Erik Montnemery <erik@montnemery.com> * Tweak test to assert without payload * black --------- Co-authored-by: Erik Montnemery <erik@montnemery.com>
This commit is contained in:
parent
80a5e341b5
commit
5ef6c03610
2 changed files with 64 additions and 7 deletions
|
@ -41,6 +41,7 @@ from .test_common import help_all_subscribe_calls
|
|||
|
||||
from tests.common import (
|
||||
MockConfigEntry,
|
||||
MockEntity,
|
||||
async_fire_mqtt_message,
|
||||
async_fire_time_changed,
|
||||
mock_restore_cache,
|
||||
|
@ -417,6 +418,37 @@ async def test_value_template_value(hass: HomeAssistant) -> None:
|
|||
assert template_state_calls.call_count == 1
|
||||
|
||||
|
||||
async def test_value_template_fails(
|
||||
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
|
||||
) -> None:
|
||||
"""Test the rendering of MQTT value template fails."""
|
||||
|
||||
# test rendering a value fails
|
||||
entity = MockEntity(entity_id="sensor.test")
|
||||
entity.hass = hass
|
||||
tpl = template.Template("{{ value_json.some_var * 2 }}")
|
||||
val_tpl = mqtt.MqttValueTemplate(tpl, hass=hass, entity=entity)
|
||||
with pytest.raises(TypeError):
|
||||
val_tpl.async_render_with_possible_json_value('{"some_var": null }')
|
||||
await hass.async_block_till_done()
|
||||
assert (
|
||||
"TypeError: unsupported operand type(s) for *: 'NoneType' and 'int' "
|
||||
"rendering template for entity 'sensor.test', "
|
||||
"template: '{{ value_json.some_var * 2 }}'"
|
||||
) in caplog.text
|
||||
caplog.clear()
|
||||
with pytest.raises(TypeError):
|
||||
val_tpl.async_render_with_possible_json_value(
|
||||
'{"some_var": null }', default=100
|
||||
)
|
||||
assert (
|
||||
"TypeError: unsupported operand type(s) for *: 'NoneType' and 'int' "
|
||||
"rendering template for entity 'sensor.test', "
|
||||
"template: '{{ value_json.some_var * 2 }}', default value: 100 and payload: "
|
||||
'{"some_var": null }'
|
||||
) in caplog.text
|
||||
|
||||
|
||||
async def test_service_call_without_topic_does_not_publish(
|
||||
hass: HomeAssistant, mqtt_mock_entry: MqttMockHAClientGenerator
|
||||
) -> None:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue