Skip to content

Commit

Permalink
Fix transmission error handling (#91548)
Browse files Browse the repository at this point in the history
* transmission error handle fix

* added unexpected case tests
  • Loading branch information
DDanii authored May 5, 2023
1 parent b6664ce commit 9ce0624
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 16 deletions.
19 changes: 11 additions & 8 deletions homeassistant/components/transmission/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
from typing import Any

import transmission_rpc
from transmission_rpc.error import TransmissionError
from transmission_rpc.error import (
TransmissionAuthError,
TransmissionConnectError,
TransmissionError,
)
import voluptuous as vol

from homeassistant.config_entries import ConfigEntry, ConfigEntryState
Expand Down Expand Up @@ -137,14 +141,13 @@ async def get_api(hass, entry):
_LOGGER.debug("Successfully connected to %s", host)
return api

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:
if "401: Unauthorized" in str(error):
_LOGGER.error("Credentials for Transmission client are not valid")
raise AuthenticationError from error
if "111: Connection refused" in str(error):
_LOGGER.error("Connecting to the Transmission client %s failed", host)
raise CannotConnect from error

_LOGGER.error(error)
raise UnknownError from error

Expand Down
29 changes: 24 additions & 5 deletions tests/components/transmission/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
from unittest.mock import MagicMock, patch

import pytest
from transmission_rpc.error import TransmissionError
from transmission_rpc.error import (
TransmissionAuthError,
TransmissionConnectError,
TransmissionError,
)

from homeassistant import config_entries
from homeassistant.components import transmission
Expand Down Expand Up @@ -125,7 +129,7 @@ async def test_error_on_wrong_credentials(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)

mock_api.side_effect = TransmissionError("401: Unauthorized")
mock_api.side_effect = TransmissionAuthError()
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
MOCK_CONFIG_DATA,
Expand All @@ -137,6 +141,21 @@ async def test_error_on_wrong_credentials(
}


async def test_unexpected_error(hass: HomeAssistant, mock_api: MagicMock) -> None:
"""Test we handle unexpected error."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)

mock_api.side_effect = TransmissionError()
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
MOCK_CONFIG_DATA,
)
assert result2["type"] == FlowResultType.FORM
assert result2["errors"] == {"base": "cannot_connect"}


async def test_error_on_connection_failure(
hass: HomeAssistant, mock_api: MagicMock
) -> None:
Expand All @@ -145,7 +164,7 @@ async def test_error_on_connection_failure(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)

mock_api.side_effect = TransmissionError("111: Connection refused")
mock_api.side_effect = TransmissionConnectError()
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
MOCK_CONFIG_DATA,
Expand Down Expand Up @@ -213,7 +232,7 @@ async def test_reauth_failed(hass: HomeAssistant, mock_api: MagicMock) -> None:
assert result["step_id"] == "reauth_confirm"
assert result["description_placeholders"] == {"username": "user"}

mock_api.side_effect = TransmissionError("401: Unauthorized")
mock_api.side_effect = TransmissionAuthError()
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
Expand Down Expand Up @@ -248,7 +267,7 @@ async def test_reauth_failed_connection_error(
assert result["step_id"] == "reauth_confirm"
assert result["description_placeholders"] == {"username": "user"}

mock_api.side_effect = TransmissionError("111: Connection refused")
mock_api.side_effect = TransmissionConnectError()
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
Expand Down
24 changes: 21 additions & 3 deletions tests/components/transmission/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
from unittest.mock import MagicMock, patch

import pytest
from transmission_rpc.error import TransmissionError
from transmission_rpc.error import (
TransmissionAuthError,
TransmissionConnectError,
TransmissionError,
)

from homeassistant.components.transmission.const import DOMAIN
from homeassistant.config_entries import ConfigEntryState
Expand Down Expand Up @@ -40,7 +44,7 @@ async def test_setup_failed_connection_error(
entry = MockConfigEntry(domain=DOMAIN, data=MOCK_CONFIG_DATA)
entry.add_to_hass(hass)

mock_api.side_effect = TransmissionError("111: Connection refused")
mock_api.side_effect = TransmissionConnectError()

await hass.config_entries.async_setup(entry.entry_id)
assert entry.state == ConfigEntryState.SETUP_RETRY
Expand All @@ -54,7 +58,21 @@ async def test_setup_failed_auth_error(
entry = MockConfigEntry(domain=DOMAIN, data=MOCK_CONFIG_DATA)
entry.add_to_hass(hass)

mock_api.side_effect = TransmissionError("401: Unauthorized")
mock_api.side_effect = TransmissionAuthError()

await hass.config_entries.async_setup(entry.entry_id)
assert entry.state == ConfigEntryState.SETUP_ERROR


async def test_setup_failed_unexpected_error(
hass: HomeAssistant, mock_api: MagicMock
) -> None:
"""Test integration failed due to unexpected error."""

entry = MockConfigEntry(domain=DOMAIN, data=MOCK_CONFIG_DATA)
entry.add_to_hass(hass)

mock_api.side_effect = TransmissionError()

await hass.config_entries.async_setup(entry.entry_id)
assert entry.state == ConfigEntryState.SETUP_ERROR
Expand Down

0 comments on commit 9ce0624

Please sign in to comment.