Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for the SolarEdge platform."""
2 
3 from __future__ import annotations
4 
5 import socket
6 from typing import Any
7 
8 from aiohttp import ClientError
9 import aiosolaredge
10 import voluptuous as vol
11 
12 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
13 from homeassistant.const import CONF_API_KEY, CONF_NAME
14 from homeassistant.core import callback
15 from homeassistant.helpers.aiohttp_client import async_get_clientsession
16 from homeassistant.util import slugify
17 
18 from .const import CONF_SITE_ID, DEFAULT_NAME, DOMAIN
19 
20 
21 class SolarEdgeConfigFlow(ConfigFlow, domain=DOMAIN):
22  """Handle a config flow."""
23 
24  VERSION = 1
25 
26  def __init__(self) -> None:
27  """Initialize the config flow."""
28  self._errors_errors: dict[str, str] = {}
29 
30  @callback
31  def _async_current_site_ids(self) -> set[str]:
32  """Return the site_ids for the domain."""
33  return {
34  entry.data[CONF_SITE_ID]
35  for entry in self._async_current_entries_async_current_entries(include_ignore=False)
36  if CONF_SITE_ID in entry.data
37  }
38 
39  def _site_in_configuration_exists(self, site_id: str) -> bool:
40  """Return True if site_id exists in configuration."""
41  return site_id in self._async_current_site_ids_async_current_site_ids()
42 
43  async def _async_check_site(self, site_id: str, api_key: str) -> bool:
44  """Check if we can connect to the soleredge api service."""
45  session = async_get_clientsession(self.hass)
46  api = aiosolaredge.SolarEdge(api_key, session)
47  try:
48  response = await api.get_details(site_id)
49  if response["details"]["status"].lower() != "active":
50  self._errors_errors[CONF_SITE_ID] = "site_not_active"
51  return False
52  except (TimeoutError, ClientError, socket.gaierror):
53  self._errors_errors[CONF_SITE_ID] = "could_not_connect"
54  return False
55  except KeyError:
56  self._errors_errors[CONF_SITE_ID] = "invalid_api_key"
57  return False
58  return True
59 
60  async def async_step_user(
61  self, user_input: dict[str, Any] | None = None
62  ) -> ConfigFlowResult:
63  """Step when user initializes a integration."""
64  self._errors_errors = {}
65  if user_input is not None:
66  name = slugify(user_input.get(CONF_NAME, DEFAULT_NAME))
67  if self._site_in_configuration_exists_site_in_configuration_exists(user_input[CONF_SITE_ID]):
68  self._errors_errors[CONF_SITE_ID] = "already_configured"
69  else:
70  site = user_input[CONF_SITE_ID]
71  api = user_input[CONF_API_KEY]
72  can_connect = await self._async_check_site_async_check_site(site, api)
73  if can_connect:
74  return self.async_create_entryasync_create_entryasync_create_entry(
75  title=name, data={CONF_SITE_ID: site, CONF_API_KEY: api}
76  )
77 
78  else:
79  user_input = {CONF_NAME: DEFAULT_NAME, CONF_SITE_ID: "", CONF_API_KEY: ""}
80  return self.async_show_formasync_show_formasync_show_form(
81  step_id="user",
82  data_schema=vol.Schema(
83  {
84  vol.Required(
85  CONF_NAME, default=user_input.get(CONF_NAME, DEFAULT_NAME)
86  ): str,
87  vol.Required(CONF_SITE_ID, default=user_input[CONF_SITE_ID]): str,
88  vol.Required(CONF_API_KEY, default=user_input[CONF_API_KEY]): str,
89  }
90  ),
91  errors=self._errors_errors,
92  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:62
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)
list[ConfigEntry] _async_current_entries(self, bool|None include_ignore=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)