Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Adax integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 import adax
9 import adax_local
10 import voluptuous as vol
11 
12 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
13 from homeassistant.const import (
14  CONF_IP_ADDRESS,
15  CONF_PASSWORD,
16  CONF_TOKEN,
17  CONF_UNIQUE_ID,
18 )
19 from homeassistant.helpers.aiohttp_client import async_get_clientsession
20 
21 from .const import (
22  ACCOUNT_ID,
23  CLOUD,
24  CONNECTION_TYPE,
25  DOMAIN,
26  LOCAL,
27  WIFI_PSWD,
28  WIFI_SSID,
29 )
30 
31 _LOGGER = logging.getLogger(__name__)
32 
33 
34 class AdaxConfigFlow(ConfigFlow, domain=DOMAIN):
35  """Handle a config flow for Adax."""
36 
37  VERSION = 2
38 
39  async def async_step_user(
40  self, user_input: dict[str, Any] | None = None
41  ) -> ConfigFlowResult:
42  """Handle the initial step."""
43  data_schema = vol.Schema(
44  {
45  vol.Required(CONNECTION_TYPE, default=CLOUD): vol.In(
46  (
47  CLOUD,
48  LOCAL,
49  )
50  )
51  }
52  )
53 
54  if user_input is None:
55  return self.async_show_formasync_show_formasync_show_form(
56  step_id="user",
57  data_schema=data_schema,
58  )
59 
60  if user_input[CONNECTION_TYPE] == LOCAL:
61  return await self.async_step_localasync_step_local()
62  return await self.async_step_cloudasync_step_cloud()
63 
64  async def async_step_local(
65  self, user_input: dict[str, Any] | None = None
66  ) -> ConfigFlowResult:
67  """Handle the local step."""
68  data_schema = vol.Schema(
69  {vol.Required(WIFI_SSID): str, vol.Required(WIFI_PSWD): str}
70  )
71  if user_input is None:
72  return self.async_show_formasync_show_formasync_show_form(
73  step_id="local",
74  data_schema=data_schema,
75  )
76 
77  wifi_ssid = user_input[WIFI_SSID].replace(" ", "")
78  wifi_pswd = user_input[WIFI_PSWD].replace(" ", "")
79  configurator = adax_local.AdaxConfig(wifi_ssid, wifi_pswd)
80 
81  try:
82  device_configured = await configurator.configure_device()
83  except adax_local.HeaterNotAvailable:
84  return self.async_abortasync_abortasync_abort(reason="heater_not_available")
85  except adax_local.HeaterNotFound:
86  return self.async_abortasync_abortasync_abort(reason="heater_not_found")
87  except adax_local.InvalidWifiCred:
88  return self.async_abortasync_abortasync_abort(reason="invalid_auth")
89 
90  if not device_configured:
91  return self.async_show_formasync_show_formasync_show_form(
92  step_id="local",
93  data_schema=data_schema,
94  errors={"base": "cannot_connect"},
95  )
96 
97  unique_id = str(configurator.mac_id)
98  await self.async_set_unique_idasync_set_unique_id(unique_id)
99  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
100 
101  return self.async_create_entryasync_create_entryasync_create_entry(
102  title=unique_id,
103  data={
104  CONF_IP_ADDRESS: configurator.device_ip,
105  CONF_TOKEN: configurator.access_token,
106  CONF_UNIQUE_ID: unique_id,
107  CONNECTION_TYPE: LOCAL,
108  },
109  )
110 
111  async def async_step_cloud(
112  self, user_input: dict[str, Any] | None = None
113  ) -> ConfigFlowResult:
114  """Handle the cloud step."""
115  data_schema = vol.Schema(
116  {vol.Required(ACCOUNT_ID): int, vol.Required(CONF_PASSWORD): str}
117  )
118  if user_input is None:
119  return self.async_show_formasync_show_formasync_show_form(step_id="cloud", data_schema=data_schema)
120 
121  errors = {}
122 
123  await self.async_set_unique_idasync_set_unique_id(str(user_input[ACCOUNT_ID]))
124  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
125 
126  account_id = user_input[ACCOUNT_ID]
127  password = user_input[CONF_PASSWORD].replace(" ", "")
128 
129  token = await adax.get_adax_token(
130  async_get_clientsession(self.hass), account_id, password
131  )
132  if token is None:
133  _LOGGER.debug("Adax: Failed to login to retrieve token")
134  errors["base"] = "cannot_connect"
135  return self.async_show_formasync_show_formasync_show_form(
136  step_id="cloud",
137  data_schema=data_schema,
138  errors=errors,
139  )
140 
141  return self.async_create_entryasync_create_entryasync_create_entry(
142  title=str(user_input[ACCOUNT_ID]),
143  data={
144  ACCOUNT_ID: account_id,
145  CONF_PASSWORD: password,
146  CONNECTION_TYPE: CLOUD,
147  },
148  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:41
ConfigFlowResult async_step_cloud(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:113
ConfigFlowResult async_step_local(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:66
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_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)
str
_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)
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)