mirror of
https://github.com/home-assistant/core.git
synced 2026-05-08 17:49:37 +01:00
Improve Transmission error handling (#163388)
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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."""
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user