Reduce overhead to process and publish MQTT messages (#99457)

This commit is contained in:
J. Nick Koston 2023-09-01 16:41:34 -04:00 committed by GitHub
parent 5a8fc43212
commit 7c87b38a23
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -110,7 +110,7 @@ def publish(
encoding: str | None = DEFAULT_ENCODING,
) -> None:
"""Publish message to a MQTT topic."""
hass.add_job(async_publish, hass, topic, payload, qos, retain, encoding)
hass.create_task(async_publish(hass, topic, payload, qos, retain, encoding))
async def async_publish(
@ -376,6 +376,7 @@ class MQTT:
) -> None:
"""Initialize Home Assistant MQTT client."""
self.hass = hass
self.loop = hass.loop
self.config_entry = config_entry
self.conf = conf
@ -806,7 +807,7 @@ class MQTT:
self, _mqttc: mqtt.Client, _userdata: None, msg: mqtt.MQTTMessage
) -> None:
"""Message received callback."""
self.hass.add_job(self._mqtt_handle_message, msg)
self.loop.call_soon_threadsafe(self._mqtt_handle_message, msg)
@lru_cache(None) # pylint: disable=method-cache-max-size-none
def _matching_subscriptions(self, topic: str) -> list[Subscription]: