Use aiohttp.ClientTimeout for timeout (#122458)

This commit is contained in:
Marc Mueller 2024-07-23 15:20:04 +02:00 committed by GitHub
parent 545514c5cd
commit 156a2427ff
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 18 additions and 8 deletions

View File

@ -8,6 +8,7 @@ from datetime import timedelta
import logging
from typing import TYPE_CHECKING, Any
import aiohttp
from aiohttp import web
from amcrest import AmcrestError
from haffmpeg.camera import CameraMjpeg
@ -244,7 +245,9 @@ class AmcrestCam(Camera):
websession = async_get_clientsession(self.hass)
streaming_url = self._api.mjpeg_url(typeno=self._resolution)
stream_coro = websession.get(
streaming_url, auth=self._token, timeout=CAMERA_WEB_SESSION_TIMEOUT
streaming_url,
auth=self._token,
timeout=aiohttp.ClientTimeout(total=CAMERA_WEB_SESSION_TIMEOUT),
)
return await async_aiohttp_proxy_web(self.hass, request, stream_coro)

View File

@ -94,7 +94,7 @@ async def fetch_redirect_uris(hass: HomeAssistant, url: str) -> list[str]:
try:
async with (
aiohttp.ClientSession() as session,
session.get(url, timeout=5) as resp,
session.get(url, timeout=aiohttp.ClientTimeout(total=5)) as resp,
):
async for data in resp.content.iter_chunked(1024):
parser.feed(data.decode())

View File

@ -115,7 +115,9 @@ class BuienradarCam(Camera):
headers = {}
try:
async with session.get(url, timeout=5, headers=headers) as res:
async with session.get(
url, timeout=aiohttp.ClientTimeout(total=5), headers=headers
) as res:
res.raise_for_status()
if res.status == 304:

View File

@ -248,7 +248,7 @@ async def _fetch_playlist(hass, url, supported_content_types):
"""Fetch a playlist from the given url."""
try:
session = aiohttp_client.async_get_clientsession(hass, verify_ssl=False)
async with session.get(url, timeout=5) as resp:
async with session.get(url, timeout=aiohttp.ClientTimeout(total=5)) as resp:
charset = resp.charset or "utf-8"
if resp.content_type in supported_content_types:
raise PlaylistSupported

View File

@ -7,6 +7,7 @@ import logging
import os.path
from typing import Any, cast
import aiohttp
import nextcord
from nextcord.abc import Messageable
@ -81,7 +82,7 @@ class DiscordNotificationService(BaseNotificationService):
async with session.get(
url,
ssl=verify_ssl,
timeout=30,
timeout=aiohttp.ClientTimeout(total=30),
raise_for_status=True,
) as resp:
content_length = resp.headers.get("Content-Length")

View File

@ -235,7 +235,7 @@ async def async_check_can_reach_url(
session = aiohttp_client.async_get_clientsession(hass)
try:
await session.get(url, timeout=5)
await session.get(url, timeout=aiohttp.ClientTimeout(total=5))
except aiohttp.ClientError:
data = {"type": "failed", "error": "unreachable"}
except TimeoutError:

View File

@ -8,6 +8,7 @@ from io import BytesIO
import logging
from typing import TYPE_CHECKING
import aiohttp
from pytrafikverket.exceptions import (
InvalidAuthentication,
MultipleCamerasFound,
@ -77,7 +78,9 @@ class TVDataUpdateCoordinator(DataUpdateCoordinator[CameraData]):
if camera_data.fullsizephoto:
image_url = f"{camera_data.photourl}?type=fullsize"
async with self.session.get(image_url, timeout=10) as get_image:
async with self.session.get(
image_url, timeout=aiohttp.ClientTimeout(total=10)
) as get_image:
if get_image.status not in range(200, 299):
raise UpdateFailed("Could not retrieve image")
image = BytesIO(await get_image.read()).getvalue()

View File

@ -163,7 +163,8 @@ async def _get_whoami(session: aiohttp.ClientSession) -> dict[str, Any] | None:
"""Query whoami.home-assistant.io for location data."""
try:
resp = await session.get(
WHOAMI_URL_DEV if HA_VERSION.endswith("0.dev0") else WHOAMI_URL, timeout=30
WHOAMI_URL_DEV if HA_VERSION.endswith("0.dev0") else WHOAMI_URL,
timeout=aiohttp.ClientTimeout(total=30),
)
except (aiohttp.ClientError, TimeoutError):
return None