Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for todoist integration."""
2 
3 from http import HTTPStatus
4 import logging
5 from typing import Any
6 
7 from requests.exceptions import HTTPError
8 from todoist_api_python.api_async import TodoistAPIAsync
9 import voluptuous as vol
10 
11 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
12 from homeassistant.const import CONF_TOKEN
13 
14 from .const import DOMAIN
15 
16 _LOGGER = logging.getLogger(__name__)
17 
18 SETTINGS_URL = "https://app.todoist.com/app/settings/integrations/developer"
19 
20 STEP_USER_DATA_SCHEMA = vol.Schema(
21  {
22  vol.Required(CONF_TOKEN): str,
23  }
24 )
25 
26 
27 class TodoistConfigFlow(ConfigFlow, domain=DOMAIN):
28  """Handle a config flow for todoist."""
29 
30  VERSION = 1
31 
32  async def async_step_user(
33  self, user_input: dict[str, Any] | None = None
34  ) -> ConfigFlowResult:
35  """Handle the initial step."""
36  if self._async_current_entries_async_current_entries():
37  return self.async_abortasync_abortasync_abort(reason="single_instance_allowed")
38 
39  errors: dict[str, str] = {}
40  if user_input is not None:
41  api = TodoistAPIAsync(user_input[CONF_TOKEN])
42  try:
43  await api.get_tasks()
44  except HTTPError as err:
45  if err.response.status_code == HTTPStatus.UNAUTHORIZED:
46  errors["base"] = "invalid_api_key"
47  else:
48  errors["base"] = "cannot_connect"
49  except Exception:
50  _LOGGER.exception("Unexpected exception")
51  errors["base"] = "unknown"
52  else:
53  return self.async_create_entryasync_create_entryasync_create_entry(title="Todoist", data=user_input)
54 
55  return self.async_show_formasync_show_formasync_show_form(
56  step_id="user",
57  data_schema=STEP_USER_DATA_SCHEMA,
58  errors=errors,
59  description_placeholders={"settings_url": SETTINGS_URL},
60  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:34
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)
list[ConfigEntry] _async_current_entries(self, bool|None include_ignore=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)
_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)