diff --git a/requirements_test.txt b/requirements_test.txt index 01ae29fa166..68e770c1b7b 100644 --- a/requirements_test.txt +++ b/requirements_test.txt @@ -19,7 +19,7 @@ pydantic==2.11.3 pylint==3.3.7 pylint-per-file-ignores==1.4.0 pipdeptree==2.26.1 -pytest-asyncio==0.26.0 +pytest-asyncio==1.0.0 pytest-aiohttp==1.1.0 pytest-cov==6.0.0 pytest-freezer==0.4.9 diff --git a/tests/components/auth/conftest.py b/tests/components/auth/conftest.py index c7c92411ce8..7189d017eb7 100644 --- a/tests/components/auth/conftest.py +++ b/tests/components/auth/conftest.py @@ -1,7 +1,5 @@ """Test configuration for auth.""" -from asyncio import AbstractEventLoop - import pytest from tests.typing import ClientSessionGenerator @@ -9,7 +7,6 @@ from tests.typing import ClientSessionGenerator @pytest.fixture def aiohttp_client( - event_loop: AbstractEventLoop, aiohttp_client: ClientSessionGenerator, socket_enabled: None, ) -> ClientSessionGenerator: diff --git a/tests/components/emulated_hue/test_upnp.py b/tests/components/emulated_hue/test_upnp.py index b16fda536c6..cf14d143447 100644 --- a/tests/components/emulated_hue/test_upnp.py +++ b/tests/components/emulated_hue/test_upnp.py @@ -1,6 +1,5 @@ """The tests for the emulated Hue component.""" -from asyncio import AbstractEventLoop from collections.abc import Generator from http import HTTPStatus import json @@ -38,7 +37,6 @@ class MockTransport: @pytest.fixture def aiohttp_client( - event_loop: AbstractEventLoop, aiohttp_client: ClientSessionGenerator, socket_enabled: None, ) -> ClientSessionGenerator: diff --git a/tests/components/frontend/test_init.py b/tests/components/frontend/test_init.py index 5a682277176..f28742cdd0a 100644 --- a/tests/components/frontend/test_init.py +++ b/tests/components/frontend/test_init.py @@ -1,6 +1,5 @@ """The tests for Home Assistant frontend.""" -from asyncio import AbstractEventLoop from collections.abc import Generator from http import HTTPStatus from pathlib import Path @@ -95,7 +94,6 @@ async def frontend_themes(hass: HomeAssistant) -> None: @pytest.fixture def aiohttp_client( - event_loop: AbstractEventLoop, aiohttp_client: ClientSessionGenerator, socket_enabled: None, ) -> ClientSessionGenerator: diff --git a/tests/components/homekit/conftest.py b/tests/components/homekit/conftest.py index 6bdad5d2b4c..777e44ea681 100644 --- a/tests/components/homekit/conftest.py +++ b/tests/components/homekit/conftest.py @@ -1,6 +1,6 @@ """HomeKit session fixtures.""" -from asyncio import AbstractEventLoop +import asyncio from collections.abc import Generator from contextlib import suppress import os @@ -26,12 +26,13 @@ def iid_storage(hass: HomeAssistant) -> Generator[AccessoryIIDStorage]: @pytest.fixture def run_driver( - hass: HomeAssistant, event_loop: AbstractEventLoop, iid_storage: AccessoryIIDStorage + hass: HomeAssistant, iid_storage: AccessoryIIDStorage ) -> Generator[HomeDriver]: """Return a custom AccessoryDriver instance for HomeKit accessory init. This mock does not mock async_stop, so the driver will not be stopped """ + event_loop = asyncio.get_event_loop() with ( patch("pyhap.accessory_driver.AsyncZeroconf"), patch("pyhap.accessory_driver.AccessoryEncoder"), @@ -55,9 +56,10 @@ def run_driver( @pytest.fixture def hk_driver( - hass: HomeAssistant, event_loop: AbstractEventLoop, iid_storage: AccessoryIIDStorage + hass: HomeAssistant, iid_storage: AccessoryIIDStorage ) -> Generator[HomeDriver]: """Return a custom AccessoryDriver instance for HomeKit accessory init.""" + event_loop = asyncio.get_event_loop() with ( patch("pyhap.accessory_driver.AsyncZeroconf"), patch("pyhap.accessory_driver.AccessoryEncoder"), @@ -85,11 +87,11 @@ def hk_driver( @pytest.fixture def mock_hap( hass: HomeAssistant, - event_loop: AbstractEventLoop, iid_storage: AccessoryIIDStorage, mock_zeroconf: MagicMock, ) -> Generator[HomeDriver]: """Return a custom AccessoryDriver instance for HomeKit accessory init.""" + event_loop = asyncio.get_event_loop() with ( patch("pyhap.accessory_driver.AsyncZeroconf"), patch("pyhap.accessory_driver.AccessoryEncoder"), diff --git a/tests/components/http/conftest.py b/tests/components/http/conftest.py index 5c10278040c..6a16956bded 100644 --- a/tests/components/http/conftest.py +++ b/tests/components/http/conftest.py @@ -1,7 +1,5 @@ """Test configuration for http.""" -from asyncio import AbstractEventLoop - import pytest from tests.typing import ClientSessionGenerator @@ -9,7 +7,6 @@ from tests.typing import ClientSessionGenerator @pytest.fixture def aiohttp_client( - event_loop: AbstractEventLoop, aiohttp_client: ClientSessionGenerator, socket_enabled: None, ) -> ClientSessionGenerator: diff --git a/tests/components/image_processing/test_init.py b/tests/components/image_processing/test_init.py index 6ff6d925d7e..ed6b3faafdc 100644 --- a/tests/components/image_processing/test_init.py +++ b/tests/components/image_processing/test_init.py @@ -1,6 +1,5 @@ """The tests for the image_processing component.""" -from asyncio import AbstractEventLoop from collections.abc import Callable from unittest.mock import PropertyMock, patch @@ -26,7 +25,6 @@ async def setup_homeassistant(hass: HomeAssistant): @pytest.fixture def aiohttp_unused_port_factory( - event_loop: AbstractEventLoop, unused_tcp_port_factory: Callable[[], int], socket_enabled: None, ) -> Callable[[], int]: diff --git a/tests/components/motioneye/test_camera.py b/tests/components/motioneye/test_camera.py index d9a9a847b63..5583d7ce45d 100644 --- a/tests/components/motioneye/test_camera.py +++ b/tests/components/motioneye/test_camera.py @@ -1,6 +1,5 @@ """Test the motionEye camera.""" -from asyncio import AbstractEventLoop from collections.abc import Callable import copy from unittest.mock import AsyncMock, Mock, call @@ -67,7 +66,6 @@ from tests.common import async_fire_time_changed @pytest.fixture def aiohttp_server( - event_loop: AbstractEventLoop, aiohttp_server: Callable[[], TestServer], socket_enabled: None, ) -> Callable[[], TestServer]: diff --git a/tests/conftest.py b/tests/conftest.py index d13384055b1..c326f57ca2f 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -330,18 +330,18 @@ def long_repr_strings() -> Generator[None]: @pytest.fixture(autouse=True) -def enable_event_loop_debug(event_loop: asyncio.AbstractEventLoop) -> None: +def enable_event_loop_debug() -> None: """Enable event loop debug mode.""" - event_loop.set_debug(True) + asyncio.get_event_loop().set_debug(True) @pytest.fixture(autouse=True) def verify_cleanup( - event_loop: asyncio.AbstractEventLoop, expected_lingering_tasks: bool, expected_lingering_timers: bool, ) -> Generator[None]: """Verify that the test has cleaned up resources correctly.""" + event_loop = asyncio.get_event_loop() threads_before = frozenset(threading.enumerate()) tasks_before = asyncio.all_tasks(event_loop) yield @@ -492,9 +492,7 @@ def aiohttp_client_cls() -> type[CoalescingClient]: @pytest.fixture -def aiohttp_client( - event_loop: asyncio.AbstractEventLoop, -) -> Generator[ClientSessionGenerator]: +def aiohttp_client() -> Generator[ClientSessionGenerator]: """Override the default aiohttp_client since 3.x does not support aiohttp_client_cls. Remove this when upgrading to 4.x as aiohttp_client_cls @@ -504,7 +502,7 @@ def aiohttp_client( aiohttp_client(server, **kwargs) aiohttp_client(raw_server, **kwargs) """ - loop = event_loop + loop = asyncio.get_event_loop() clients = [] async def go( diff --git a/tests/scripts/test_auth.py b/tests/scripts/test_auth.py index e9b6f4f718f..31b80bb410d 100644 --- a/tests/scripts/test_auth.py +++ b/tests/scripts/test_auth.py @@ -1,7 +1,7 @@ """Test the auth script to manage local users.""" import argparse -from asyncio import AbstractEventLoop +import asyncio from collections.abc import Generator import logging from typing import Any @@ -143,7 +143,7 @@ async def test_change_password_invalid_user( data.validate_login("invalid-user", "new-pass") -def test_parsing_args(event_loop: AbstractEventLoop) -> None: +async def test_parsing_args() -> None: """Test we parse args correctly.""" called = False @@ -158,7 +158,8 @@ def test_parsing_args(event_loop: AbstractEventLoop) -> None: args = Mock(config="/somewhere/config", func=mock_func) + event_loop = asyncio.get_event_loop() with patch("argparse.ArgumentParser.parse_args", return_value=args): - script_auth.run(None) + await event_loop.run_in_executor(None, script_auth.run, None) assert called, "Mock function did not get called" diff --git a/tests/scripts/test_check_config.py b/tests/scripts/test_check_config.py index 7e3c1abbb22..3a2007060ae 100644 --- a/tests/scripts/test_check_config.py +++ b/tests/scripts/test_check_config.py @@ -55,7 +55,7 @@ def normalize_yaml_files(check_dict): @pytest.mark.parametrize("hass_config_yaml", [BAD_CORE_CONFIG]) -@pytest.mark.usefixtures("mock_is_file", "event_loop", "mock_hass_config_yaml") +@pytest.mark.usefixtures("mock_is_file", "mock_hass_config_yaml") def test_bad_core_config() -> None: """Test a bad core config setup.""" res = check_config.check(get_test_config_dir()) @@ -65,7 +65,7 @@ def test_bad_core_config() -> None: @pytest.mark.parametrize("hass_config_yaml", [BASE_CONFIG + "light:\n platform: demo"]) -@pytest.mark.usefixtures("mock_is_file", "event_loop", "mock_hass_config_yaml") +@pytest.mark.usefixtures("mock_is_file", "mock_hass_config_yaml") def test_config_platform_valid() -> None: """Test a valid platform setup.""" res = check_config.check(get_test_config_dir()) @@ -96,7 +96,7 @@ def test_config_platform_valid() -> None: ), ], ) -@pytest.mark.usefixtures("mock_is_file", "event_loop", "mock_hass_config_yaml") +@pytest.mark.usefixtures("mock_is_file", "mock_hass_config_yaml") def test_component_platform_not_found(platforms: set[str], error: str) -> None: """Test errors if component or platform not found.""" # Make sure they don't exist @@ -121,7 +121,7 @@ def test_component_platform_not_found(platforms: set[str], error: str) -> None: } ], ) -@pytest.mark.usefixtures("mock_is_file", "event_loop", "mock_hass_config_yaml") +@pytest.mark.usefixtures("mock_is_file", "mock_hass_config_yaml") def test_secrets() -> None: """Test secrets config checking method.""" res = check_config.check(get_test_config_dir(), True) @@ -151,7 +151,7 @@ def test_secrets() -> None: @pytest.mark.parametrize( "hass_config_yaml", [BASE_CONFIG + ' packages:\n p1:\n group: ["a"]'] ) -@pytest.mark.usefixtures("mock_is_file", "event_loop", "mock_hass_config_yaml") +@pytest.mark.usefixtures("mock_is_file", "mock_hass_config_yaml") def test_package_invalid() -> None: """Test an invalid package.""" res = check_config.check(get_test_config_dir()) @@ -168,7 +168,7 @@ def test_package_invalid() -> None: @pytest.mark.parametrize( "hass_config_yaml", [BASE_CONFIG + "automation: !include no.yaml"] ) -@pytest.mark.usefixtures("event_loop", "mock_hass_config_yaml") +@pytest.mark.usefixtures("mock_hass_config_yaml") def test_bootstrap_error() -> None: """Test a valid platform setup.""" res = check_config.check(get_test_config_dir(YAML_CONFIG_FILE))