Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for solax integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from solax import real_time_api
9 from solax.discovery import DiscoveryError
10 import voluptuous as vol
11 
12 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
13 from homeassistant.const import CONF_IP_ADDRESS, CONF_PASSWORD, CONF_PORT
15 
16 from .const import DOMAIN
17 
18 _LOGGER = logging.getLogger(__name__)
19 
20 DEFAULT_PORT = 80
21 DEFAULT_PASSWORD = ""
22 
23 STEP_USER_DATA_SCHEMA = vol.Schema(
24  {
25  vol.Required(CONF_IP_ADDRESS): cv.string,
26  vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
27  vol.Optional(CONF_PASSWORD, default=DEFAULT_PASSWORD): cv.string,
28  }
29 )
30 
31 
32 async def validate_api(data) -> str:
33  """Validate the credentials."""
34 
35  api = await real_time_api(
36  data[CONF_IP_ADDRESS], data[CONF_PORT], data[CONF_PASSWORD]
37  )
38  response = await api.get_data()
39  return response.serial_number
40 
41 
42 class SolaxConfigFlow(ConfigFlow, domain=DOMAIN):
43  """Handle a config flow for Solax."""
44 
45  async def async_step_user(
46  self, user_input: dict[str, Any] | None = None
47  ) -> ConfigFlowResult:
48  """Handle the initial step."""
49  errors: dict[str, Any] = {}
50  if user_input is None:
51  return self.async_show_formasync_show_formasync_show_form(
52  step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
53  )
54 
55  try:
56  serial_number = await validate_api(user_input)
57  except (ConnectionError, DiscoveryError):
58  errors["base"] = "cannot_connect"
59  except Exception:
60  _LOGGER.exception("Unexpected exception")
61  errors["base"] = "unknown"
62  else:
63  await self.async_set_unique_idasync_set_unique_id(serial_number)
64  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
65  return self.async_create_entryasync_create_entryasync_create_entry(title=serial_number, data=user_input)
66 
67  return self.async_show_formasync_show_formasync_show_form(
68  step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
69  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:47
None _abort_if_unique_id_configured(self, dict[str, Any]|None updates=None, bool reload_on_update=True, *str error="already_configured")
ConfigEntry|None async_set_unique_id(self, str|None unique_id=None, *bool raise_on_progress=True)
ConfigFlowResult async_create_entry(self, *str title, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None, Mapping[str, Any]|None options=None)
ConfigFlowResult async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
_FlowResultT async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
_FlowResultT async_create_entry(self, *str|None title=None, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None)