Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Kostal Plenticore Solar Inverter integration."""
2 
3 import logging
4 from typing import Any
5 
6 from aiohttp.client_exceptions import ClientError
7 from pykoplenti import ApiClient, AuthenticationException
8 import voluptuous as vol
9 
10 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
11 from homeassistant.const import CONF_BASE, CONF_HOST, CONF_PASSWORD
12 from homeassistant.core import HomeAssistant
13 from homeassistant.helpers.aiohttp_client import async_get_clientsession
14 
15 from .const import DOMAIN
16 from .helper import get_hostname_id
17 
18 _LOGGER = logging.getLogger(__name__)
19 
20 DATA_SCHEMA = vol.Schema(
21  {
22  vol.Required(CONF_HOST): str,
23  vol.Required(CONF_PASSWORD): str,
24  }
25 )
26 
27 
28 async def test_connection(hass: HomeAssistant, data) -> str:
29  """Test the connection to the inverter.
30 
31  Data has the keys from DATA_SCHEMA with values provided by the user.
32  """
33 
34  session = async_get_clientsession(hass)
35  async with ApiClient(session, data["host"]) as client:
36  await client.login(data["password"])
37  hostname_id = await get_hostname_id(client)
38  values = await client.get_setting_values("scb:network", hostname_id)
39 
40  return values["scb:network"][hostname_id]
41 
42 
43 class KostalPlenticoreConfigFlow(ConfigFlow, domain=DOMAIN):
44  """Handle a config flow for Kostal Plenticore Solar Inverter."""
45 
46  VERSION = 1
47 
48  async def async_step_user(
49  self, user_input: dict[str, Any] | None = None
50  ) -> ConfigFlowResult:
51  """Handle the initial step."""
52  errors = {}
53 
54  if user_input is not None:
55  self._async_abort_entries_match_async_abort_entries_match({CONF_HOST: user_input[CONF_HOST]})
56 
57  try:
58  hostname = await test_connection(self.hass, user_input)
59  except AuthenticationException as ex:
60  errors[CONF_PASSWORD] = "invalid_auth"
61  _LOGGER.error("Error response: %s", ex)
62  except (ClientError, TimeoutError):
63  errors[CONF_HOST] = "cannot_connect"
64  except Exception:
65  _LOGGER.exception("Unexpected exception")
66  errors[CONF_BASE] = "unknown"
67  else:
68  return self.async_create_entryasync_create_entryasync_create_entry(title=hostname, data=user_input)
69 
70  return self.async_show_formasync_show_formasync_show_form(
71  step_id="user", data_schema=DATA_SCHEMA, errors=errors
72  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:50
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)
None _async_abort_entries_match(self, dict[str, Any]|None match_dict=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)
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)