mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Avoid constructing mqtt json attrs template if its not defined (#118146)
This commit is contained in:
parent
0ae5275f01
commit
04101b044b
@ -387,9 +387,10 @@ class MqttAttributesMixin(Entity):
|
|||||||
|
|
||||||
def _attributes_prepare_subscribe_topics(self) -> None:
|
def _attributes_prepare_subscribe_topics(self) -> None:
|
||||||
"""(Re)Subscribe to topics."""
|
"""(Re)Subscribe to topics."""
|
||||||
self._attr_tpl = MqttValueTemplate(
|
if template := self._attributes_config.get(CONF_JSON_ATTRS_TEMPLATE):
|
||||||
self._attributes_config.get(CONF_JSON_ATTRS_TEMPLATE), entity=self
|
self._attr_tpl = MqttValueTemplate(
|
||||||
).async_render_with_possible_json_value
|
template, entity=self
|
||||||
|
).async_render_with_possible_json_value
|
||||||
self._attributes_sub_state = async_prepare_subscribe_topics(
|
self._attributes_sub_state = async_prepare_subscribe_topics(
|
||||||
self.hass,
|
self.hass,
|
||||||
self._attributes_sub_state,
|
self._attributes_sub_state,
|
||||||
@ -422,9 +423,9 @@ class MqttAttributesMixin(Entity):
|
|||||||
@callback
|
@callback
|
||||||
def _attributes_message_received(self, msg: ReceiveMessage) -> None:
|
def _attributes_message_received(self, msg: ReceiveMessage) -> None:
|
||||||
"""Update extra state attributes."""
|
"""Update extra state attributes."""
|
||||||
if TYPE_CHECKING:
|
payload = (
|
||||||
assert self._attr_tpl is not None
|
self._attr_tpl(msg.payload) if self._attr_tpl is not None else msg.payload
|
||||||
payload = self._attr_tpl(msg.payload)
|
)
|
||||||
try:
|
try:
|
||||||
json_dict = json_loads(payload) if isinstance(payload, str) else None
|
json_dict = json_loads(payload) if isinstance(payload, str) else None
|
||||||
except ValueError:
|
except ValueError:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user