Reduce overhead to call get_mqtt_data (#117887)

We call this 100000s of times if there are many subscriptions

https://github.com/home-assistant/core/pull/109030#issuecomment-2123612530
This commit is contained in:
J. Nick Koston 2024-05-21 15:11:27 -10:00 committed by GitHub
parent f429bfa903
commit 4ed45a322c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 3 additions and 0 deletions

View File

@ -241,6 +241,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
websocket_api.async_register_command(hass, websocket_subscribe)
websocket_api.async_register_command(hass, websocket_mqtt_info)
hass.data[DATA_MQTT] = mqtt_data = MqttData(config=mqtt_yaml, client=client)
get_mqtt_data.cache_clear()
client.start(mqtt_data)
# Restore saved subscriptions

View File

@ -3,6 +3,7 @@
from __future__ import annotations
import asyncio
from functools import lru_cache
import os
from pathlib import Path
import tempfile
@ -216,6 +217,7 @@ def valid_birth_will(config: ConfigType) -> ConfigType:
return config
@lru_cache(maxsize=1)
def get_mqtt_data(hass: HomeAssistant) -> MqttData:
"""Return typed MqttData from hass.data[DATA_MQTT]."""
mqtt_data: MqttData = hass.data[DATA_MQTT]