Skip to content

Commit

Permalink
Avoid constructing mqtt availability template objects when there is n…
Browse files Browse the repository at this point in the history
…o template (#118171)
  • Loading branch information
bdraco authored May 26, 2024
1 parent c368fff commit 8b3cad3
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions homeassistant/components/mqtt/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,10 +498,10 @@ def _availability_setup_from_config(self, config: ConfigType) -> None:
}

for avail_topic_conf in self._avail_topics.values():
avail_topic_conf[CONF_AVAILABILITY_TEMPLATE] = MqttValueTemplate(
avail_topic_conf[CONF_AVAILABILITY_TEMPLATE],
entity=self,
).async_render_with_possible_json_value
if template := avail_topic_conf[CONF_AVAILABILITY_TEMPLATE]:
avail_topic_conf[CONF_AVAILABILITY_TEMPLATE] = MqttValueTemplate(
template, entity=self
).async_render_with_possible_json_value

self._avail_config = config

Expand Down Expand Up @@ -537,7 +537,9 @@ def _availability_message_received(self, msg: ReceiveMessage) -> None:
"""Handle a new received MQTT availability message."""
topic = msg.topic
avail_topic = self._avail_topics[topic]
payload = avail_topic[CONF_AVAILABILITY_TEMPLATE](msg.payload)
template = avail_topic[CONF_AVAILABILITY_TEMPLATE]
payload = template(msg.payload) if template else msg.payload

if payload == avail_topic[CONF_PAYLOAD_AVAILABLE]:
self._available[topic] = True
self._available_latest = True
Expand Down

0 comments on commit 8b3cad3

Please sign in to comment.