diff --git a/homeassistant/components/weatherflow/config_flow.py b/homeassistant/components/weatherflow/config_flow.py index e7c878fc397e..256e74efc0c6 100644 --- a/homeassistant/components/weatherflow/config_flow.py +++ b/homeassistant/components/weatherflow/config_flow.py @@ -18,6 +18,8 @@ from .const import ( ERROR_MSG_NO_DEVICE_FOUND, ) +DISCOVERY_TIMEOUT = 10 + async def _async_can_discover_devices() -> bool: """Return if there are devices that can be discovered.""" @@ -30,10 +32,11 @@ async def _async_can_discover_devices() -> bool: if not future_event.done(): future_event.set_result(None) - async with WeatherFlowListener() as client, asyncio.timeout(10): + async with WeatherFlowListener() as client: try: client.on(EVENT_DEVICE_DISCOVERED, _async_found) - await future_event + async with asyncio.timeout(DISCOVERY_TIMEOUT): + await future_event except TimeoutError: return False diff --git a/tests/components/weatherflow/test_config_flow.py b/tests/components/weatherflow/test_config_flow.py index 258e395319dc..f0c19014f18e 100644 --- a/tests/components/weatherflow/test_config_flow.py +++ b/tests/components/weatherflow/test_config_flow.py @@ -87,3 +87,30 @@ async def test_devices_with_various_mocks_errors( await hass.async_block_till_done() assert result["type"] is FlowResultType.CREATE_ENTRY assert result["data"] == {} + + +@pytest.mark.usefixtures("mock_setup_entry") +async def test_no_device_discovered( + hass: HomeAssistant, mock_start: AsyncMock, mock_stop: AsyncMock +) -> None: + """Test the discovery timeout shows no device found, then finish the flow.""" + + # The mocked hub is discovered after 0.1 seconds, so it is missed by a + # 0.01 second discovery timeout and found on the retry. + with patch( + "homeassistant.components.weatherflow.config_flow.DISCOVERY_TIMEOUT", 0.01 + ): + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": config_entries.SOURCE_USER}, + ) + + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "user" + assert result["errors"]["base"] == ERROR_MSG_NO_DEVICE_FOUND + + result = await hass.config_entries.flow.async_configure(result["flow_id"]) + + await hass.async_block_till_done() + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["data"] == {}