1 """Config flow for the MELCloud platform."""
3 from __future__
import annotations
6 from collections.abc
import Mapping
7 from http
import HTTPStatus
11 from aiohttp
import ClientError, ClientResponseError
13 import voluptuous
as vol
19 from .const
import DOMAIN
21 _LOGGER = logging.getLogger(__name__)
25 """Handle a config flow."""
29 async
def _create_entry(self, username: str, token: str) -> ConfigFlowResult:
30 """Register new entry."""
34 title=username, data={CONF_USERNAME: username, CONF_TOKEN: token}
41 password: str |
None =
None,
42 token: str |
None =
None,
43 ) -> ConfigFlowResult:
46 async
with asyncio.timeout(10):
47 if (acquired_token := token)
is None:
48 acquired_token = await pymelcloud.login(
53 await pymelcloud.get_devices(
57 except ClientResponseError
as err:
58 if err.status
in (HTTPStatus.UNAUTHORIZED, HTTPStatus.FORBIDDEN):
61 except (TimeoutError, ClientError):
64 return await self.
_create_entry_create_entry(username, acquired_token)
67 self, user_input: dict[str, Any] |
None =
None
68 ) -> ConfigFlowResult:
69 """User initiated config flow."""
70 if user_input
is None:
73 data_schema=vol.Schema(
74 {vol.Required(CONF_USERNAME): str, vol.Required(CONF_PASSWORD): str}
77 username = user_input[CONF_USERNAME]
78 return await self.
_create_client_create_client(username, password=user_input[CONF_PASSWORD])
81 self, entry_data: Mapping[str, Any]
82 ) -> ConfigFlowResult:
83 """Handle initiation of re-authentication with MELCloud."""
87 self, user_input: dict[str, Any] |
None =
None
88 ) -> ConfigFlowResult:
89 """Handle re-authentication with MELCloud."""
90 errors: dict[str, str] = {}
92 if user_input
is not None:
100 step_id=
"reauth_confirm",
101 data_schema=vol.Schema(
102 {vol.Required(CONF_USERNAME): str, vol.Required(CONF_PASSWORD): str}
108 self, user_input: dict[str, Any]
109 ) -> tuple[str |
None, dict[str, str]]:
110 """Reauthenticate with MELCloud."""
111 errors: dict[str, str] = {}
112 acquired_token =
None
115 async
with asyncio.timeout(10):
116 acquired_token = await pymelcloud.login(
117 user_input[CONF_USERNAME],
118 user_input[CONF_PASSWORD],
121 except (ClientResponseError, AttributeError)
as err:
123 isinstance(err, ClientResponseError)
126 HTTPStatus.UNAUTHORIZED,
127 HTTPStatus.FORBIDDEN,
129 or isinstance(err, AttributeError)
130 and err.name ==
"get"
132 errors[
"base"] =
"invalid_auth"
134 errors[
"base"] =
"cannot_connect"
139 errors[
"base"] =
"cannot_connect"
141 return acquired_token, errors
144 self, user_input: dict[str, Any] |
None =
None
145 ) -> ConfigFlowResult:
146 """Handle a reconfiguration flow initialized by the user."""
147 errors: dict[str, str] = {}
148 acquired_token =
None
151 if user_input
is not None:
152 user_input[CONF_USERNAME] = reconfigure_entry.data[CONF_USERNAME]
154 async
with asyncio.timeout(10):
155 acquired_token = await pymelcloud.login(
156 user_input[CONF_USERNAME],
157 user_input[CONF_PASSWORD],
160 except (ClientResponseError, AttributeError)
as err:
162 isinstance(err, ClientResponseError)
165 HTTPStatus.UNAUTHORIZED,
166 HTTPStatus.FORBIDDEN,
168 or isinstance(err, AttributeError)
169 and err.name ==
"get"
171 errors[
"base"] =
"invalid_auth"
173 errors[
"base"] =
"cannot_connect"
178 errors[
"base"] =
"cannot_connect"
181 user_input[CONF_TOKEN] = acquired_token
183 reconfigure_entry, data_updates=user_input
187 step_id=
"reconfigure",
188 data_schema=vol.Schema(
190 vol.Required(CONF_PASSWORD): str,
194 description_placeholders={
195 CONF_USERNAME: reconfigure_entry.data[CONF_USERNAME]
ConfigFlowResult _create_client(self, str username, *str|None password=None, str|None token=None)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
ConfigFlowResult _create_entry(self, str username, str token)
ConfigFlowResult async_step_reconfigure(self, dict[str, Any]|None user_input=None)
tuple[str|None, dict[str, str]] async_reauthenticate_client(self, dict[str, Any] user_input)
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
None _abort_if_unique_id_configured(self, dict[str, Any]|None updates=None, bool reload_on_update=True, *str error="already_configured")
ConfigEntry _get_reauth_entry(self)
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)
ConfigEntry _get_reconfigure_entry(self)
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)
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)