Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Adds config flow for Mill integration."""
2 
3 from typing import Any
4 
5 from mill import Mill
6 from mill_local import Mill as MillLocal
7 import voluptuous as vol
8 
9 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
10 from homeassistant.const import CONF_IP_ADDRESS, CONF_PASSWORD, CONF_USERNAME
11 from homeassistant.helpers.aiohttp_client import async_get_clientsession
12 
13 from .const import CLOUD, CONNECTION_TYPE, DOMAIN, LOCAL
14 
15 
16 class MillConfigFlow(ConfigFlow, domain=DOMAIN):
17  """Handle a config flow for Mill integration."""
18 
19  VERSION = 1
20 
21  async def async_step_user(
22  self, user_input: dict[str, Any] | None = None
23  ) -> ConfigFlowResult:
24  """Handle the initial step."""
25  data_schema = vol.Schema(
26  {
27  vol.Required(CONNECTION_TYPE, default=CLOUD): vol.In(
28  (
29  CLOUD,
30  LOCAL,
31  )
32  )
33  }
34  )
35 
36  if user_input is None:
37  return self.async_show_formasync_show_formasync_show_form(
38  step_id="user",
39  data_schema=data_schema,
40  )
41 
42  if user_input[CONNECTION_TYPE] == LOCAL:
43  return await self.async_step_localasync_step_local()
44  return await self.async_step_cloudasync_step_cloud()
45 
46  async def async_step_local(
47  self, user_input: dict[str, str] | None = None
48  ) -> ConfigFlowResult:
49  """Handle the local step."""
50  data_schema = vol.Schema({vol.Required(CONF_IP_ADDRESS): str})
51  if user_input is None:
52  return self.async_show_formasync_show_formasync_show_form(
53  step_id="local",
54  data_schema=data_schema,
55  )
56 
57  mill_data_connection = MillLocal(
58  user_input[CONF_IP_ADDRESS],
59  websession=async_get_clientsession(self.hass),
60  )
61 
62  await self.async_set_unique_idasync_set_unique_id(mill_data_connection.device_ip)
63  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
64 
65  if not await mill_data_connection.connect():
66  return self.async_show_formasync_show_formasync_show_form(
67  step_id="local",
68  data_schema=data_schema,
69  errors={"base": "cannot_connect"},
70  )
71 
72  return self.async_create_entryasync_create_entryasync_create_entry(
73  title=user_input[CONF_IP_ADDRESS],
74  data={
75  CONF_IP_ADDRESS: user_input[CONF_IP_ADDRESS],
76  CONNECTION_TYPE: LOCAL,
77  },
78  )
79 
80  async def async_step_cloud(
81  self, user_input: dict[str, str] | None = None
82  ) -> ConfigFlowResult:
83  """Handle the cloud step."""
84  data_schema = vol.Schema(
85  {vol.Required(CONF_USERNAME): str, vol.Required(CONF_PASSWORD): str}
86  )
87  if user_input is None:
88  return self.async_show_formasync_show_formasync_show_form(
89  step_id="cloud",
90  data_schema=data_schema,
91  errors={},
92  )
93 
94  username = user_input[CONF_USERNAME].replace(" ", "")
95  password = user_input[CONF_PASSWORD].replace(" ", "")
96 
97  mill_data_connection = Mill(
98  username,
99  password,
100  websession=async_get_clientsession(self.hass),
101  )
102 
103  errors = {}
104 
105  if not await mill_data_connection.connect():
106  errors["base"] = "cannot_connect"
107  return self.async_show_formasync_show_formasync_show_form(
108  step_id="cloud",
109  data_schema=data_schema,
110  errors=errors,
111  )
112 
113  unique_id = username
114 
115  await self.async_set_unique_idasync_set_unique_id(unique_id)
116  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
117 
118  return self.async_create_entryasync_create_entryasync_create_entry(
119  title=unique_id,
120  data={
121  CONF_USERNAME: username,
122  CONF_PASSWORD: password,
123  CONNECTION_TYPE: CLOUD,
124  },
125  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:23
ConfigFlowResult async_step_cloud(self, dict[str, str]|None user_input=None)
Definition: config_flow.py:82
ConfigFlowResult async_step_local(self, dict[str, str]|None user_input=None)
Definition: config_flow.py:48
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)
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)