Improve MQTT reload performance (#73313)
* Improve MQTT reload performance * Update homeassistant/components/mqtt/__init__.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/mqtt/mixins.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/mqtt/__init__.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
parent
b4a6ccfbc8
commit
de2fade8c6
3 changed files with 37 additions and 16 deletions
|
@ -28,7 +28,14 @@ from homeassistant.data_entry_flow import BaseServiceInfo
|
|||
from homeassistant.exceptions import TemplateError, Unauthorized
|
||||
from homeassistant.helpers import config_validation as cv, event, template
|
||||
from homeassistant.helpers.device_registry import DeviceEntry
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.dispatcher import (
|
||||
async_dispatcher_connect,
|
||||
async_dispatcher_send,
|
||||
)
|
||||
from homeassistant.helpers.reload import (
|
||||
async_integration_yaml_config,
|
||||
async_setup_reload_service,
|
||||
)
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
|
||||
# Loading the config flow file will register the flow
|
||||
|
@ -60,12 +67,14 @@ from .const import ( # noqa: F401
|
|||
DATA_MQTT,
|
||||
DATA_MQTT_CONFIG,
|
||||
DATA_MQTT_RELOAD_NEEDED,
|
||||
DATA_MQTT_UPDATED_CONFIG,
|
||||
DEFAULT_ENCODING,
|
||||
DEFAULT_QOS,
|
||||
DEFAULT_RETAIN,
|
||||
DOMAIN,
|
||||
MQTT_CONNECTED,
|
||||
MQTT_DISCONNECTED,
|
||||
MQTT_RELOADED,
|
||||
PLATFORMS,
|
||||
)
|
||||
from .models import ( # noqa: F401
|
||||
|
@ -227,7 +236,9 @@ async def _async_config_entry_updated(hass: HomeAssistant, entry: ConfigEntry) -
|
|||
await _async_setup_discovery(hass, mqtt_client.conf, entry)
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_setup_entry( # noqa: C901
|
||||
hass: HomeAssistant, entry: ConfigEntry
|
||||
) -> bool:
|
||||
"""Load a config entry."""
|
||||
# Merge basic configuration, and add missing defaults for basic options
|
||||
_merge_basic_config(hass, entry, hass.data.get(DATA_MQTT_CONFIG, {}))
|
||||
|
@ -364,6 +375,17 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||
hass.data[DATA_CONFIG_ENTRY_LOCK] = asyncio.Lock()
|
||||
hass.data[CONFIG_ENTRY_IS_SETUP] = set()
|
||||
|
||||
# Setup reload service. Once support for legacy config is removed in 2022.9, we
|
||||
# should no longer call async_setup_reload_service but instead implement a custom
|
||||
# service
|
||||
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
|
||||
|
||||
async def _async_reload_platforms(_: Event | None) -> None:
|
||||
"""Discover entities for a platform."""
|
||||
config_yaml = await async_integration_yaml_config(hass, DOMAIN) or {}
|
||||
hass.data[DATA_MQTT_UPDATED_CONFIG] = config_yaml.get(DOMAIN, {})
|
||||
async_dispatcher_send(hass, MQTT_RELOADED)
|
||||
|
||||
async def async_forward_entry_setup():
|
||||
"""Forward the config entry setup to the platforms."""
|
||||
async with hass.data[DATA_CONFIG_ENTRY_LOCK]:
|
||||
|
@ -374,6 +396,10 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||
await hass.config_entries.async_forward_entry_setup(
|
||||
entry, component
|
||||
)
|
||||
# Setup reload service after all platforms have loaded
|
||||
entry.async_on_unload(
|
||||
hass.bus.async_listen("event_mqtt_reloaded", _async_reload_platforms)
|
||||
)
|
||||
|
||||
hass.async_create_task(async_forward_entry_setup())
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue