Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """The filesize config flow."""
2 
3 from __future__ import annotations
4 
5 import logging
6 import pathlib
7 from typing import Any
8 
9 import voluptuous as vol
10 
11 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
12 from homeassistant.const import CONF_FILE_PATH
13 from homeassistant.core import HomeAssistant
14 
15 from .const import DOMAIN
16 
17 DATA_SCHEMA = vol.Schema({vol.Required(CONF_FILE_PATH): str})
18 
19 _LOGGER = logging.getLogger(__name__)
20 
21 
22 def validate_path(hass: HomeAssistant, path: str) -> tuple[str | None, dict[str, str]]:
23  """Validate path."""
24  get_path = pathlib.Path(path)
25  if not get_path.exists() or not get_path.is_file():
26  _LOGGER.error("Can not access file %s", path)
27  return (None, {"base": "not_valid"})
28 
29  if not hass.config.is_allowed_path(path):
30  _LOGGER.error("Filepath %s is not allowed", path)
31  return (None, {"base": "not_allowed"})
32 
33  full_path = get_path.absolute()
34 
35  return (str(full_path), {})
36 
37 
38 class FilesizeConfigFlow(ConfigFlow, domain=DOMAIN):
39  """Config flow for Filesize."""
40 
41  VERSION = 1
42 
43  async def async_step_user(
44  self, user_input: dict[str, Any] | None = None
45  ) -> ConfigFlowResult:
46  """Handle a flow initialized by the user."""
47  errors: dict[str, str] = {}
48 
49  if user_input is not None:
50  full_path, errors = await self.hass.async_add_executor_job(
51  validate_path, self.hass, user_input[CONF_FILE_PATH]
52  )
53  if not errors:
54  await self.async_set_unique_idasync_set_unique_id(full_path)
55  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
56 
57  name = str(user_input[CONF_FILE_PATH]).rsplit("/", maxsplit=1)[-1]
58  return self.async_create_entryasync_create_entryasync_create_entry(
59  title=name,
60  data={CONF_FILE_PATH: user_input[CONF_FILE_PATH]},
61  )
62 
63  return self.async_show_formasync_show_formasync_show_form(
64  step_id="user", data_schema=DATA_SCHEMA, errors=errors
65  )
66 
68  self, user_input: dict[str, Any] | None = None
69  ) -> ConfigFlowResult:
70  """Handle a reconfigure flow initialized by the user."""
71  errors: dict[str, str] = {}
72 
73  if user_input is not None:
74  reconfigure_entry = self._get_reconfigure_entry_get_reconfigure_entry()
75  full_path, errors = await self.hass.async_add_executor_job(
76  validate_path, self.hass, user_input[CONF_FILE_PATH]
77  )
78  if not errors:
79  await self.async_set_unique_idasync_set_unique_id(full_path)
80  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
81 
82  name = str(user_input[CONF_FILE_PATH]).rsplit("/", maxsplit=1)[-1]
83  return self.async_update_reload_and_abortasync_update_reload_and_abort(
84  reconfigure_entry,
85  title=name,
86  unique_id=self.unique_idunique_id,
87  data_updates={CONF_FILE_PATH: user_input[CONF_FILE_PATH]},
88  )
89 
90  return self.async_show_formasync_show_formasync_show_form(
91  step_id="reconfigure", data_schema=DATA_SCHEMA, errors=errors
92  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:45
ConfigFlowResult async_step_reconfigure(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:69
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_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_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)
str
_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)
tuple[str|None, dict[str, str]] validate_path(HomeAssistant hass, str path)
Definition: config_flow.py:22