Skip to content

Commit

Permalink
Use aiohttp.ClientTimeout for timeout (#122458)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p committed Jul 23, 2024
1 parent 545514c commit 156a242
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 8 deletions.
5 changes: 4 additions & 1 deletion homeassistant/components/amcrest/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import logging
from typing import TYPE_CHECKING, Any

import aiohttp
from aiohttp import web
from amcrest import AmcrestError
from haffmpeg.camera import CameraMjpeg
Expand Down Expand Up @@ -244,7 +245,9 @@ async def handle_async_mjpeg_stream(
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)
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/auth/indieauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
4 changes: 3 additions & 1 deletion homeassistant/components/buienradar/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ async def __retrieve_radar_image(self) -> bool:
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:
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/cast/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/discord/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import os.path
from typing import Any, cast

import aiohttp
import nextcord
from nextcord.abc import Messageable

Expand Down Expand Up @@ -81,7 +82,7 @@ async def async_get_file_from_url(
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")
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/system_health/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 4 additions & 1 deletion homeassistant/components/trafikverket_camera/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import logging
from typing import TYPE_CHECKING

import aiohttp
from pytrafikverket.exceptions import (
InvalidAuthentication,
MultipleCamerasFound,
Expand Down Expand Up @@ -77,7 +78,9 @@ async def _async_update_data(self) -> 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()
Expand Down
3 changes: 2 additions & 1 deletion homeassistant/util/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 156a242

Please sign in to comment.