core/homeassistant/components/wallbox/config_flow.py

59 lines
1.7 KiB
Python

"""Config flow for Wallbox integration."""
import voluptuous as vol
from homeassistant import config_entries, core
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from . import InvalidAuth, WallboxHub
from .const import CONF_STATION, DOMAIN
COMPONENT_DOMAIN = DOMAIN
STEP_USER_DATA_SCHEMA = vol.Schema(
{
vol.Required(CONF_STATION): str,
vol.Required(CONF_USERNAME): str,
vol.Required(CONF_PASSWORD): str,
}
)
async def validate_input(hass: core.HomeAssistant, data):
"""Validate the user input allows to connect.
Data has the keys from STEP_USER_DATA_SCHEMA with values provided by the user.
"""
hub = WallboxHub(data["station"], data["username"], data["password"], hass)
await hub.async_get_data()
# Return info that you want to store in the config entry.
return {"title": "Wallbox Portal"}
class ConfigFlow(config_entries.ConfigFlow, domain=COMPONENT_DOMAIN):
"""Handle a config flow for Wallbox."""
async def async_step_user(self, user_input=None):
"""Handle the initial step."""
if user_input is None:
return self.async_show_form(
step_id="user",
data_schema=STEP_USER_DATA_SCHEMA,
)
errors = {}
try:
info = await validate_input(self.hass, user_input)
except ConnectionError:
errors["base"] = "cannot_connect"
except InvalidAuth:
errors["base"] = "invalid_auth"
else:
return self.async_create_entry(title=info["title"], data=user_input)
return self.async_show_form(
step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
)