Avoid creating template objects in mqtt sensor if they are not configured (#118194)

This commit is contained in:
J. Nick Koston 2024-05-26 12:09:06 -10:00 committed by GitHub
parent 841d5dfd4f
commit 98d7821f47
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -131,8 +131,12 @@ class MqttSensor(MqttEntity, RestoreSensor):
_expiration_trigger: CALLBACK_TYPE | None = None _expiration_trigger: CALLBACK_TYPE | None = None
_expire_after: int | None _expire_after: int | None
_expired: bool | None _expired: bool | None
_template: Callable[[ReceivePayloadType, PayloadSentinel], ReceivePayloadType] _template: (
_last_reset_template: Callable[[ReceivePayloadType], ReceivePayloadType] Callable[[ReceivePayloadType, PayloadSentinel], ReceivePayloadType] | None
) = None
_last_reset_template: Callable[[ReceivePayloadType], ReceivePayloadType] | None = (
None
)
async def mqtt_async_added_to_hass(self) -> None: async def mqtt_async_added_to_hass(self) -> None:
"""Restore state for entities with expire_after set.""" """Restore state for entities with expire_after set."""
@ -172,8 +176,7 @@ class MqttSensor(MqttEntity, RestoreSensor):
) )
async def async_will_remove_from_hass(self) -> None: async def async_will_remove_from_hass(self) -> None:
"""Remove exprire triggers.""" """Remove expire triggers."""
# Clean up expire triggers
if self._expiration_trigger: if self._expiration_trigger:
_LOGGER.debug("Clean up expire after trigger for %s", self.entity_id) _LOGGER.debug("Clean up expire after trigger for %s", self.entity_id)
self._expiration_trigger() self._expiration_trigger()
@ -202,11 +205,13 @@ class MqttSensor(MqttEntity, RestoreSensor):
else: else:
self._expired = None self._expired = None
if value_template := config.get(CONF_VALUE_TEMPLATE):
self._template = MqttValueTemplate( self._template = MqttValueTemplate(
self._config.get(CONF_VALUE_TEMPLATE), entity=self value_template, entity=self
).async_render_with_possible_json_value ).async_render_with_possible_json_value
if last_reset_template := config.get(CONF_LAST_RESET_VALUE_TEMPLATE):
self._last_reset_template = MqttValueTemplate( self._last_reset_template = MqttValueTemplate(
self._config.get(CONF_LAST_RESET_VALUE_TEMPLATE), entity=self last_reset_template, entity=self
).async_render_with_possible_json_value ).async_render_with_possible_json_value
@callback @callback
@ -226,7 +231,10 @@ class MqttSensor(MqttEntity, RestoreSensor):
self.hass, self._expire_after, self._value_is_expired self.hass, self._expire_after, self._value_is_expired
) )
payload = self._template(msg.payload, PayloadSentinel.DEFAULT) if template := self._template:
payload = template(msg.payload, PayloadSentinel.DEFAULT)
else:
payload = msg.payload
if payload is PayloadSentinel.DEFAULT: if payload is PayloadSentinel.DEFAULT:
return return
new_value = str(payload) new_value = str(payload)
@ -260,8 +268,8 @@ class MqttSensor(MqttEntity, RestoreSensor):
@callback @callback
def _update_last_reset(self, msg: ReceiveMessage) -> None: def _update_last_reset(self, msg: ReceiveMessage) -> None:
payload = self._last_reset_template(msg.payload) template = self._last_reset_template
payload = msg.payload if template is None else template(msg.payload)
if not payload: if not payload:
_LOGGER.debug("Ignoring empty last_reset message from '%s'", msg.topic) _LOGGER.debug("Ignoring empty last_reset message from '%s'", msg.topic)
return return