Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Sun WEG integration."""
2 
3 from collections.abc import Mapping
4 from typing import Any
5 
6 from sunweg.api import APIHelper, SunWegApiError
7 import voluptuous as vol
8 
9 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
10 from homeassistant.const import CONF_NAME, CONF_PASSWORD, CONF_USERNAME
11 from homeassistant.core import callback
12 
13 from .const import CONF_PLANT_ID, DOMAIN
14 
15 
16 class SunWEGConfigFlow(ConfigFlow, domain=DOMAIN):
17  """Config flow class."""
18 
19  VERSION = 1
20 
21  def __init__(self) -> None:
22  """Initialise sun weg server flow."""
23  self.apiapi: APIHelper = None
24  self.datadata: dict[str, Any] = {}
25 
26  @callback
27  def _async_show_user_form(self, step_id: str, errors=None) -> ConfigFlowResult:
28  """Show the form to the user."""
29  default_username = ""
30  if CONF_USERNAME in self.datadata:
31  default_username = self.datadata[CONF_USERNAME]
32  data_schema = vol.Schema(
33  {
34  vol.Required(CONF_USERNAME, default=default_username): str,
35  vol.Required(CONF_PASSWORD): str,
36  }
37  )
38 
39  return self.async_show_formasync_show_formasync_show_form(
40  step_id=step_id, data_schema=data_schema, errors=errors
41  )
42 
44  self, step: str, username: str, password: str
45  ) -> ConfigFlowResult | None:
46  """Set username and password."""
47  if self.apiapi:
48  # Set username and password
49  self.apiapi.username = username
50  self.apiapi.password = password
51  else:
52  # Initialise the library with the username & password
53  self.apiapi = APIHelper(username, password)
54 
55  try:
56  if not self.apiapi.authenticate():
57  return self._async_show_user_form_async_show_user_form(step, {"base": "invalid_auth"})
58  except SunWegApiError:
59  return self._async_show_user_form_async_show_user_form(step, {"base": "timeout_connect"})
60 
61  return None
62 
63  async def async_step_user(self, user_input=None) -> ConfigFlowResult:
64  """Handle the start of the config flow."""
65  if not user_input:
66  return self._async_show_user_form_async_show_user_form("user")
67 
68  # Store authentication info
69  self.datadata = user_input
70 
71  conf_result = await self.hass.async_add_executor_job(
72  self._set_auth_data_set_auth_data,
73  "user",
74  user_input[CONF_USERNAME],
75  user_input[CONF_PASSWORD],
76  )
77 
78  return await self.async_step_plantasync_step_plant() if conf_result is None else conf_result
79 
80  async def async_step_plant(self, user_input=None) -> ConfigFlowResult:
81  """Handle adding a "plant" to Home Assistant."""
82  plant_list = await self.hass.async_add_executor_job(self.apiapi.listPlants)
83 
84  if len(plant_list) == 0:
85  return self.async_abortasync_abortasync_abort(reason="no_plants")
86 
87  plants = {plant.id: plant.name for plant in plant_list}
88 
89  if user_input is None and len(plant_list) > 1:
90  data_schema = vol.Schema({vol.Required(CONF_PLANT_ID): vol.In(plants)})
91 
92  return self.async_show_formasync_show_formasync_show_form(step_id="plant", data_schema=data_schema)
93 
94  if user_input is None and len(plant_list) == 1:
95  user_input = {CONF_PLANT_ID: plant_list[0].id}
96 
97  user_input[CONF_NAME] = plants[user_input[CONF_PLANT_ID]]
98  await self.async_set_unique_idasync_set_unique_id(user_input[CONF_PLANT_ID])
99  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
100  self.datadata.update(user_input)
101  return self.async_create_entryasync_create_entryasync_create_entry(title=self.datadata[CONF_NAME], data=self.datadata)
102 
103  async def async_step_reauth(
104  self, entry_data: Mapping[str, Any]
105  ) -> ConfigFlowResult:
106  """Handle reauthorization request from SunWEG."""
107  self.datadata.update(entry_data)
108  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
109 
111  self, user_input: dict[str, Any] | None = None
112  ) -> ConfigFlowResult:
113  """Handle reauthorization flow."""
114  if user_input is None:
115  return self._async_show_user_form_async_show_user_form("reauth_confirm")
116 
117  self.datadata.update(user_input)
118  conf_result = await self.hass.async_add_executor_job(
119  self._set_auth_data_set_auth_data,
120  "reauth_confirm",
121  user_input[CONF_USERNAME],
122  user_input[CONF_PASSWORD],
123  )
124  if conf_result is not None:
125  return conf_result
126 
127  return self.async_update_reload_and_abortasync_update_reload_and_abort(
128  self._get_reauth_entry_get_reauth_entry(), data=self.datadata
129  )
ConfigFlowResult async_step_user(self, user_input=None)
Definition: config_flow.py:63
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:112
ConfigFlowResult _async_show_user_form(self, str step_id, errors=None)
Definition: config_flow.py:27
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:105
ConfigFlowResult|None _set_auth_data(self, str step, str username, str password)
Definition: config_flow.py:45
ConfigFlowResult async_step_plant(self, user_input=None)
Definition: config_flow.py:80
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_update_reload_and_abort(self, ConfigEntry entry, *str|None|UndefinedType unique_id=UNDEFINED, str|UndefinedType title=UNDEFINED, Mapping[str, Any]|UndefinedType data=UNDEFINED, Mapping[str, Any]|UndefinedType data_updates=UNDEFINED, Mapping[str, Any]|UndefinedType options=UNDEFINED, str|UndefinedType reason=UNDEFINED, bool reload_even_if_entry_is_unchanged=True)
ConfigFlowResult async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=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)
_FlowResultT async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)
IssData update(pyiss.ISS iss)
Definition: __init__.py:33
dict[str, str|bool] authenticate(HomeAssistant hass, str host, str security_code)
Definition: config_flow.py:132