From 04101b044b2618989ab35753219b62d66c60440a Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 25 May 2024 16:13:54 -1000 Subject: [PATCH] Avoid constructing mqtt json attrs template if its not defined (#118146) --- homeassistant/components/mqtt/mixins.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/mqtt/mixins.py b/homeassistant/components/mqtt/mixins.py index 568c0aebd06..e3ac3676f2b 100644 --- a/homeassistant/components/mqtt/mixins.py +++ b/homeassistant/components/mqtt/mixins.py @@ -387,9 +387,10 @@ class MqttAttributesMixin(Entity): def _attributes_prepare_subscribe_topics(self) -> None: """(Re)Subscribe to topics.""" - self._attr_tpl = MqttValueTemplate( - self._attributes_config.get(CONF_JSON_ATTRS_TEMPLATE), entity=self - ).async_render_with_possible_json_value + if template := self._attributes_config.get(CONF_JSON_ATTRS_TEMPLATE): + self._attr_tpl = MqttValueTemplate( + template, entity=self + ).async_render_with_possible_json_value self._attributes_sub_state = async_prepare_subscribe_topics( self.hass, self._attributes_sub_state, @@ -422,9 +423,9 @@ class MqttAttributesMixin(Entity): @callback def _attributes_message_received(self, msg: ReceiveMessage) -> None: """Update extra state attributes.""" - if TYPE_CHECKING: - assert self._attr_tpl is not None - payload = self._attr_tpl(msg.payload) + payload = ( + self._attr_tpl(msg.payload) if self._attr_tpl is not None else msg.payload + ) try: json_dict = json_loads(payload) if isinstance(payload, str) else None except ValueError: