diff --git a/homeassistant/components/powerwall/__init__.py b/homeassistant/components/powerwall/__init__.py index cfd666d13ffd..40d95800aa56 100644 --- a/homeassistant/components/powerwall/__init__.py +++ b/homeassistant/components/powerwall/__init__.py @@ -18,7 +18,11 @@ from homeassistant.components import persistent_notification from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_IP_ADDRESS, CONF_PASSWORD, Platform from homeassistant.core import HomeAssistant, callback -from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady +from homeassistant.exceptions import ( + ConfigEntryAuthFailed, + ConfigEntryError, + ConfigEntryNotReady, +) from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.helpers.aiohttp_client import async_create_clientsession from homeassistant.helpers.update_coordinator import UpdateFailed @@ -189,13 +193,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: PowerwallConfigEntry) -> except (TimeoutError, PowerwallUnreachableError) as err: raise ConfigEntryNotReady from err except MissingAttributeError as err: - # The error might include some important - # information about what exactly changed. - _LOGGER.error("The powerwall api has changed: %s", str(err)) persistent_notification.async_create( hass, API_CHANGED_ERROR_BODY, API_CHANGED_TITLE ) - return False + raise ConfigEntryError( + translation_domain=DOMAIN, + translation_key="api_changed", + ) from err except AccessDeniedError as err: if use_auth_cookie and tries == 0: _LOGGER.debug( diff --git a/homeassistant/components/powerwall/strings.json b/homeassistant/components/powerwall/strings.json index 2d52aa69d465..df7a10057e94 100644 --- a/homeassistant/components/powerwall/strings.json +++ b/homeassistant/components/powerwall/strings.json @@ -197,5 +197,10 @@ "name": "Off-grid operation" } } + }, + "exceptions": { + "api_changed": { + "message": "The Powerwall API has changed" + } } } diff --git a/tests/components/powerwall/test_init.py b/tests/components/powerwall/test_init.py index fe03d3371265..fda6de41771d 100644 --- a/tests/components/powerwall/test_init.py +++ b/tests/components/powerwall/test_init.py @@ -2,10 +2,11 @@ import datetime from http.cookies import Morsel -from unittest.mock import MagicMock, patch +from unittest.mock import MagicMock, Mock, patch from aiohttp import CookieJar from tesla_powerwall import AccessDeniedError, ApiError, LoginResponse +from tesla_powerwall.error import MissingAttributeError from homeassistant.components.powerwall.const import ( AUTH_COOKIE_KEY, @@ -377,3 +378,25 @@ async def test_setup_non_404_api_error_propagates(hass: HomeAssistant) -> None: await hass.async_block_till_done() assert config_entry.state is ConfigEntryState.SETUP_RETRY + + +async def test_setup_api_changed(hass: HomeAssistant) -> None: + """Test setup fails when the Powerwall API has changed.""" + mock_powerwall = await _mock_powerwall_with_fixtures(hass) + config_entry = MockConfigEntry(domain=DOMAIN, data={CONF_IP_ADDRESS: "1.2.3.4"}) + config_entry.add_to_hass(hass) + + with ( + patch( + "homeassistant.components.powerwall.Powerwall", return_value=mock_powerwall + ), + patch( + "homeassistant.components.powerwall._login_and_fetch_base_info", + side_effect=MissingAttributeError(Mock(), "din", "operation"), + ), + ): + await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + + assert config_entry.state is ConfigEntryState.SETUP_ERROR + assert config_entry.reason == "The Powerwall API has changed"