diff --git a/homeassistant/components/vesync/__init__.py b/homeassistant/components/vesync/__init__.py index 133713b33524d1..f7a986ee573e8d 100644 --- a/homeassistant/components/vesync/__init__.py +++ b/homeassistant/components/vesync/__init__.py @@ -3,12 +3,16 @@ import logging from pyvesync import VeSync -from pyvesync.utils.errors import VeSyncLoginError +from pyvesync.utils.errors import ( + VeSyncAPIResponseError, + VeSyncLoginError, + VeSyncServerError, +) from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform from homeassistant.core import HomeAssistant -from homeassistant.exceptions import ConfigEntryAuthFailed +from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady from homeassistant.helpers import config_validation as cv, entity_registry as er from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.device_registry import DeviceEntry @@ -59,7 +63,17 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b try: await manager.login() except VeSyncLoginError as err: - raise ConfigEntryAuthFailed from err + raise ConfigEntryAuthFailed( + translation_domain=DOMAIN, translation_key="invalid_auth" + ) from err + except VeSyncServerError as err: + raise ConfigEntryNotReady( + translation_domain=DOMAIN, translation_key="server_error" + ) from err + except VeSyncAPIResponseError as err: + raise ConfigEntryNotReady( + translation_domain=DOMAIN, translation_key="api_response_error" + ) from err hass.data[DOMAIN] = {} hass.data[DOMAIN][VS_MANAGER] = manager diff --git a/homeassistant/components/vesync/strings.json b/homeassistant/components/vesync/strings.json index 6435a9676bbc8a..4f72c6c790a679 100644 --- a/homeassistant/components/vesync/strings.json +++ b/homeassistant/components/vesync/strings.json @@ -5,7 +5,9 @@ "single_instance_allowed": "[%key:common::config_flow::abort::single_instance_allowed%]" }, "error": { - "invalid_auth": "[%key:common::config_flow::error::invalid_auth%]" + "api_response_error": "API response error", + "invalid_auth": "[%key:common::config_flow::error::invalid_auth%]", + "server_error": "Server error occurred" }, "step": { "reauth_confirm": { diff --git a/tests/components/vesync/test_init.py b/tests/components/vesync/test_init.py index 97d27fe221cd12..1e10e2f9faaf1f 100644 --- a/tests/components/vesync/test_init.py +++ b/tests/components/vesync/test_init.py @@ -2,8 +2,13 @@ from unittest.mock import AsyncMock, patch +import pytest from pyvesync import VeSync -from pyvesync.utils.errors import VeSyncLoginError +from pyvesync.utils.errors import ( + VeSyncAPIResponseError, + VeSyncLoginError, + VeSyncServerError, +) from homeassistant.components.vesync import ( async_remove_config_entry_device, @@ -18,21 +23,30 @@ from tests.common import MockConfigEntry -async def test_async_setup_entry__not_login( +@pytest.mark.parametrize( + ("exception", "expected_state"), + [ + (VeSyncLoginError("Mock login failed"), ConfigEntryState.SETUP_ERROR), + (VeSyncAPIResponseError("Mock login failed"), ConfigEntryState.SETUP_RETRY), + (VeSyncServerError("Mock login failed"), ConfigEntryState.SETUP_RETRY), + ], +) +async def test_async_setup_entry_login_errors( hass: HomeAssistant, config_entry: ConfigEntry, manager: VeSync, + exception: Exception, + expected_state: ConfigEntryState, ) -> None: - """Test setup does not create config entry when not logged in.""" - manager.login = AsyncMock(side_effect=VeSyncLoginError("Mock login failed")) + """Test setup handles different login errors appropriately.""" + manager.login = AsyncMock(side_effect=exception) assert not await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() assert manager.login.call_count == 1 assert len(hass.config_entries.async_entries(DOMAIN)) == 1 - assert config_entry.state is ConfigEntryState.SETUP_ERROR - assert not hass.data.get(DOMAIN) + assert config_entry.state is expected_state async def test_async_setup_entry__no_devices(