Add comment to explain not using the core API in MQTT client (#108942)

This commit is contained in:
Jan Bouwhuis 2024-01-27 13:05:31 +01:00 committed by GitHub
parent dec9eb9ae4
commit 677b06f502
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -216,6 +216,10 @@ def subscribe(
def remove() -> None:
"""Remove listener convert."""
# MQTT messages tend to be high volume,
# and since they come in via a thread and need to be processed in the event loop,
# we want to avoid hass.add_job since most of the time is spent calling
# inspect to figure out how to run the callback.
hass.loop.call_soon_threadsafe(async_remove)
return remove
@ -796,6 +800,10 @@ class MQTT:
self, _mqttc: mqtt.Client, _userdata: None, msg: mqtt.MQTTMessage
) -> None:
"""Message received callback."""
# MQTT messages tend to be high volume,
# and since they come in via a thread and need to be processed in the event loop,
# we want to avoid hass.add_job since most of the time is spent calling
# inspect to figure out how to run the callback.
self.loop.call_soon_threadsafe(self._mqtt_handle_message, msg)
@lru_cache(None) # pylint: disable=method-cache-max-size-none