diff --git a/homeassistant/components/wallbox/__init__.py b/homeassistant/components/wallbox/__init__.py index 4ea2cf98be1..b2f8ac7fd5d 100644 --- a/homeassistant/components/wallbox/__init__.py +++ b/homeassistant/components/wallbox/__init__.py @@ -10,7 +10,7 @@ from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryAuthFailed from .const import CONF_STATION, DOMAIN, UPDATE_INTERVAL -from .coordinator import InvalidAuth, WallboxCoordinator +from .coordinator import InvalidAuth, WallboxCoordinator, async_validate_input PLATFORMS = [Platform.LOCK, Platform.NUMBER, Platform.SENSOR, Platform.SWITCH] @@ -22,18 +22,16 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: entry.data[CONF_PASSWORD], jwtTokenDrift=UPDATE_INTERVAL, ) + try: + await async_validate_input(hass, wallbox) + except InvalidAuth as ex: + raise ConfigEntryAuthFailed from ex + wallbox_coordinator = WallboxCoordinator( entry.data[CONF_STATION], wallbox, hass, ) - - try: - await wallbox_coordinator.async_validate_input() - - except InvalidAuth as ex: - raise ConfigEntryAuthFailed from ex - await wallbox_coordinator.async_config_entry_first_refresh() hass.data.setdefault(DOMAIN, {})[entry.entry_id] = wallbox_coordinator diff --git a/homeassistant/components/wallbox/config_flow.py b/homeassistant/components/wallbox/config_flow.py index 0969de432f0..bdc51eef963 100644 --- a/homeassistant/components/wallbox/config_flow.py +++ b/homeassistant/components/wallbox/config_flow.py @@ -13,7 +13,7 @@ from homeassistant.const import CONF_PASSWORD, CONF_USERNAME from homeassistant.core import HomeAssistant from .const import CONF_STATION, DOMAIN -from .coordinator import InvalidAuth, WallboxCoordinator +from .coordinator import InvalidAuth, async_validate_input COMPONENT_DOMAIN = DOMAIN @@ -32,9 +32,8 @@ async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str, Data has the keys from STEP_USER_DATA_SCHEMA with values provided by the user. """ wallbox = Wallbox(data["username"], data["password"]) - wallbox_coordinator = WallboxCoordinator(data["station"], wallbox, hass) - await wallbox_coordinator.async_validate_input() + await async_validate_input(hass, wallbox) # Return info that you want to store in the config entry. return {"title": "Wallbox Portal"} diff --git a/homeassistant/components/wallbox/coordinator.py b/homeassistant/components/wallbox/coordinator.py index f3679551bc4..99c565d9c0c 100644 --- a/homeassistant/components/wallbox/coordinator.py +++ b/homeassistant/components/wallbox/coordinator.py @@ -89,6 +89,21 @@ def _require_authentication[_WallboxCoordinatorT: WallboxCoordinator, **_P]( return require_authentication +def _validate(wallbox: Wallbox) -> None: + """Authenticate using Wallbox API.""" + try: + wallbox.authenticate() + except requests.exceptions.HTTPError as wallbox_connection_error: + if wallbox_connection_error.response.status_code == 403: + raise InvalidAuth from wallbox_connection_error + raise ConnectionError from wallbox_connection_error + + +async def async_validate_input(hass: HomeAssistant, wallbox: Wallbox) -> None: + """Get new sensor data for Wallbox component.""" + await hass.async_add_executor_job(_validate, wallbox) + + class WallboxCoordinator(DataUpdateCoordinator[dict[str, Any]]): """Wallbox Coordinator class.""" @@ -108,19 +123,6 @@ class WallboxCoordinator(DataUpdateCoordinator[dict[str, Any]]): """Authenticate using Wallbox API.""" self._wallbox.authenticate() - def _validate(self) -> None: - """Authenticate using Wallbox API.""" - try: - self._wallbox.authenticate() - except requests.exceptions.HTTPError as wallbox_connection_error: - if wallbox_connection_error.response.status_code == 403: - raise InvalidAuth from wallbox_connection_error - raise ConnectionError from wallbox_connection_error - - async def async_validate_input(self) -> None: - """Get new sensor data for Wallbox component.""" - await self.hass.async_add_executor_job(self._validate) - @_require_authentication def _get_data(self) -> dict[str, Any]: """Get new sensor data for Wallbox component."""