Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Local file."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from typing import Any, cast
7 
8 import voluptuous as vol
9 
10 from homeassistant.const import CONF_FILE_PATH, CONF_NAME
12  SchemaCommonFlowHandler,
13  SchemaConfigFlowHandler,
14  SchemaFlowError,
15  SchemaFlowFormStep,
16 )
17 from homeassistant.helpers.selector import TextSelector
18 
19 from .const import DEFAULT_NAME, DOMAIN
20 from .util import check_file_path_access
21 
22 
23 async def validate_options(
24  handler: SchemaCommonFlowHandler, user_input: dict[str, Any]
25 ) -> dict[str, Any]:
26  """Validate options selected."""
27  file_path: str = user_input[CONF_FILE_PATH]
28  if not await handler.parent_handler.hass.async_add_executor_job(
29  check_file_path_access, file_path
30  ):
31  raise SchemaFlowError("not_readable_path")
32 
33  handler.parent_handler._async_abort_entries_match( # noqa: SLF001
34  {CONF_FILE_PATH: user_input[CONF_FILE_PATH]}
35  )
36 
37  return user_input
38 
39 
40 DATA_SCHEMA_OPTIONS = vol.Schema(
41  {
42  vol.Required(CONF_FILE_PATH): TextSelector(),
43  }
44 )
45 DATA_SCHEMA_SETUP = vol.Schema(
46  {
47  vol.Optional(CONF_NAME, default=DEFAULT_NAME): TextSelector(),
48  }
49 ).extend(DATA_SCHEMA_OPTIONS.schema)
50 
51 CONFIG_FLOW = {
52  "user": SchemaFlowFormStep(
53  schema=DATA_SCHEMA_SETUP,
54  validate_user_input=validate_options,
55  ),
56  "import": SchemaFlowFormStep(
57  schema=DATA_SCHEMA_SETUP,
58  validate_user_input=validate_options,
59  ),
60 }
61 OPTIONS_FLOW = {
62  "init": SchemaFlowFormStep(
63  DATA_SCHEMA_OPTIONS,
64  validate_user_input=validate_options,
65  )
66 }
67 
68 
70  """Handle a config flow for Local file."""
71 
72  config_flow = CONFIG_FLOW
73  options_flow = OPTIONS_FLOW
74 
75  def async_config_entry_title(self, options: Mapping[str, Any]) -> str:
76  """Return config entry title."""
77  return cast(str, options[CONF_NAME])
dict[str, Any] validate_options(SchemaCommonFlowHandler handler, dict[str, Any] user_input)
Definition: config_flow.py:25