mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 17:57:11 +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 typing import Any, Final, Required, TypedDict, final
|
||||||
from urllib.parse import quote, urlparse
|
from urllib.parse import quote, urlparse
|
||||||
|
|
||||||
|
import aiohttp
|
||||||
from aiohttp import web
|
from aiohttp import web
|
||||||
from aiohttp.hdrs import CACHE_CONTROL, CONTENT_TYPE
|
from aiohttp.hdrs import CACHE_CONTROL, CONTENT_TYPE
|
||||||
from aiohttp.typedefs import LooseHeaders
|
from aiohttp.typedefs import LooseHeaders
|
||||||
@ -1336,6 +1337,9 @@ async def websocket_browse_media(
|
|||||||
connection.send_result(msg["id"], result)
|
connection.send_result(msg["id"], result)
|
||||||
|
|
||||||
|
|
||||||
|
_FETCH_TIMEOUT = aiohttp.ClientTimeout(total=10)
|
||||||
|
|
||||||
|
|
||||||
async def async_fetch_image(
|
async def async_fetch_image(
|
||||||
logger: logging.Logger, hass: HomeAssistant, url: str
|
logger: logging.Logger, hass: HomeAssistant, url: str
|
||||||
) -> tuple[bytes | None, str | None]:
|
) -> tuple[bytes | None, str | None]:
|
||||||
@ -1343,12 +1347,11 @@ async def async_fetch_image(
|
|||||||
content, content_type = (None, None)
|
content, content_type = (None, None)
|
||||||
websession = async_get_clientsession(hass)
|
websession = async_get_clientsession(hass)
|
||||||
with suppress(TimeoutError):
|
with suppress(TimeoutError):
|
||||||
async with asyncio.timeout(10):
|
response = await websession.get(url, timeout=_FETCH_TIMEOUT)
|
||||||
response = await websession.get(url)
|
if response.status == HTTPStatus.OK:
|
||||||
if response.status == HTTPStatus.OK:
|
content = await response.read()
|
||||||
content = await response.read()
|
if content_type := response.headers.get(CONTENT_TYPE):
|
||||||
if content_type := response.headers.get(CONTENT_TYPE):
|
content_type = content_type.split(";")[0]
|
||||||
content_type = content_type.split(";")[0]
|
|
||||||
|
|
||||||
if content is None:
|
if content is None:
|
||||||
url_parts = URL(url)
|
url_parts = URL(url)
|
||||||
|
@ -477,7 +477,7 @@ async def test_media_image_proxy(
|
|||||||
class MockWebsession:
|
class MockWebsession:
|
||||||
"""Test websession."""
|
"""Test websession."""
|
||||||
|
|
||||||
async def get(self, url):
|
async def get(self, url, **kwargs):
|
||||||
"""Test websession get."""
|
"""Test websession get."""
|
||||||
return MockResponse()
|
return MockResponse()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user