Optimize mqtt platform setup (#102449)
* Optimize mqtt platform setup and correct issue * Avoid coroutine for setup entity from discovery * Avoid extra check * Revert string constants * Add commentpull/102515/head
parent
215febc912
commit
973b8900a9
|
@ -432,15 +432,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
]
|
]
|
||||||
await asyncio.gather(*tasks)
|
await asyncio.gather(*tasks)
|
||||||
|
|
||||||
await asyncio.gather(
|
for _, component in mqtt_data.reload_handlers.items():
|
||||||
*(
|
component()
|
||||||
[
|
|
||||||
mqtt_data.reload_handlers[component]()
|
|
||||||
for component in RELOADABLE_PLATFORMS
|
|
||||||
if component in mqtt_data.reload_handlers
|
|
||||||
]
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
# Fire event
|
# Fire event
|
||||||
hass.bus.async_fire(f"event_{DOMAIN}_reloaded", context=call.context)
|
hass.bus.async_fire(f"event_{DOMAIN}_reloaded", context=call.context)
|
||||||
|
|
|
@ -29,7 +29,7 @@ from homeassistant.const import (
|
||||||
CONF_UNIQUE_ID,
|
CONF_UNIQUE_ID,
|
||||||
CONF_VALUE_TEMPLATE,
|
CONF_VALUE_TEMPLATE,
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
|
||||||
from homeassistant.helpers import (
|
from homeassistant.helpers import (
|
||||||
config_validation as cv,
|
config_validation as cv,
|
||||||
device_registry as dr,
|
device_registry as dr,
|
||||||
|
@ -276,10 +276,14 @@ def async_handle_schema_error(
|
||||||
async def _async_discover(
|
async def _async_discover(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
domain: str,
|
domain: str,
|
||||||
async_setup: partial[Coroutine[Any, Any, None]],
|
setup: partial[CALLBACK_TYPE] | None,
|
||||||
|
async_setup: partial[Coroutine[Any, Any, None]] | None,
|
||||||
discovery_payload: MQTTDiscoveryPayload,
|
discovery_payload: MQTTDiscoveryPayload,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Discover and add an MQTT entity, automation or tag."""
|
"""Discover and add an MQTT entity, automation or tag.
|
||||||
|
|
||||||
|
setup is to be run in the event loop when there is nothing to be awaited.
|
||||||
|
"""
|
||||||
if not mqtt_config_entry_enabled(hass):
|
if not mqtt_config_entry_enabled(hass):
|
||||||
_LOGGER.warning(
|
_LOGGER.warning(
|
||||||
(
|
(
|
||||||
|
@ -292,6 +296,9 @@ async def _async_discover(
|
||||||
return
|
return
|
||||||
discovery_data = discovery_payload.discovery_data
|
discovery_data = discovery_payload.discovery_data
|
||||||
try:
|
try:
|
||||||
|
if setup is not None:
|
||||||
|
setup(discovery_payload)
|
||||||
|
elif async_setup is not None:
|
||||||
await async_setup(discovery_payload)
|
await async_setup(discovery_payload)
|
||||||
except vol.Invalid as err:
|
except vol.Invalid as err:
|
||||||
discovery_hash = discovery_data[ATTR_DISCOVERY_HASH]
|
discovery_hash = discovery_data[ATTR_DISCOVERY_HASH]
|
||||||
|
@ -326,7 +333,7 @@ async def async_setup_non_entity_entry_helper(
|
||||||
hass,
|
hass,
|
||||||
MQTT_DISCOVERY_NEW.format(domain, "mqtt"),
|
MQTT_DISCOVERY_NEW.format(domain, "mqtt"),
|
||||||
functools.partial(
|
functools.partial(
|
||||||
_async_discover, hass, domain, async_setup_from_discovery
|
_async_discover, hass, domain, None, async_setup_from_discovery
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
@ -345,7 +352,8 @@ async def async_setup_entity_entry_helper(
|
||||||
"""Set up entity creation dynamically through MQTT discovery."""
|
"""Set up entity creation dynamically through MQTT discovery."""
|
||||||
mqtt_data = get_mqtt_data(hass)
|
mqtt_data = get_mqtt_data(hass)
|
||||||
|
|
||||||
async def async_setup_from_discovery(
|
@callback
|
||||||
|
def async_setup_from_discovery(
|
||||||
discovery_payload: MQTTDiscoveryPayload,
|
discovery_payload: MQTTDiscoveryPayload,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up an MQTT entity from discovery."""
|
"""Set up an MQTT entity from discovery."""
|
||||||
|
@ -364,12 +372,13 @@ async def async_setup_entity_entry_helper(
|
||||||
hass,
|
hass,
|
||||||
MQTT_DISCOVERY_NEW.format(domain, "mqtt"),
|
MQTT_DISCOVERY_NEW.format(domain, "mqtt"),
|
||||||
functools.partial(
|
functools.partial(
|
||||||
_async_discover, hass, domain, async_setup_from_discovery
|
_async_discover, hass, domain, async_setup_from_discovery, None
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
async def _async_setup_entities() -> None:
|
@callback
|
||||||
|
def _async_setup_entities() -> None:
|
||||||
"""Set up MQTT items from configuration.yaml."""
|
"""Set up MQTT items from configuration.yaml."""
|
||||||
nonlocal entity_class
|
nonlocal entity_class
|
||||||
mqtt_data = get_mqtt_data(hass)
|
mqtt_data = get_mqtt_data(hass)
|
||||||
|
@ -433,7 +442,7 @@ async def async_setup_entity_entry_helper(
|
||||||
mqtt_data.reload_schema[domain] = platform_schema_modern
|
mqtt_data.reload_schema[domain] = platform_schema_modern
|
||||||
# discover manual configured MQTT items
|
# discover manual configured MQTT items
|
||||||
mqtt_data.reload_handlers[domain] = _async_setup_entities
|
mqtt_data.reload_handlers[domain] = _async_setup_entities
|
||||||
await _async_setup_entities()
|
_async_setup_entities()
|
||||||
|
|
||||||
|
|
||||||
def init_entity_id_from_config(
|
def init_entity_id_from_config(
|
||||||
|
|
|
@ -342,9 +342,7 @@ class MqttData:
|
||||||
issues: dict[str, set[str]] = field(default_factory=dict)
|
issues: dict[str, set[str]] = field(default_factory=dict)
|
||||||
last_discovery: float = 0.0
|
last_discovery: float = 0.0
|
||||||
reload_dispatchers: list[CALLBACK_TYPE] = field(default_factory=list)
|
reload_dispatchers: list[CALLBACK_TYPE] = field(default_factory=list)
|
||||||
reload_handlers: dict[str, Callable[[], Coroutine[Any, Any, None]]] = field(
|
reload_handlers: dict[str, CALLBACK_TYPE] = field(default_factory=dict)
|
||||||
default_factory=dict
|
|
||||||
)
|
|
||||||
reload_schema: dict[str, vol.Schema] = field(default_factory=dict)
|
reload_schema: dict[str, vol.Schema] = field(default_factory=dict)
|
||||||
state_write_requests: EntityTopicState = field(default_factory=EntityTopicState)
|
state_write_requests: EntityTopicState = field(default_factory=EntityTopicState)
|
||||||
subscriptions_to_restore: list[Subscription] = field(default_factory=list)
|
subscriptions_to_restore: list[Subscription] = field(default_factory=list)
|
||||||
|
|
Loading…
Reference in New Issue