Use aiohttp.ClientTimeout for timeout (#122458)
This commit is contained in:
parent
545514c5cd
commit
156a2427ff
8 changed files with 18 additions and 8 deletions
|
@ -8,6 +8,7 @@ from datetime import timedelta
|
||||||
import logging
|
import logging
|
||||||
from typing import TYPE_CHECKING, Any
|
from typing import TYPE_CHECKING, Any
|
||||||
|
|
||||||
|
import aiohttp
|
||||||
from aiohttp import web
|
from aiohttp import web
|
||||||
from amcrest import AmcrestError
|
from amcrest import AmcrestError
|
||||||
from haffmpeg.camera import CameraMjpeg
|
from haffmpeg.camera import CameraMjpeg
|
||||||
|
@ -244,7 +245,9 @@ class AmcrestCam(Camera):
|
||||||
websession = async_get_clientsession(self.hass)
|
websession = async_get_clientsession(self.hass)
|
||||||
streaming_url = self._api.mjpeg_url(typeno=self._resolution)
|
streaming_url = self._api.mjpeg_url(typeno=self._resolution)
|
||||||
stream_coro = websession.get(
|
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)
|
return await async_aiohttp_proxy_web(self.hass, request, stream_coro)
|
||||||
|
|
|
@ -94,7 +94,7 @@ async def fetch_redirect_uris(hass: HomeAssistant, url: str) -> list[str]:
|
||||||
try:
|
try:
|
||||||
async with (
|
async with (
|
||||||
aiohttp.ClientSession() as session,
|
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):
|
async for data in resp.content.iter_chunked(1024):
|
||||||
parser.feed(data.decode())
|
parser.feed(data.decode())
|
||||||
|
|
|
@ -115,7 +115,9 @@ class BuienradarCam(Camera):
|
||||||
headers = {}
|
headers = {}
|
||||||
|
|
||||||
try:
|
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()
|
res.raise_for_status()
|
||||||
|
|
||||||
if res.status == 304:
|
if res.status == 304:
|
||||||
|
|
|
@ -248,7 +248,7 @@ async def _fetch_playlist(hass, url, supported_content_types):
|
||||||
"""Fetch a playlist from the given url."""
|
"""Fetch a playlist from the given url."""
|
||||||
try:
|
try:
|
||||||
session = aiohttp_client.async_get_clientsession(hass, verify_ssl=False)
|
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"
|
charset = resp.charset or "utf-8"
|
||||||
if resp.content_type in supported_content_types:
|
if resp.content_type in supported_content_types:
|
||||||
raise PlaylistSupported
|
raise PlaylistSupported
|
||||||
|
|
|
@ -7,6 +7,7 @@ import logging
|
||||||
import os.path
|
import os.path
|
||||||
from typing import Any, cast
|
from typing import Any, cast
|
||||||
|
|
||||||
|
import aiohttp
|
||||||
import nextcord
|
import nextcord
|
||||||
from nextcord.abc import Messageable
|
from nextcord.abc import Messageable
|
||||||
|
|
||||||
|
@ -81,7 +82,7 @@ class DiscordNotificationService(BaseNotificationService):
|
||||||
async with session.get(
|
async with session.get(
|
||||||
url,
|
url,
|
||||||
ssl=verify_ssl,
|
ssl=verify_ssl,
|
||||||
timeout=30,
|
timeout=aiohttp.ClientTimeout(total=30),
|
||||||
raise_for_status=True,
|
raise_for_status=True,
|
||||||
) as resp:
|
) as resp:
|
||||||
content_length = resp.headers.get("Content-Length")
|
content_length = resp.headers.get("Content-Length")
|
||||||
|
|
|
@ -235,7 +235,7 @@ async def async_check_can_reach_url(
|
||||||
session = aiohttp_client.async_get_clientsession(hass)
|
session = aiohttp_client.async_get_clientsession(hass)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
await session.get(url, timeout=5)
|
await session.get(url, timeout=aiohttp.ClientTimeout(total=5))
|
||||||
except aiohttp.ClientError:
|
except aiohttp.ClientError:
|
||||||
data = {"type": "failed", "error": "unreachable"}
|
data = {"type": "failed", "error": "unreachable"}
|
||||||
except TimeoutError:
|
except TimeoutError:
|
||||||
|
|
|
@ -8,6 +8,7 @@ from io import BytesIO
|
||||||
import logging
|
import logging
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
|
import aiohttp
|
||||||
from pytrafikverket.exceptions import (
|
from pytrafikverket.exceptions import (
|
||||||
InvalidAuthentication,
|
InvalidAuthentication,
|
||||||
MultipleCamerasFound,
|
MultipleCamerasFound,
|
||||||
|
@ -77,7 +78,9 @@ class TVDataUpdateCoordinator(DataUpdateCoordinator[CameraData]):
|
||||||
if camera_data.fullsizephoto:
|
if camera_data.fullsizephoto:
|
||||||
image_url = f"{camera_data.photourl}?type=fullsize"
|
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):
|
if get_image.status not in range(200, 299):
|
||||||
raise UpdateFailed("Could not retrieve image")
|
raise UpdateFailed("Could not retrieve image")
|
||||||
image = BytesIO(await get_image.read()).getvalue()
|
image = BytesIO(await get_image.read()).getvalue()
|
||||||
|
|
|
@ -163,7 +163,8 @@ async def _get_whoami(session: aiohttp.ClientSession) -> dict[str, Any] | None:
|
||||||
"""Query whoami.home-assistant.io for location data."""
|
"""Query whoami.home-assistant.io for location data."""
|
||||||
try:
|
try:
|
||||||
resp = await session.get(
|
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):
|
except (aiohttp.ClientError, TimeoutError):
|
||||||
return None
|
return None
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue