mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Reduce creation of MQTT related discovery tasks (#90801)
* Reduce creation of MQTT related discovery tasks Most of the branching can avoid creating a task as it did not need to await for the majority of cases. We fallback to creating a task for the cases were we do need to await. * comment * revert
This commit is contained in:
parent
d442f2aedb
commit
66b105fb21
@ -218,7 +218,8 @@ async def async_start( # noqa: C901
|
|||||||
discovery_hash = (component, discovery_id)
|
discovery_hash = (component, discovery_id)
|
||||||
if discovery_hash in mqtt_data.discovery_already_discovered or payload:
|
if discovery_hash in mqtt_data.discovery_already_discovered or payload:
|
||||||
|
|
||||||
async def discovery_done(_: Any) -> None:
|
@callback
|
||||||
|
def discovery_done(_: Any) -> None:
|
||||||
pending = mqtt_data.discovery_pending_discovered[discovery_hash][
|
pending = mqtt_data.discovery_pending_discovered[discovery_hash][
|
||||||
"pending"
|
"pending"
|
||||||
]
|
]
|
||||||
|
@ -832,8 +832,37 @@ class MqttDiscoveryUpdate(Entity):
|
|||||||
else:
|
else:
|
||||||
await self.async_remove(force_remove=True)
|
await self.async_remove(force_remove=True)
|
||||||
|
|
||||||
async def discovery_callback(payload: MQTTDiscoveryPayload) -> None:
|
async def _async_process_discovery_update(
|
||||||
"""Handle discovery update."""
|
payload: MQTTDiscoveryPayload,
|
||||||
|
discovery_update: Callable[
|
||||||
|
[MQTTDiscoveryPayload], Coroutine[Any, Any, None]
|
||||||
|
],
|
||||||
|
discovery_data: DiscoveryInfoType,
|
||||||
|
) -> None:
|
||||||
|
"""Process discovery update."""
|
||||||
|
try:
|
||||||
|
await discovery_update(payload)
|
||||||
|
finally:
|
||||||
|
send_discovery_done(self.hass, discovery_data)
|
||||||
|
|
||||||
|
async def _async_process_discovery_update_and_remove(
|
||||||
|
payload: MQTTDiscoveryPayload, discovery_data: DiscoveryInfoType
|
||||||
|
) -> None:
|
||||||
|
"""Process discovery update and remove entity."""
|
||||||
|
self._cleanup_discovery_on_remove()
|
||||||
|
await _async_remove_state_and_registry_entry(self)
|
||||||
|
send_discovery_done(self.hass, discovery_data)
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def discovery_callback(payload: MQTTDiscoveryPayload) -> None:
|
||||||
|
"""Handle discovery update.
|
||||||
|
|
||||||
|
If the payload has changed we will create a task to
|
||||||
|
do the discovery update.
|
||||||
|
|
||||||
|
As this callback can fire when nothing has changed, this
|
||||||
|
is a normal function to avoid task creation until it is needed.
|
||||||
|
"""
|
||||||
_LOGGER.debug(
|
_LOGGER.debug(
|
||||||
"Got update for entity with hash: %s '%s'",
|
"Got update for entity with hash: %s '%s'",
|
||||||
discovery_hash,
|
discovery_hash,
|
||||||
@ -846,17 +875,20 @@ class MqttDiscoveryUpdate(Entity):
|
|||||||
if not payload:
|
if not payload:
|
||||||
# Empty payload: Remove component
|
# Empty payload: Remove component
|
||||||
_LOGGER.info("Removing component: %s", self.entity_id)
|
_LOGGER.info("Removing component: %s", self.entity_id)
|
||||||
self._cleanup_discovery_on_remove()
|
self.hass.async_create_task(
|
||||||
await _async_remove_state_and_registry_entry(self)
|
_async_process_discovery_update_and_remove(
|
||||||
send_discovery_done(self.hass, self._discovery_data)
|
payload, self._discovery_data
|
||||||
|
)
|
||||||
|
)
|
||||||
elif self._discovery_update:
|
elif self._discovery_update:
|
||||||
if old_payload != self._discovery_data[ATTR_DISCOVERY_PAYLOAD]:
|
if old_payload != self._discovery_data[ATTR_DISCOVERY_PAYLOAD]:
|
||||||
# Non-empty, changed payload: Notify component
|
# Non-empty, changed payload: Notify component
|
||||||
_LOGGER.info("Updating component: %s", self.entity_id)
|
_LOGGER.info("Updating component: %s", self.entity_id)
|
||||||
try:
|
self.hass.async_create_task(
|
||||||
await self._discovery_update(payload)
|
_async_process_discovery_update(
|
||||||
finally:
|
payload, self._discovery_update, self._discovery_data
|
||||||
send_discovery_done(self.hass, self._discovery_data)
|
)
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
# Non-empty, unchanged payload: Ignore to avoid changing states
|
# Non-empty, unchanged payload: Ignore to avoid changing states
|
||||||
_LOGGER.debug("Ignoring unchanged update for: %s", self.entity_id)
|
_LOGGER.debug("Ignoring unchanged update for: %s", self.entity_id)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user