From e0828f1efc47f0567da7857c0aa1c984046277cc Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 13 Mar 2024 15:12:13 -1000 Subject: [PATCH] Ensure apple_tv setup retries later on timeout (#113367) --- homeassistant/components/apple_tv/__init__.py | 39 +++++++++++-------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/homeassistant/components/apple_tv/__init__.py b/homeassistant/components/apple_tv/__init__.py index 0b9ef14f253..cd1a1c59127 100644 --- a/homeassistant/components/apple_tv/__init__.py +++ b/homeassistant/components/apple_tv/__init__.py @@ -54,6 +54,25 @@ SIGNAL_DISCONNECTED = "apple_tv_disconnected" PLATFORMS = [Platform.MEDIA_PLAYER, Platform.REMOTE] +AUTH_EXCEPTIONS = ( + exceptions.AuthenticationError, + exceptions.InvalidCredentialsError, + exceptions.NoCredentialsError, +) +CONNECTION_TIMEOUT_EXCEPTIONS = ( + asyncio.CancelledError, + TimeoutError, + exceptions.ConnectionLostError, + exceptions.ConnectionFailedError, +) +DEVICE_EXCEPTIONS = ( + exceptions.ProtocolError, + exceptions.NoServiceError, + exceptions.PairingError, + exceptions.BackOffError, + exceptions.DeviceIdMissingError, +) + async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up a config entry for Apple TV.""" @@ -64,27 +83,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: try: await manager.async_first_connect() - except ( - exceptions.AuthenticationError, - exceptions.InvalidCredentialsError, - exceptions.NoCredentialsError, - ) as ex: + except AUTH_EXCEPTIONS as ex: raise ConfigEntryAuthFailed( f"{address}: Authentication failed, try reconfiguring device: {ex}" ) from ex - except ( - asyncio.CancelledError, - exceptions.ConnectionLostError, - exceptions.ConnectionFailedError, - ) as ex: + except CONNECTION_TIMEOUT_EXCEPTIONS as ex: raise ConfigEntryNotReady(f"{address}: {ex}") from ex - except ( - exceptions.ProtocolError, - exceptions.NoServiceError, - exceptions.PairingError, - exceptions.BackOffError, - exceptions.DeviceIdMissingError, - ) as ex: + except DEVICE_EXCEPTIONS as ex: _LOGGER.debug( "Error setting up apple_tv at %s: %s", address, ex, exc_info=ex )