diff --git a/homeassistant/components/tradfri/__init__.py b/homeassistant/components/tradfri/__init__.py index 206f4e07007..2acd79a3e49 100644 --- a/homeassistant/components/tradfri/__init__.py +++ b/homeassistant/components/tradfri/__init__.py @@ -5,7 +5,7 @@ from datetime import datetime, timedelta import logging from typing import Any -from pytradfri import Gateway, RequestError +from pytradfri import Gateway, PytradfriError, RequestError from pytradfri.api.aiocoap_api import APIFactory import voluptuous as vol @@ -36,6 +36,7 @@ from .const import ( KEY_API, PLATFORMS, SIGNAL_GW, + TIMEOUT_API, ) _LOGGER = logging.getLogger(__name__) @@ -107,14 +108,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: gateway = Gateway() try: - gateway_info = await api(gateway.get_gateway_info()) - devices_commands = await api(gateway.get_devices()) - devices = await api(devices_commands) - groups_commands = await api(gateway.get_groups()) - groups = await api(groups_commands) - except RequestError as err: + gateway_info = await api(gateway.get_gateway_info(), timeout=TIMEOUT_API) + devices_commands = await api(gateway.get_devices(), timeout=TIMEOUT_API) + devices = await api(devices_commands, timeout=TIMEOUT_API) + groups_commands = await api(gateway.get_groups(), timeout=TIMEOUT_API) + groups = await api(groups_commands, timeout=TIMEOUT_API) + except PytradfriError as exc: await factory.shutdown() - raise ConfigEntryNotReady from err + raise ConfigEntryNotReady from exc tradfri_data[KEY_API] = api tradfri_data[FACTORY] = factory diff --git a/homeassistant/components/tradfri/const.py b/homeassistant/components/tradfri/const.py index 5bcd1f376e1..abd3e8aff4a 100644 --- a/homeassistant/components/tradfri/const.py +++ b/homeassistant/components/tradfri/const.py @@ -26,3 +26,4 @@ KEY_SECURITY_CODE = "security_code" SUPPORTED_GROUP_FEATURES = SUPPORT_BRIGHTNESS | SUPPORT_TRANSITION SUPPORTED_LIGHT_FEATURES = SUPPORT_TRANSITION PLATFORMS = ["cover", "fan", "light", "sensor", "switch"] +TIMEOUT_API = 30 diff --git a/tests/components/tradfri/conftest.py b/tests/components/tradfri/conftest.py index c5310d52b9c..f4e871d79e1 100644 --- a/tests/components/tradfri/conftest.py +++ b/tests/components/tradfri/conftest.py @@ -59,7 +59,7 @@ def mock_gateway_fixture(): def mock_api_fixture(mock_gateway): """Mock api.""" - async def api(command): + async def api(command, timeout=None): """Mock api function.""" # Store the data for "real" command objects. if hasattr(command, "_data") and not isinstance(command, Mock):