mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 04:37:06 +00:00
Only decode msg topic once when handling mqtt payloads (#109258)
This commit is contained in:
parent
31094e72a0
commit
5c3bf13ca4
@ -818,25 +818,29 @@ class MQTT:
|
||||
|
||||
@callback
|
||||
def _mqtt_handle_message(self, msg: mqtt.MQTTMessage) -> None:
|
||||
topic = msg.topic
|
||||
# msg.topic is a property that decodes the topic to a string
|
||||
# every time it is accessed. Save the result to avoid
|
||||
# decoding the same topic multiple times.
|
||||
_LOGGER.debug(
|
||||
"Received%s message on %s (qos=%s): %s",
|
||||
" retained" if msg.retain else "",
|
||||
msg.topic,
|
||||
topic,
|
||||
msg.qos,
|
||||
msg.payload[0:8192],
|
||||
)
|
||||
timestamp = dt_util.utcnow()
|
||||
|
||||
subscriptions = self._matching_subscriptions(msg.topic)
|
||||
subscriptions = self._matching_subscriptions(topic)
|
||||
|
||||
for subscription in subscriptions:
|
||||
if msg.retain:
|
||||
retained_topics = self._retained_topics.setdefault(subscription, set())
|
||||
# Skip if the subscription already received a retained message
|
||||
if msg.topic in retained_topics:
|
||||
if topic in retained_topics:
|
||||
continue
|
||||
# Remember the subscription had an initial retained message
|
||||
self._retained_topics[subscription].add(msg.topic)
|
||||
self._retained_topics[subscription].add(topic)
|
||||
|
||||
payload: SubscribePayloadType = msg.payload
|
||||
if subscription.encoding is not None:
|
||||
@ -846,7 +850,7 @@ class MQTT:
|
||||
_LOGGER.warning(
|
||||
"Can't decode payload %s on %s with encoding %s (for %s)",
|
||||
msg.payload[0:8192],
|
||||
msg.topic,
|
||||
topic,
|
||||
subscription.encoding,
|
||||
subscription.job,
|
||||
)
|
||||
@ -854,7 +858,7 @@ class MQTT:
|
||||
self.hass.async_run_hass_job(
|
||||
subscription.job,
|
||||
ReceiveMessage(
|
||||
msg.topic,
|
||||
topic,
|
||||
payload,
|
||||
msg.qos,
|
||||
msg.retain,
|
||||
|
Loading…
x
Reference in New Issue
Block a user