mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 18:27:09 +00:00
Dispatch the same ReceiveMessage object if the subscription topic is the same (#114769)
This commit is contained in:
parent
535da483b6
commit
3d8a110908
@ -835,6 +835,7 @@ class MQTT:
|
|||||||
timestamp = dt_util.utcnow()
|
timestamp = dt_util.utcnow()
|
||||||
|
|
||||||
subscriptions = self._matching_subscriptions(topic)
|
subscriptions = self._matching_subscriptions(topic)
|
||||||
|
msg_cache_by_subscription_topic: dict[str, ReceiveMessage] = {}
|
||||||
|
|
||||||
for subscription in subscriptions:
|
for subscription in subscriptions:
|
||||||
if msg.retain:
|
if msg.retain:
|
||||||
@ -858,17 +859,24 @@ class MQTT:
|
|||||||
subscription.job,
|
subscription.job,
|
||||||
)
|
)
|
||||||
continue
|
continue
|
||||||
self.hass.async_run_hass_job(
|
subscription_topic = subscription.topic
|
||||||
subscription.job,
|
if subscription_topic not in msg_cache_by_subscription_topic:
|
||||||
ReceiveMessage(
|
# Only make one copy of the message
|
||||||
|
# per topic so we avoid storing a separate
|
||||||
|
# dataclass in memory for each subscriber
|
||||||
|
# to the same topic for retained messages
|
||||||
|
receive_msg = ReceiveMessage(
|
||||||
topic,
|
topic,
|
||||||
payload,
|
payload,
|
||||||
msg.qos,
|
msg.qos,
|
||||||
msg.retain,
|
msg.retain,
|
||||||
subscription.topic,
|
subscription_topic,
|
||||||
timestamp,
|
timestamp,
|
||||||
),
|
|
||||||
)
|
)
|
||||||
|
msg_cache_by_subscription_topic[subscription_topic] = receive_msg
|
||||||
|
else:
|
||||||
|
receive_msg = msg_cache_by_subscription_topic[subscription_topic]
|
||||||
|
self.hass.async_run_hass_job(subscription.job, receive_msg)
|
||||||
self._mqtt_data.state_write_requests.process_write_state_requests(msg)
|
self._mqtt_data.state_write_requests.process_write_state_requests(msg)
|
||||||
|
|
||||||
def _mqtt_on_callback(
|
def _mqtt_on_callback(
|
||||||
|
@ -58,7 +58,7 @@ class PublishMessage:
|
|||||||
retain: bool
|
retain: bool
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass(slots=True, frozen=True)
|
||||||
class ReceiveMessage:
|
class ReceiveMessage:
|
||||||
"""MQTT Message received."""
|
"""MQTT Message received."""
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user