From 59b7f8d103b0cffaf73ff9c09819bf973ce6a6ca Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 28 Feb 2024 16:34:08 -1000 Subject: [PATCH] Fix tplink blocking startup by waiting for discovery (#111788) * Fix tplink blocking statup by waiting for discovery * remove started --- homeassistant/components/tplink/__init__.py | 8 +++----- tests/components/tplink/test_init.py | 9 ++------- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/homeassistant/components/tplink/__init__.py b/homeassistant/components/tplink/__init__.py index e2342e617de..b8510f7ef81 100644 --- a/homeassistant/components/tplink/__init__.py +++ b/homeassistant/components/tplink/__init__.py @@ -28,7 +28,6 @@ from homeassistant.const import ( CONF_MODEL, CONF_PASSWORD, CONF_USERNAME, - EVENT_HOMEASSISTANT_STARTED, ) from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady @@ -112,14 +111,13 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up the TP-Link component.""" hass.data.setdefault(DOMAIN, {}) - if discovered_devices := await async_discover_devices(hass): - async_trigger_discovery(hass, discovered_devices) - async def _async_discovery(*_: Any) -> None: if discovered := await async_discover_devices(hass): async_trigger_discovery(hass, discovered) - hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STARTED, _async_discovery) + hass.async_create_background_task( + _async_discovery(), "tplink first discovery", eager_start=True + ) async_track_time_interval( hass, _async_discovery, DISCOVERY_INTERVAL, cancel_on_shutdown=True ) diff --git a/tests/components/tplink/test_init.py b/tests/components/tplink/test_init.py index 7bee7823013..4af4a80c927 100644 --- a/tests/components/tplink/test_init.py +++ b/tests/components/tplink/test_init.py @@ -17,7 +17,6 @@ from homeassistant.const import ( CONF_HOST, CONF_PASSWORD, CONF_USERNAME, - EVENT_HOMEASSISTANT_STARTED, STATE_ON, STATE_UNAVAILABLE, ) @@ -52,17 +51,13 @@ async def test_configuring_tplink_causes_discovery(hass: HomeAssistant) -> None: call_count = len(discover.mock_calls) assert discover.mock_calls - hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED) + async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=15)) await hass.async_block_till_done() assert len(discover.mock_calls) == call_count * 2 - async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=15)) - await hass.async_block_till_done() - assert len(discover.mock_calls) == call_count * 3 - async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=30)) await hass.async_block_till_done() - assert len(discover.mock_calls) == call_count * 4 + assert len(discover.mock_calls) == call_count * 3 async def test_config_entry_reload(hass: HomeAssistant) -> None: