diff --git a/homeassistant/components/huisbaasje/__init__.py b/homeassistant/components/huisbaasje/__init__.py index e11d1d23fc45..adafca59b733 100644 --- a/homeassistant/components/huisbaasje/__init__.py +++ b/homeassistant/components/huisbaasje/__init__.py @@ -1,19 +1,16 @@ """The EnergyFlip integration.""" -import logging - from energyflip import EnergyFlip, EnergyFlipException from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform from homeassistant.core import HomeAssistant +from homeassistant.exceptions import ConfigEntryError -from .const import FETCH_TIMEOUT, SOURCE_TYPES +from .const import DOMAIN, FETCH_TIMEOUT, SOURCE_TYPES from .coordinator import EnergyFlipConfigEntry, EnergyFlipUpdateCoordinator PLATFORMS = [Platform.SENSOR] -_LOGGER = logging.getLogger(__name__) - async def async_setup_entry(hass: HomeAssistant, entry: EnergyFlipConfigEntry) -> bool: """Set up EnergyFlip from a config entry.""" @@ -29,15 +26,19 @@ async def async_setup_entry(hass: HomeAssistant, entry: EnergyFlipConfigEntry) - try: await energyflip.authenticate() except EnergyFlipException as exception: - _LOGGER.error("Authentication failed: %s", str(exception)) - return False + raise ConfigEntryError( + translation_domain=DOMAIN, + translation_key="authentication_failed", + ) from exception # Immediately get customer id, since it is required for all api calls try: await energyflip.customer_overview() except EnergyFlipException as exception: - _LOGGER.error("Getting customer id failed: %s", str(exception)) - return False + raise ConfigEntryError( + translation_domain=DOMAIN, + translation_key="customer_overview_failed", + ) from exception # Create a coordinator for polling updates coordinator = EnergyFlipUpdateCoordinator(hass, entry, energyflip) diff --git a/homeassistant/components/huisbaasje/strings.json b/homeassistant/components/huisbaasje/strings.json index 16389de6c664..0c045d0dd932 100644 --- a/homeassistant/components/huisbaasje/strings.json +++ b/homeassistant/components/huisbaasje/strings.json @@ -62,5 +62,13 @@ "name": "Gas this year" } } + }, + "exceptions": { + "authentication_failed": { + "message": "Authentication failed" + }, + "customer_overview_failed": { + "message": "Getting customer ID failed" + } } } diff --git a/tests/components/huisbaasje/test_init.py b/tests/components/huisbaasje/test_init.py index d7274d505021..37f671fa8601 100644 --- a/tests/components/huisbaasje/test_init.py +++ b/tests/components/huisbaasje/test_init.py @@ -63,7 +63,8 @@ async def test_setup_entry_error_authenticate(hass: HomeAssistant) -> None: """Test for unsuccessfully setting a config entry, erroring on authenticate.""" with ( patch( - "energyflip.EnergyFlip.authenticate", side_effect=EnergyFlipException + "energyflip.EnergyFlip.authenticate", + side_effect=EnergyFlipException("Boom"), ) as mock_authenticate, patch( "energyflip.EnergyFlip.customer_overview", @@ -89,6 +90,7 @@ async def test_setup_entry_error_authenticate(hass: HomeAssistant) -> None: # Assert integration is loaded with error assert config_entry.state is ConfigEntryState.SETUP_ERROR + assert config_entry.reason == "Authentication failed" assert DOMAIN not in hass.data # Assert entities are not loaded @@ -110,7 +112,7 @@ async def test_setup_entry_error_customer_overview(hass: HomeAssistant) -> None: ) as mock_authenticate, patch( "energyflip.EnergyFlip.customer_overview", - side_effect=EnergyFlipException, + side_effect=EnergyFlipException("Boom"), ) as mock_customer_overview, ): config_entry = MockConfigEntry( @@ -132,6 +134,7 @@ async def test_setup_entry_error_customer_overview(hass: HomeAssistant) -> None: # Assert integration is loaded with error assert config_entry.state is ConfigEntryState.SETUP_ERROR + assert config_entry.reason == "Getting customer ID failed" assert DOMAIN not in hass.data # Assert entities are not loaded