mirror of
https://github.com/home-assistant/core.git
synced 2026-12-05 05:14:01 -04:00
Raise ConfigEntryError instead of returning False in huisbaasje setup (#185060)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
246eb3697e
commit
4bd15f6b16
@@ -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)
|
||||
|
||||
@@ -62,5 +62,13 @@
|
||||
"name": "Gas this year"
|
||||
}
|
||||
}
|
||||
},
|
||||
"exceptions": {
|
||||
"authentication_failed": {
|
||||
"message": "Authentication failed"
|
||||
},
|
||||
"customer_overview_failed": {
|
||||
"message": "Getting customer ID failed"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user