Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow to configure demo component."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 import voluptuous as vol
8 
10  ConfigEntry,
11  ConfigFlow,
12  ConfigFlowResult,
13  OptionsFlow,
14 )
15 from homeassistant.core import callback
17 
18 from . import DOMAIN
19 
20 CONF_STRING = "string"
21 CONF_BOOLEAN = "bool"
22 CONF_INT = "int"
23 CONF_SELECT = "select"
24 CONF_MULTISELECT = "multi"
25 
26 
27 class DemoConfigFlow(ConfigFlow, domain=DOMAIN):
28  """Demo configuration flow."""
29 
30  VERSION = 1
31 
32  @staticmethod
33  @callback
35  config_entry: ConfigEntry,
36  ) -> OptionsFlowHandler:
37  """Get the options flow for this handler."""
38  return OptionsFlowHandler(config_entry)
39 
40  async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult:
41  """Set the config entry up from yaml."""
42  return self.async_create_entryasync_create_entryasync_create_entry(title="Demo", data=import_data)
43 
44 
46  """Handle options."""
47 
48  def __init__(self, config_entry: ConfigEntry) -> None:
49  """Initialize options flow."""
50  self.optionsoptions = dict(config_entry.options)
51 
52  async def async_step_init(
53  self, user_input: dict[str, Any] | None = None
54  ) -> ConfigFlowResult:
55  """Manage the options."""
56  return await self.async_step_options_1async_step_options_1()
57 
59  self, user_input: dict[str, Any] | None = None
60  ) -> ConfigFlowResult:
61  """Manage the options."""
62  if user_input is not None:
63  self.optionsoptions.update(user_input)
64  return await self.async_step_options_2async_step_options_2()
65 
66  return self.async_show_formasync_show_form(
67  step_id="options_1",
68  data_schema=vol.Schema(
69  {
70  vol.Required("constant"): "Constant Value",
71  vol.Optional(
72  CONF_BOOLEAN,
73  default=self.config_entryconfig_entryconfig_entry.options.get(CONF_BOOLEAN, False),
74  ): bool,
75  vol.Optional(
76  CONF_INT,
77  default=self.config_entryconfig_entryconfig_entry.options.get(CONF_INT, 10),
78  ): int,
79  }
80  ),
81  )
82 
84  self, user_input: dict[str, Any] | None = None
85  ) -> ConfigFlowResult:
86  """Manage the options 2."""
87  if user_input is not None:
88  self.optionsoptions.update(user_input)
89  return await self._update_options_update_options()
90 
91  return self.async_show_formasync_show_form(
92  step_id="options_2",
93  data_schema=vol.Schema(
94  {
95  vol.Optional(
96  CONF_STRING,
97  default=self.config_entryconfig_entryconfig_entry.options.get(
98  CONF_STRING,
99  "Default",
100  ),
101  ): str,
102  vol.Optional(
103  CONF_SELECT,
104  default=self.config_entryconfig_entryconfig_entry.options.get(CONF_SELECT, "default"),
105  ): vol.In(["default", "other"]),
106  vol.Optional(
107  CONF_MULTISELECT,
108  default=self.config_entryconfig_entryconfig_entry.options.get(
109  CONF_MULTISELECT, ["default"]
110  ),
111  ): cv.multi_select({"default": "Default", "other": "Other"}),
112  }
113  ),
114  )
115 
116  async def _update_options(self) -> ConfigFlowResult:
117  """Update config entry options."""
118  return self.async_create_entryasync_create_entry(title="", data=self.optionsoptions)
OptionsFlowHandler async_get_options_flow(ConfigEntry config_entry)
Definition: config_flow.py:36
ConfigFlowResult async_step_import(self, dict[str, Any] import_data)
Definition: config_flow.py:40
ConfigFlowResult async_step_options_1(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:60
ConfigFlowResult async_step_init(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:54
ConfigFlowResult async_step_options_2(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:85
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)
None config_entry(self, ConfigEntry value)
_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)
IssData update(pyiss.ISS iss)
Definition: __init__.py:33