diff --git a/aioshelly/block_device.py b/aioshelly/block_device.py index 4011e34c..c3219fb0 100644 --- a/aioshelly/block_device.py +++ b/aioshelly/block_device.py @@ -11,7 +11,7 @@ from .coap import COAP, CoapMessage from .common import ConnectionOptions, IpOrOptionsType, get_info, process_ip_or_options -from .const import BLOCK_DEVICE_INIT_TIMEOUT +from .const import BLOCK_DEVICE_INIT_TIMEOUT, HTTP_CALL_TIMEOUT from .exceptions import AuthRequired, NotInitialized, WrongShellyGen BLOCK_VALUE_UNIT = "U" @@ -224,6 +224,7 @@ async def http_request( params=params, auth=self.options.auth, raise_for_status=True, + timeout=HTTP_CALL_TIMEOUT, ) return cast(dict, await resp.json()) diff --git a/aioshelly/common.py b/aioshelly/common.py index 231eb24d..864415d9 100644 --- a/aioshelly/common.py +++ b/aioshelly/common.py @@ -9,8 +9,9 @@ from typing import Any, Union import aiohttp +import async_timeout -from .const import GEN1_MIN_FIRMWARE_DATE, GEN2_MIN_FIRMWARE_DATE +from .const import GEN1_MIN_FIRMWARE_DATE, GEN2_MIN_FIRMWARE_DATE, HTTP_CALL_TIMEOUT from .exceptions import FirmwareUnsupported FIRMWARE_PATTERN = re.compile(r"^(\d{8})") @@ -62,10 +63,11 @@ async def get_info( aiohttp_session: aiohttp.ClientSession, ip_address: str ) -> dict[str, Any]: """Get info from device through REST call.""" - async with aiohttp_session.get( - f"http://{ip_address}/shelly", raise_for_status=True - ) as resp: - result: dict[str, Any] = await resp.json() + async with async_timeout.timeout(HTTP_CALL_TIMEOUT): + async with aiohttp_session.get( + f"http://{ip_address}/shelly", raise_for_status=True + ) as resp: + result: dict[str, Any] = await resp.json() if not shelly_supported_firmware(result): raise FirmwareUnsupported diff --git a/aioshelly/const.py b/aioshelly/const.py index ed64455c..f57cbebb 100644 --- a/aioshelly/const.py +++ b/aioshelly/const.py @@ -51,6 +51,9 @@ # Timeout used for Block Device init BLOCK_DEVICE_INIT_TIMEOUT = 10 +# Timeout used for HTTP calls +HTTP_CALL_TIMEOUT = 10 + # Firmware 1.8.0 release date (CoAP v2) GEN1_MIN_FIRMWARE_DATE = 20200812