mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 02:07:09 +00:00
Move validation routine out of wallbox coordinator (#129415)
This commit is contained in:
parent
dc2028f99c
commit
8e7d782102
@ -10,7 +10,7 @@ from homeassistant.core import HomeAssistant
|
|||||||
from homeassistant.exceptions import ConfigEntryAuthFailed
|
from homeassistant.exceptions import ConfigEntryAuthFailed
|
||||||
|
|
||||||
from .const import CONF_STATION, DOMAIN, UPDATE_INTERVAL
|
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]
|
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],
|
entry.data[CONF_PASSWORD],
|
||||||
jwtTokenDrift=UPDATE_INTERVAL,
|
jwtTokenDrift=UPDATE_INTERVAL,
|
||||||
)
|
)
|
||||||
|
try:
|
||||||
|
await async_validate_input(hass, wallbox)
|
||||||
|
except InvalidAuth as ex:
|
||||||
|
raise ConfigEntryAuthFailed from ex
|
||||||
|
|
||||||
wallbox_coordinator = WallboxCoordinator(
|
wallbox_coordinator = WallboxCoordinator(
|
||||||
entry.data[CONF_STATION],
|
entry.data[CONF_STATION],
|
||||||
wallbox,
|
wallbox,
|
||||||
hass,
|
hass,
|
||||||
)
|
)
|
||||||
|
|
||||||
try:
|
|
||||||
await wallbox_coordinator.async_validate_input()
|
|
||||||
|
|
||||||
except InvalidAuth as ex:
|
|
||||||
raise ConfigEntryAuthFailed from ex
|
|
||||||
|
|
||||||
await wallbox_coordinator.async_config_entry_first_refresh()
|
await wallbox_coordinator.async_config_entry_first_refresh()
|
||||||
|
|
||||||
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = wallbox_coordinator
|
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = wallbox_coordinator
|
||||||
|
@ -13,7 +13,7 @@ from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
|||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from .const import CONF_STATION, DOMAIN
|
from .const import CONF_STATION, DOMAIN
|
||||||
from .coordinator import InvalidAuth, WallboxCoordinator
|
from .coordinator import InvalidAuth, async_validate_input
|
||||||
|
|
||||||
COMPONENT_DOMAIN = DOMAIN
|
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.
|
Data has the keys from STEP_USER_DATA_SCHEMA with values provided by the user.
|
||||||
"""
|
"""
|
||||||
wallbox = Wallbox(data["username"], data["password"])
|
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 info that you want to store in the config entry.
|
||||||
return {"title": "Wallbox Portal"}
|
return {"title": "Wallbox Portal"}
|
||||||
|
@ -89,6 +89,21 @@ def _require_authentication[_WallboxCoordinatorT: WallboxCoordinator, **_P](
|
|||||||
return require_authentication
|
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]]):
|
class WallboxCoordinator(DataUpdateCoordinator[dict[str, Any]]):
|
||||||
"""Wallbox Coordinator class."""
|
"""Wallbox Coordinator class."""
|
||||||
|
|
||||||
@ -108,19 +123,6 @@ class WallboxCoordinator(DataUpdateCoordinator[dict[str, Any]]):
|
|||||||
"""Authenticate using Wallbox API."""
|
"""Authenticate using Wallbox API."""
|
||||||
self._wallbox.authenticate()
|
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
|
@_require_authentication
|
||||||
def _get_data(self) -> dict[str, Any]:
|
def _get_data(self) -> dict[str, Any]:
|
||||||
"""Get new sensor data for Wallbox component."""
|
"""Get new sensor data for Wallbox component."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user