Improve apple_tv error reporting when setup fails (#110071)

* Improve apple_tv error reporting when setup fails

* Improve apple_tv error reporting when setup fails

* Update homeassistant/components/apple_tv/__init__.py

* ensure cleaned up
This commit is contained in:
J. Nick Koston 2024-02-09 07:50:30 -06:00 committed by GitHub
parent ae5bef6ffa
commit ced922bb1a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -26,7 +26,7 @@ from homeassistant.const import (
Platform, Platform,
) )
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
from homeassistant.helpers import device_registry as dr from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.device_registry import DeviceInfo
@ -57,10 +57,35 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
manager = AppleTVManager(hass, entry) manager = AppleTVManager(hass, entry)
if manager.is_on: if manager.is_on:
await manager.connect_once(raise_missing_credentials=True) address = entry.data[CONF_ADDRESS]
if not manager.atv:
address = entry.data[CONF_ADDRESS] try:
raise ConfigEntryNotReady(f"Not found at {address}, waiting for discovery") await manager.async_first_connect()
except (
exceptions.AuthenticationError,
exceptions.InvalidCredentialsError,
exceptions.NoCredentialsError,
) as ex:
raise ConfigEntryAuthFailed(
f"{address}: Authentication failed, try reconfiguring device: {ex}"
) from ex
except (
asyncio.CancelledError,
exceptions.ConnectionLostError,
exceptions.ConnectionFailedError,
) as ex:
raise ConfigEntryNotReady(f"{address}: {ex}") from ex
except (
exceptions.ProtocolError,
exceptions.NoServiceError,
exceptions.PairingError,
exceptions.BackOffError,
exceptions.DeviceIdMissingError,
) as ex:
_LOGGER.debug(
"Error setting up apple_tv at %s: %s", address, ex, exc_info=ex
)
raise ConfigEntryNotReady(f"{address}: {ex}") from ex
hass.data.setdefault(DOMAIN, {})[entry.unique_id] = manager hass.data.setdefault(DOMAIN, {})[entry.unique_id] = manager
@ -228,11 +253,25 @@ class AppleTVManager(DeviceListener):
"Not starting connect loop (%s, %s)", self.atv is None, self.is_on "Not starting connect loop (%s, %s)", self.atv is None, self.is_on
) )
async def _connect_once(self, raise_missing_credentials: bool) -> None:
"""Connect to device once."""
if conf := await self._scan():
await self._connect(conf, raise_missing_credentials)
async def async_first_connect(self):
"""Connect to device for the first time."""
connect_ok = False
try:
await self._connect_once(raise_missing_credentials=True)
connect_ok = True
finally:
if not connect_ok:
await self.disconnect()
async def connect_once(self, raise_missing_credentials: bool) -> None: async def connect_once(self, raise_missing_credentials: bool) -> None:
"""Try to connect once.""" """Try to connect once."""
try: try:
if conf := await self._scan(): await self._connect_once(raise_missing_credentials)
await self._connect(conf, raise_missing_credentials)
except exceptions.AuthenticationError: except exceptions.AuthenticationError:
self.config_entry.async_start_reauth(self.hass) self.config_entry.async_start_reauth(self.hass)
await self.disconnect() await self.disconnect()
@ -245,7 +284,7 @@ class AppleTVManager(DeviceListener):
pass pass
except Exception: # pylint: disable=broad-except except Exception: # pylint: disable=broad-except
_LOGGER.exception("Failed to connect") _LOGGER.exception("Failed to connect")
self.atv = None await self.disconnect()
async def _connect_loop(self): async def _connect_loop(self):
"""Connect loop background task function.""" """Connect loop background task function."""