Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log entity_id payload and template on MQTT value template error #98353

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 33 additions & 7 deletions homeassistant/components/mqtt/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,22 @@ def async_render_with_possible_json_value(
values,
self._value_template,
)
rendered_payload = (
self._value_template.async_render_with_possible_json_value(
payload, variables=values
try:
rendered_payload = (
self._value_template.async_render_with_possible_json_value(
payload, variables=values
)
)
)
except Exception as ex: # pylint: disable=broad-except
jbouwh marked this conversation as resolved.
Show resolved Hide resolved
_LOGGER.error(
"%s: %s rendering template for entity '%s', template: "
"'%s'",
type(ex).__name__,
ex,
self._entity.entity_id if self._entity else "n/a",
self._value_template.template,
)
raise ex
return rendered_payload

_LOGGER.debug(
Expand All @@ -248,9 +259,24 @@ def async_render_with_possible_json_value(
default,
self._value_template,
)
rendered_payload = self._value_template.async_render_with_possible_json_value(
payload, default, variables=values
)
try:
rendered_payload = (
self._value_template.async_render_with_possible_json_value(
payload, default, variables=values
)
)
except Exception as ex: # pylint: disable=broad-except
_LOGGER.error(
"%s: %s rendering template for entity '%s', template: "
"'%s', default value: %s and payload: %s",
type(ex).__name__,
ex,
self._entity.entity_id if self._entity else "n/a",
self._value_template.template,
default,
payload,
)
raise ex
return rendered_payload


Expand Down
33 changes: 33 additions & 0 deletions tests/components/mqtt/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

from tests.common import (
MockConfigEntry,
MockEntity,
async_fire_mqtt_message,
async_fire_time_changed,
mock_restore_cache,
Expand Down Expand Up @@ -417,6 +418,38 @@ 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 }}' with payload: "
'{"some_var": null }'
) 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:
Expand Down