Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Downloader integration."""
2 
3 from __future__ import annotations
4 
5 import os
6 from typing import Any
7 
8 import voluptuous as vol
9 
10 from homeassistant import exceptions
11 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
12 from homeassistant.helpers import config_validation as cv
13 
14 from .const import _LOGGER, CONF_DOWNLOAD_DIR, DEFAULT_NAME, DOMAIN
15 
16 
17 class DownloaderConfigFlow(ConfigFlow, domain=DOMAIN):
18  """Handle a config flow for Downloader."""
19 
20  VERSION = 1
21 
22  async def async_step_user(
23  self, user_input: dict[str, Any] | None = None
24  ) -> ConfigFlowResult:
25  """Handle the initial step."""
26  errors: dict[str, str] = {}
27 
28  if user_input is not None:
29  try:
30  await self._validate_input_validate_input(user_input)
31  except DirectoryDoesNotExist:
32  errors["base"] = "directory_does_not_exist"
33  else:
34  return self.async_create_entryasync_create_entryasync_create_entry(title=DEFAULT_NAME, data=user_input)
35 
36  return self.async_show_formasync_show_formasync_show_form(
37  step_id="user",
38  data_schema=vol.Schema(
39  {
40  vol.Required(CONF_DOWNLOAD_DIR): cv.string,
41  }
42  ),
43  errors=errors,
44  )
45 
46  async def _validate_input(self, user_input: dict[str, Any]) -> None:
47  """Validate the user input if the directory exists."""
48  download_path = user_input[CONF_DOWNLOAD_DIR]
49  if not os.path.isabs(download_path):
50  download_path = self.hass.config.path(download_path)
51 
52  if not await self.hass.async_add_executor_job(os.path.isdir, download_path):
53  _LOGGER.error(
54  "Download path %s does not exist. File Downloader not active",
55  download_path,
56  )
57  raise DirectoryDoesNotExist
58 
59 
61  """Error to indicate the specified download directory does not exist."""
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:24
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)