From c15f7f304f603c6624b4ecbd894e35d937f25c48 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 26 May 2024 17:07:24 -1000 Subject: [PATCH] Remove unneeded dispatcher in mqtt discovery (#118205) --- homeassistant/components/mqtt/discovery.py | 40 +++++++++------------- tests/components/mqtt/test_discovery.py | 2 -- 2 files changed, 17 insertions(+), 25 deletions(-) diff --git a/homeassistant/components/mqtt/discovery.py b/homeassistant/components/mqtt/discovery.py index 675e7c460c2..29bb44d9e8f 100644 --- a/homeassistant/components/mqtt/discovery.py +++ b/homeassistant/components/mqtt/discovery.py @@ -54,7 +54,6 @@ MQTT_DISCOVERY_UPDATED: SignalTypeFormat[MQTTDiscoveryPayload] = SignalTypeForma MQTT_DISCOVERY_NEW: SignalTypeFormat[MQTTDiscoveryPayload] = SignalTypeFormat( "mqtt_discovery_new_{}_{}" ) -MQTT_DISCOVERY_NEW_COMPONENT = "mqtt_discovery_new_component" MQTT_DISCOVERY_DONE: SignalTypeFormat[Any] = SignalTypeFormat( "mqtt_discovery_done_{}_{}" ) @@ -110,17 +109,11 @@ async def async_start( # noqa: C901 mqtt_data = hass.data[DATA_MQTT] platform_setup_lock: dict[str, asyncio.Lock] = {} - async def _async_component_setup(discovery_payload: MQTTDiscoveryPayload) -> None: - """Perform component set up.""" + @callback + def _async_add_component(discovery_payload: MQTTDiscoveryPayload) -> None: + """Add a component from a discovery message.""" discovery_hash = discovery_payload.discovery_data[ATTR_DISCOVERY_HASH] component, discovery_id = discovery_hash - platform_setup_lock.setdefault(component, asyncio.Lock()) - async with platform_setup_lock[component]: - if component not in mqtt_data.platforms_loaded: - await async_forward_entry_setup_and_setup_discovery( - hass, config_entry, {component} - ) - # Add component message = f"Found new component: {component} {discovery_id}" async_log_discovery_origin_info(message, discovery_payload) mqtt_data.discovery_already_discovered.add(discovery_hash) @@ -128,11 +121,16 @@ async def async_start( # noqa: C901 hass, MQTT_DISCOVERY_NEW.format(component, "mqtt"), discovery_payload ) - mqtt_data.reload_dispatchers.append( - async_dispatcher_connect( - hass, MQTT_DISCOVERY_NEW_COMPONENT, _async_component_setup - ) - ) + async def _async_component_setup( + component: str, discovery_payload: MQTTDiscoveryPayload + ) -> None: + """Perform component set up.""" + async with platform_setup_lock.setdefault(component, asyncio.Lock()): + if component not in mqtt_data.platforms_loaded: + await async_forward_entry_setup_and_setup_discovery( + hass, config_entry, {component} + ) + _async_add_component(discovery_payload) @callback def async_discovery_message_received(msg: ReceiveMessage) -> None: # noqa: C901 @@ -297,7 +295,9 @@ async def async_start( # noqa: C901 if component not in mqtt_data.platforms_loaded and payload: # Load component first - async_dispatcher_send(hass, MQTT_DISCOVERY_NEW_COMPONENT, payload) + config_entry.async_create_task( + hass, _async_component_setup(component, payload) + ) elif already_discovered: # Dispatch update message = f"Component has already been discovered: {component} {discovery_id}, sending update" @@ -306,13 +306,7 @@ async def async_start( # noqa: C901 hass, MQTT_DISCOVERY_UPDATED.format(*discovery_hash), payload ) elif payload: - # Add component - message = f"Found new component: {component} {discovery_id}" - async_log_discovery_origin_info(message, payload) - mqtt_data.discovery_already_discovered.add(discovery_hash) - async_dispatcher_send( - hass, MQTT_DISCOVERY_NEW.format(component, "mqtt"), payload - ) + _async_add_component(payload) else: # Unhandled discovery message async_dispatcher_send( diff --git a/tests/components/mqtt/test_discovery.py b/tests/components/mqtt/test_discovery.py index 148b91b6b20..32a6488b438 100644 --- a/tests/components/mqtt/test_discovery.py +++ b/tests/components/mqtt/test_discovery.py @@ -18,7 +18,6 @@ from homeassistant.components.mqtt.abbreviations import ( from homeassistant.components.mqtt.discovery import ( MQTT_DISCOVERY_DONE, MQTT_DISCOVERY_NEW, - MQTT_DISCOVERY_NEW_COMPONENT, MQTT_DISCOVERY_UPDATED, MQTTDiscoveryPayload, async_start, @@ -1783,7 +1782,6 @@ async def test_update_with_bad_config_not_breaks_discovery( "signal_message", [ MQTT_DISCOVERY_NEW, - MQTT_DISCOVERY_NEW_COMPONENT, MQTT_DISCOVERY_UPDATED, MQTT_DISCOVERY_DONE, ],