From f71343a3ba6711367c127b93ebf78e75ca118ea5 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 28 Feb 2024 04:06:33 -1000 Subject: [PATCH] Import tplink in the executor to avoid blocking the event loop (#111691) * Import tplink in the executor to avoid blocking the event loop 2024-02-27 22:44:19.908 DEBUG (MainThread) [homeassistant.loader] Component tplink import took 1.620 seconds (loaded_executor=False) * patch out discovery because it happens too fast now --- homeassistant/components/tplink/manifest.json | 1 + tests/components/tplink/test_config_flow.py | 26 ++++++++++++------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/tplink/manifest.json b/homeassistant/components/tplink/manifest.json index a91e7e5a46f..f0a4696fd0b 100644 --- a/homeassistant/components/tplink/manifest.json +++ b/homeassistant/components/tplink/manifest.json @@ -266,6 +266,7 @@ } ], "documentation": "https://www.home-assistant.io/integrations/tplink", + "import_executor": true, "iot_class": "local_polling", "loggers": ["kasa"], "quality_scale": "platinum", diff --git a/tests/components/tplink/test_config_flow.py b/tests/components/tplink/test_config_flow.py index 6163afac564..2834625292c 100644 --- a/tests/components/tplink/test_config_flow.py +++ b/tests/components/tplink/test_config_flow.py @@ -740,8 +740,10 @@ async def test_integration_discovery_with_ip_change( """Test reauth flow.""" mock_connect["connect"].side_effect = SmartDeviceException() mock_config_entry.add_to_hass(hass) - await hass.config_entries.async_setup(mock_config_entry.entry_id) - await hass.async_block_till_done() + with patch("homeassistant.components.tplink.Discover.discover", return_value={}): + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + assert mock_config_entry.state == config_entries.ConfigEntryState.SETUP_RETRY flows = hass.config_entries.flow.async_progress() @@ -791,8 +793,9 @@ async def test_dhcp_discovery_with_ip_change( """Test dhcp discovery with an IP change.""" mock_connect["connect"].side_effect = SmartDeviceException() mock_config_entry.add_to_hass(hass) - await hass.config_entries.async_setup(mock_config_entry.entry_id) - await hass.async_block_till_done() + with patch("homeassistant.components.tplink.Discover.discover", return_value={}): + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() assert mock_config_entry.state == config_entries.ConfigEntryState.SETUP_RETRY flows = hass.config_entries.flow.async_progress() @@ -855,8 +858,9 @@ async def test_reauth_update_from_discovery( """Test reauth flow.""" mock_connect["connect"].side_effect = AuthenticationException mock_config_entry.add_to_hass(hass) - await hass.config_entries.async_setup(mock_config_entry.entry_id) - await hass.async_block_till_done() + with patch("homeassistant.components.tplink.Discover.discover", return_value={}): + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() assert mock_config_entry.state == config_entries.ConfigEntryState.SETUP_ERROR @@ -891,8 +895,9 @@ async def test_reauth_update_from_discovery_with_ip_change( """Test reauth flow.""" mock_connect["connect"].side_effect = AuthenticationException() mock_config_entry.add_to_hass(hass) - await hass.config_entries.async_setup(mock_config_entry.entry_id) - await hass.async_block_till_done() + with patch("homeassistant.components.tplink.Discover.discover", return_value={}): + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() assert mock_config_entry.state == config_entries.ConfigEntryState.SETUP_ERROR flows = hass.config_entries.flow.async_progress() @@ -1121,8 +1126,9 @@ async def test_reauth_update_other_flows( mock_connect["connect"].side_effect = AuthenticationException() mock_config_entry.add_to_hass(hass) mock_config_entry2.add_to_hass(hass) - await hass.config_entries.async_setup(mock_config_entry.entry_id) - await hass.async_block_till_done() + with patch("homeassistant.components.tplink.Discover.discover", return_value={}): + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() assert mock_config_entry2.state == config_entries.ConfigEntryState.SETUP_ERROR assert mock_config_entry.state == config_entries.ConfigEntryState.SETUP_ERROR