Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow to configure the Nextcloud integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from typing import Any
7 
8 from nextcloudmonitor import (
9  NextcloudMonitor,
10  NextcloudMonitorAuthorizationError,
11  NextcloudMonitorConnectionError,
12  NextcloudMonitorRequestError,
13 )
14 import voluptuous as vol
15 
16 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
17 from homeassistant.const import CONF_PASSWORD, CONF_URL, CONF_USERNAME, CONF_VERIFY_SSL
18 
19 from .const import DEFAULT_VERIFY_SSL, DOMAIN
20 
21 DATA_SCHEMA_USER = vol.Schema(
22  {
23  vol.Required(CONF_URL): str,
24  vol.Required(CONF_USERNAME): str,
25  vol.Required(CONF_PASSWORD): str,
26  vol.Required(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): bool,
27  }
28 )
29 DATA_SCHEMA_REAUTH = vol.Schema(
30  {
31  vol.Required(CONF_USERNAME): str,
32  vol.Required(CONF_PASSWORD): str,
33  }
34 )
35 
36 
37 class NextcloudConfigFlow(ConfigFlow, domain=DOMAIN):
38  """Handle a Nextcloud config flow."""
39 
40  VERSION = 1
41 
42  def _try_connect_nc(self, user_input: dict) -> NextcloudMonitor:
43  """Try to connect to nextcloud server."""
44  return NextcloudMonitor(
45  user_input[CONF_URL],
46  user_input[CONF_USERNAME],
47  user_input[CONF_PASSWORD],
48  user_input.get(CONF_VERIFY_SSL, DEFAULT_VERIFY_SSL),
49  )
50 
51  async def async_step_user(
52  self, user_input: dict[str, Any] | None = None
53  ) -> ConfigFlowResult:
54  """Handle a flow initialized by the user."""
55  errors = {}
56 
57  if user_input is not None:
58  self._async_abort_entries_match_async_abort_entries_match({CONF_URL: user_input.get(CONF_URL)})
59  try:
60  await self.hass.async_add_executor_job(self._try_connect_nc_try_connect_nc, user_input)
61  except NextcloudMonitorAuthorizationError:
62  errors["base"] = "invalid_auth"
63  except (NextcloudMonitorConnectionError, NextcloudMonitorRequestError):
64  errors["base"] = "connection_error"
65  else:
66  return self.async_create_entryasync_create_entryasync_create_entry(
67  title=user_input[CONF_URL],
68  data=user_input,
69  )
70 
71  data_schema = self.add_suggested_values_to_schemaadd_suggested_values_to_schema(DATA_SCHEMA_USER, user_input)
72  return self.async_show_formasync_show_formasync_show_form(
73  step_id="user", data_schema=data_schema, errors=errors
74  )
75 
76  async def async_step_reauth(
77  self, entry_data: Mapping[str, Any]
78  ) -> ConfigFlowResult:
79  """Handle flow upon an API authentication error."""
80  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
81 
83  self, user_input: dict[str, Any] | None = None
84  ) -> ConfigFlowResult:
85  """Handle reauthorization flow."""
86  errors = {}
87 
88  reauth_entry = self._get_reauth_entry_get_reauth_entry()
89  if user_input is not None:
90  try:
91  await self.hass.async_add_executor_job(
92  self._try_connect_nc_try_connect_nc, {**reauth_entry.data, **user_input}
93  )
94  except NextcloudMonitorAuthorizationError:
95  errors["base"] = "invalid_auth"
96  except (NextcloudMonitorConnectionError, NextcloudMonitorRequestError):
97  errors["base"] = "connection_error"
98  else:
99  return self.async_update_reload_and_abortasync_update_reload_and_abort(
100  reauth_entry, data_updates=user_input
101  )
102 
103  data_schema = self.add_suggested_values_to_schemaadd_suggested_values_to_schema(
104  DATA_SCHEMA_REAUTH,
105  {CONF_USERNAME: reauth_entry.data[CONF_USERNAME], **(user_input or {})},
106  )
107  return self.async_show_formasync_show_formasync_show_form(
108  step_id="reauth_confirm",
109  data_schema=data_schema,
110  description_placeholders={"url": reauth_entry.data[CONF_URL]},
111  errors=errors,
112  )
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:84
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:53
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:78
NextcloudMonitor _try_connect_nc(self, dict user_input)
Definition: config_flow.py:42
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)
None _async_abort_entries_match(self, dict[str, Any]|None match_dict=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)
vol.Schema add_suggested_values_to_schema(self, vol.Schema data_schema, Mapping[str, Any]|None suggested_values)
_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)