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
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
32 changed files with 859 additions and 22 deletions

View file

@ -29,6 +29,8 @@ if TYPE_CHECKING:
from .discovery import MQTTDiscoveryPayload
from .tag import MQTTTagScanner
from .const import TEMPLATE_ERRORS
class PayloadSentinel(StrEnum):
"""Sentinel for `async_render_with_possible_json_value`."""
@ -247,7 +249,7 @@ class MqttValueTemplate:
payload, variables=values
)
)
except Exception as exc:
except TEMPLATE_ERRORS as exc:
_LOGGER.error(
"%s: %s rendering template for entity '%s', template: '%s'",
type(exc).__name__,
@ -255,7 +257,7 @@ class MqttValueTemplate:
self._entity.entity_id if self._entity else "n/a",
self._value_template.template,
)
raise exc
raise
return rendered_payload
_LOGGER.debug(
@ -274,18 +276,18 @@ class MqttValueTemplate:
payload, default, variables=values
)
)
except Exception as ex:
except TEMPLATE_ERRORS as exc:
_LOGGER.error(
"%s: %s rendering template for entity '%s', template: "
"'%s', default value: %s and payload: %s",
type(ex).__name__,
ex,
type(exc).__name__,
exc,
self._entity.entity_id if self._entity else "n/a",
self._value_template.template,
default,
payload,
)
raise ex
raise
return rendered_payload