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
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

View file

@ -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

View file

@ -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):