mirror of
https://github.com/home-assistant/core.git
synced 2025-04-24 17:27:52 +00:00
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:
parent
8c73c1e1a5
commit
381ffe6eed
@ -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)
|
||||
|
@ -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()
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user