From 0a734b742620e509de376ea94c704fcbfe1109c7 Mon Sep 17 00:00:00 2001 From: Andrew Jackson Date: Wed, 18 Feb 2026 18:41:28 +0000 Subject: [PATCH] Improve Transmission error handling (#163388) --- .../components/transmission/__init__.py | 40 +++++++------------ .../components/transmission/config_flow.py | 14 ++++--- .../components/transmission/errors.py | 15 ------- tests/components/transmission/test_init.py | 2 +- 4 files changed, 25 insertions(+), 46 deletions(-) delete mode 100644 homeassistant/components/transmission/errors.py diff --git a/homeassistant/components/transmission/__init__.py b/homeassistant/components/transmission/__init__.py index d5a566879a6..56d6a2d5d67 100644 --- a/homeassistant/components/transmission/__init__.py +++ b/homeassistant/components/transmission/__init__.py @@ -36,7 +36,6 @@ from homeassistant.helpers.typing import ConfigType from .const import DEFAULT_PATH, DEFAULT_SSL, DOMAIN from .coordinator import TransmissionConfigEntry, TransmissionDataUpdateCoordinator -from .errors import AuthenticationError, CannotConnect, UnknownError from .services import async_setup_services _LOGGER = logging.getLogger(__name__) @@ -93,10 +92,10 @@ async def async_setup_entry( try: api = await get_api(hass, dict(config_entry.data)) - except CannotConnect as error: - raise ConfigEntryNotReady from error - except (AuthenticationError, UnknownError) as error: - raise ConfigEntryAuthFailed from error + except TransmissionAuthError as err: + raise ConfigEntryAuthFailed from err + except (TransmissionConnectError, TransmissionError) as err: + raise ConfigEntryNotReady from err protocol: Final = "https" if config_entry.data[CONF_SSL] else "http" device_registry = dr.async_get(hass) @@ -171,26 +170,17 @@ async def get_api( username = entry.get(CONF_USERNAME) password = entry.get(CONF_PASSWORD) - try: - api = await hass.async_add_executor_job( - partial( - transmission_rpc.Client, - username=username, - password=password, - protocol=protocol, - host=host, - port=port, - path=path, - ) + api = await hass.async_add_executor_job( + partial( + transmission_rpc.Client, + username=username, + password=password, + protocol=protocol, + host=host, + port=port, + path=path, ) - except TransmissionAuthError as error: - _LOGGER.error("Credentials for Transmission client are not valid") - raise AuthenticationError from error - except TransmissionConnectError as error: - _LOGGER.error("Connecting to the Transmission client %s failed", host) - raise CannotConnect from error - except TransmissionError as error: - _LOGGER.error(error) - raise UnknownError from error + ) + _LOGGER.debug("Successfully connected to %s", host) return api diff --git a/homeassistant/components/transmission/config_flow.py b/homeassistant/components/transmission/config_flow.py index 467a2ce5548..9294319aeb8 100644 --- a/homeassistant/components/transmission/config_flow.py +++ b/homeassistant/components/transmission/config_flow.py @@ -5,6 +5,11 @@ from __future__ import annotations from collections.abc import Mapping from typing import Any +from transmission_rpc.error import ( + TransmissionAuthError, + TransmissionConnectError, + TransmissionError, +) import voluptuous as vol from homeassistant.config_entries import ( @@ -37,7 +42,6 @@ from .const import ( DOMAIN, SUPPORTED_ORDER_MODES, ) -from .errors import AuthenticationError, CannotConnect, UnknownError DATA_SCHEMA = vol.Schema( { @@ -78,10 +82,10 @@ class TransmissionFlowHandler(ConfigFlow, domain=DOMAIN): try: await get_api(self.hass, user_input) - except AuthenticationError: + except TransmissionAuthError: errors[CONF_USERNAME] = "invalid_auth" errors[CONF_PASSWORD] = "invalid_auth" - except CannotConnect, UnknownError: + except TransmissionConnectError, TransmissionError: errors["base"] = "cannot_connect" if not errors: @@ -113,9 +117,9 @@ class TransmissionFlowHandler(ConfigFlow, domain=DOMAIN): try: await get_api(self.hass, user_input) - except AuthenticationError: + except TransmissionAuthError: errors[CONF_PASSWORD] = "invalid_auth" - except CannotConnect, UnknownError: + except TransmissionConnectError, TransmissionError: errors["base"] = "cannot_connect" else: return self.async_update_reload_and_abort(reauth_entry, data=user_input) diff --git a/homeassistant/components/transmission/errors.py b/homeassistant/components/transmission/errors.py deleted file mode 100644 index 68d442c3a74..00000000000 --- a/homeassistant/components/transmission/errors.py +++ /dev/null @@ -1,15 +0,0 @@ -"""Errors for the Transmission component.""" - -from homeassistant.exceptions import HomeAssistantError - - -class AuthenticationError(HomeAssistantError): - """Wrong Username or Password.""" - - -class CannotConnect(HomeAssistantError): - """Unable to connect to client.""" - - -class UnknownError(HomeAssistantError): - """Unknown Error.""" diff --git a/tests/components/transmission/test_init.py b/tests/components/transmission/test_init.py index 07698681d1e..17ebadc5875 100644 --- a/tests/components/transmission/test_init.py +++ b/tests/components/transmission/test_init.py @@ -91,7 +91,7 @@ async def test_setup_failed_unexpected_error( await hass.config_entries.async_setup(mock_config_entry.entry_id) - assert mock_config_entry.state is ConfigEntryState.SETUP_ERROR + assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY async def test_unload_entry(