Move validation routine out of wallbox coordinator (#129415)

This commit is contained in:
epenet 2024-10-29 18:13:11 +01:00 committed by GitHub
parent dc2028f99c
commit 8e7d782102
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 23 additions and 24 deletions

View File

@ -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

View File

@ -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"}

View File

@ -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."""