Use built-in aiohttp timeout instead of asyncio.timeout in media_player (#116364)

* Use built-in aiohttp timeout instead of asyncio.timeout in media_player

Avoids having two timeouts running to fetch images

* fix mock
This commit is contained in:
J. Nick Koston 2024-04-29 00:38:40 -05:00 committed by GitHub
parent 8c73c1e1a5
commit 381ffe6eed
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 7 deletions

View File

@ -17,6 +17,7 @@ import secrets
from typing import Any, Final, Required, TypedDict, final
from urllib.parse import quote, urlparse
import aiohttp
from aiohttp import web
from aiohttp.hdrs import CACHE_CONTROL, CONTENT_TYPE
from aiohttp.typedefs import LooseHeaders
@ -1336,6 +1337,9 @@ async def websocket_browse_media(
connection.send_result(msg["id"], result)
_FETCH_TIMEOUT = aiohttp.ClientTimeout(total=10)
async def async_fetch_image(
logger: logging.Logger, hass: HomeAssistant, url: str
) -> tuple[bytes | None, str | None]:
@ -1343,12 +1347,11 @@ async def async_fetch_image(
content, content_type = (None, None)
websession = async_get_clientsession(hass)
with suppress(TimeoutError):
async with asyncio.timeout(10):
response = await websession.get(url)
if response.status == HTTPStatus.OK:
content = await response.read()
if content_type := response.headers.get(CONTENT_TYPE):
content_type = content_type.split(";")[0]
response = await websession.get(url, timeout=_FETCH_TIMEOUT)
if response.status == HTTPStatus.OK:
content = await response.read()
if content_type := response.headers.get(CONTENT_TYPE):
content_type = content_type.split(";")[0]
if content is None:
url_parts = URL(url)

View File

@ -477,7 +477,7 @@ async def test_media_image_proxy(
class MockWebsession:
"""Test websession."""
async def get(self, url):
async def get(self, url, **kwargs):
"""Test websession get."""
return MockResponse()