mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Add type hints to http client fixtures (#86795)
This commit is contained in:
parent
cc36848a6d
commit
7e7a27f8b9
@ -16,8 +16,7 @@ import threading
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
from unittest.mock import AsyncMock, MagicMock, Mock, patch
|
from unittest.mock import AsyncMock, MagicMock, Mock, patch
|
||||||
|
|
||||||
from aiohttp import client
|
from aiohttp import ClientWebSocketResponse, client
|
||||||
from aiohttp.pytest_plugin import AiohttpClient
|
|
||||||
from aiohttp.test_utils import (
|
from aiohttp.test_utils import (
|
||||||
BaseTestServer,
|
BaseTestServer,
|
||||||
TestClient,
|
TestClient,
|
||||||
@ -55,6 +54,7 @@ from homeassistant.setup import async_setup_component
|
|||||||
from homeassistant.util import dt as dt_util, location
|
from homeassistant.util import dt as dt_util, location
|
||||||
|
|
||||||
from .ignore_uncaught_exceptions import IGNORE_UNCAUGHT_EXCEPTIONS
|
from .ignore_uncaught_exceptions import IGNORE_UNCAUGHT_EXCEPTIONS
|
||||||
|
from .typing import TestClientGenerator, TestWebSocketGenerator
|
||||||
|
|
||||||
pytest.register_assert_rewrite("tests.common")
|
pytest.register_assert_rewrite("tests.common")
|
||||||
|
|
||||||
@ -332,7 +332,7 @@ def aiohttp_client_cls():
|
|||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def aiohttp_client(
|
def aiohttp_client(
|
||||||
event_loop: asyncio.AbstractEventLoop,
|
event_loop: asyncio.AbstractEventLoop,
|
||||||
) -> Generator[AiohttpClient, None, None]:
|
) -> Generator[TestClientGenerator, None, None]:
|
||||||
"""Override the default aiohttp_client since 3.x does not support aiohttp_client_cls.
|
"""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
|
Remove this when upgrading to 4.x as aiohttp_client_cls
|
||||||
@ -607,10 +607,15 @@ def local_auth(hass):
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def hass_client(hass, aiohttp_client, hass_access_token, socket_enabled):
|
def hass_client(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
aiohttp_client: TestClientGenerator,
|
||||||
|
hass_access_token: str,
|
||||||
|
socket_enabled: None,
|
||||||
|
) -> TestClientGenerator:
|
||||||
"""Return an authenticated HTTP client."""
|
"""Return an authenticated HTTP client."""
|
||||||
|
|
||||||
async def auth_client():
|
async def auth_client() -> TestClient:
|
||||||
"""Return an authenticated client."""
|
"""Return an authenticated client."""
|
||||||
return await aiohttp_client(
|
return await aiohttp_client(
|
||||||
hass.http.app, headers={"Authorization": f"Bearer {hass_access_token}"}
|
hass.http.app, headers={"Authorization": f"Bearer {hass_access_token}"}
|
||||||
@ -620,10 +625,14 @@ def hass_client(hass, aiohttp_client, hass_access_token, socket_enabled):
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def hass_client_no_auth(hass, aiohttp_client, socket_enabled):
|
def hass_client_no_auth(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
aiohttp_client: TestClientGenerator,
|
||||||
|
socket_enabled: None,
|
||||||
|
) -> TestClientGenerator:
|
||||||
"""Return an unauthenticated HTTP client."""
|
"""Return an unauthenticated HTTP client."""
|
||||||
|
|
||||||
async def client():
|
async def client() -> TestClient:
|
||||||
"""Return an authenticated client."""
|
"""Return an authenticated client."""
|
||||||
return await aiohttp_client(hass.http.app)
|
return await aiohttp_client(hass.http.app)
|
||||||
|
|
||||||
@ -655,10 +664,17 @@ def current_request_with_host(current_request):
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def hass_ws_client(aiohttp_client, hass_access_token, hass, socket_enabled):
|
def hass_ws_client(
|
||||||
|
aiohttp_client: TestClientGenerator,
|
||||||
|
hass_access_token: str | None,
|
||||||
|
hass: HomeAssistant,
|
||||||
|
socket_enabled: None,
|
||||||
|
) -> TestWebSocketGenerator:
|
||||||
"""Websocket client fixture connected to websocket server."""
|
"""Websocket client fixture connected to websocket server."""
|
||||||
|
|
||||||
async def create_client(hass=hass, access_token=hass_access_token):
|
async def create_client(
|
||||||
|
hass: HomeAssistant = hass, access_token: str | None = hass_access_token
|
||||||
|
) -> ClientWebSocketResponse:
|
||||||
"""Create a websocket client."""
|
"""Create a websocket client."""
|
||||||
assert await async_setup_component(hass, "websocket_api", {})
|
assert await async_setup_component(hass, "websocket_api", {})
|
||||||
client = await aiohttp_client(hass.http.app)
|
client = await aiohttp_client(hass.http.app)
|
||||||
|
11
tests/typing.py
Normal file
11
tests/typing.py
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
"""Typing helpers for Home Assistant tests."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from collections.abc import Callable, Coroutine
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from aiohttp import ClientWebSocketResponse
|
||||||
|
from aiohttp.test_utils import TestClient
|
||||||
|
|
||||||
|
TestClientGenerator = Callable[..., Coroutine[Any, Any, TestClient]]
|
||||||
|
TestWebSocketGenerator = Callable[..., Coroutine[Any, Any, ClientWebSocketResponse]]
|
Loading…
x
Reference in New Issue
Block a user