Catch connection errors that makes tradfri hang in startup (#59368)

This commit is contained in:
jan iversen 2021-11-09 13:00:50 +01:00 committed by GitHub
parent fc58df6df9
commit 06d29040b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 9 deletions

View File

@ -5,7 +5,7 @@ from datetime import datetime, timedelta
import logging import logging
from typing import Any from typing import Any
from pytradfri import Gateway, RequestError from pytradfri import Gateway, PytradfriError, RequestError
from pytradfri.api.aiocoap_api import APIFactory from pytradfri.api.aiocoap_api import APIFactory
import voluptuous as vol import voluptuous as vol
@ -36,6 +36,7 @@ from .const import (
KEY_API, KEY_API,
PLATFORMS, PLATFORMS,
SIGNAL_GW, SIGNAL_GW,
TIMEOUT_API,
) )
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -107,14 +108,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
gateway = Gateway() gateway = Gateway()
try: try:
gateway_info = await api(gateway.get_gateway_info()) gateway_info = await api(gateway.get_gateway_info(), timeout=TIMEOUT_API)
devices_commands = await api(gateway.get_devices()) devices_commands = await api(gateway.get_devices(), timeout=TIMEOUT_API)
devices = await api(devices_commands) devices = await api(devices_commands, timeout=TIMEOUT_API)
groups_commands = await api(gateway.get_groups()) groups_commands = await api(gateway.get_groups(), timeout=TIMEOUT_API)
groups = await api(groups_commands) groups = await api(groups_commands, timeout=TIMEOUT_API)
except RequestError as err: except PytradfriError as exc:
await factory.shutdown() await factory.shutdown()
raise ConfigEntryNotReady from err raise ConfigEntryNotReady from exc
tradfri_data[KEY_API] = api tradfri_data[KEY_API] = api
tradfri_data[FACTORY] = factory tradfri_data[FACTORY] = factory

View File

@ -26,3 +26,4 @@ KEY_SECURITY_CODE = "security_code"
SUPPORTED_GROUP_FEATURES = SUPPORT_BRIGHTNESS | SUPPORT_TRANSITION SUPPORTED_GROUP_FEATURES = SUPPORT_BRIGHTNESS | SUPPORT_TRANSITION
SUPPORTED_LIGHT_FEATURES = SUPPORT_TRANSITION SUPPORTED_LIGHT_FEATURES = SUPPORT_TRANSITION
PLATFORMS = ["cover", "fan", "light", "sensor", "switch"] PLATFORMS = ["cover", "fan", "light", "sensor", "switch"]
TIMEOUT_API = 30

View File

@ -59,7 +59,7 @@ def mock_gateway_fixture():
def mock_api_fixture(mock_gateway): def mock_api_fixture(mock_gateway):
"""Mock api.""" """Mock api."""
async def api(command): async def api(command, timeout=None):
"""Mock api function.""" """Mock api function."""
# Store the data for "real" command objects. # Store the data for "real" command objects.
if hasattr(command, "_data") and not isinstance(command, Mock): if hasattr(command, "_data") and not isinstance(command, Mock):