Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Version integration."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 import voluptuous as vol
8 
9 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
10 from homeassistant.const import CONF_SOURCE
11 
12 from .const import (
13  ATTR_VERSION_SOURCE,
14  CONF_BETA,
15  CONF_BOARD,
16  CONF_CHANNEL,
17  CONF_IMAGE,
18  CONF_VERSION_SOURCE,
19  DEFAULT_BOARD,
20  DEFAULT_CHANNEL,
21  DEFAULT_CONFIGURATION,
22  DEFAULT_IMAGE,
23  DEFAULT_NAME_CURRENT,
24  DOMAIN,
25  STEP_USER,
26  STEP_VERSION_SOURCE,
27  VALID_BOARDS,
28  VALID_CHANNELS,
29  VALID_CONTAINER_IMAGES,
30  VALID_IMAGES,
31  VERSION_SOURCE_LOCAL,
32  VERSION_SOURCE_MAP,
33 )
34 
35 
36 class VersionConfigFlow(ConfigFlow, domain=DOMAIN):
37  """Handle a config flow for Version."""
38 
39  VERSION = 1
40 
41  def __init__(self) -> None:
42  """Initialize the Version config flow."""
43  self._entry_data_entry_data: dict[str, Any] = DEFAULT_CONFIGURATION.copy()
44 
45  async def async_step_user(
46  self,
47  user_input: dict[str, Any] | None = None,
48  ) -> ConfigFlowResult:
49  """Handle the initial user step."""
50  if user_input is None:
51  self._entry_data_entry_data = DEFAULT_CONFIGURATION.copy()
52  return self.async_show_formasync_show_formasync_show_form(
53  step_id=STEP_USER,
54  data_schema=vol.Schema(
55  {
56  vol.Required(
57  CONF_VERSION_SOURCE,
58  default=VERSION_SOURCE_LOCAL,
59  ): vol.In(VERSION_SOURCE_MAP.keys())
60  }
61  ),
62  )
63 
64  user_input[CONF_SOURCE] = VERSION_SOURCE_MAP[user_input[CONF_VERSION_SOURCE]]
65  self._entry_data_entry_data.update(user_input)
66 
67  if not self.show_advanced_optionsshow_advanced_options or user_input[CONF_SOURCE] in (
68  "local",
69  "haio",
70  ):
71  return self.async_create_entryasync_create_entryasync_create_entry(
72  title=self._config_entry_name_config_entry_name,
73  data=self._entry_data_entry_data,
74  )
75 
76  return await self.async_step_version_sourceasync_step_version_source()
77 
79  self,
80  user_input: dict[str, Any] | None = None,
81  ) -> ConfigFlowResult:
82  """Handle the version_source step."""
83  if user_input is None:
84  if self._entry_data_entry_data[CONF_SOURCE] in (
85  "supervisor",
86  "container",
87  ):
88  data_schema = vol.Schema(
89  {
90  vol.Required(
91  CONF_CHANNEL, default=DEFAULT_CHANNEL.title()
92  ): vol.In(VALID_CHANNELS),
93  }
94  )
95  if self._entry_data_entry_data[CONF_SOURCE] == "supervisor":
96  data_schema = data_schema.extend(
97  {
98  vol.Required(CONF_IMAGE, default=DEFAULT_IMAGE): vol.In(
99  VALID_IMAGES
100  ),
101  vol.Required(CONF_BOARD, default=DEFAULT_BOARD): vol.In(
102  VALID_BOARDS
103  ),
104  }
105  )
106  else:
107  data_schema = data_schema.extend(
108  {
109  vol.Required(CONF_IMAGE, default=DEFAULT_IMAGE): vol.In(
110  VALID_CONTAINER_IMAGES
111  )
112  }
113  )
114  else:
115  data_schema = vol.Schema({vol.Required(CONF_BETA, default=False): bool})
116 
117  return self.async_show_formasync_show_formasync_show_form(
118  step_id=STEP_VERSION_SOURCE,
119  data_schema=data_schema,
120  description_placeholders={
121  ATTR_VERSION_SOURCE: self._entry_data_entry_data[CONF_VERSION_SOURCE]
122  },
123  )
124  self._entry_data_entry_data.update(user_input)
125  self._entry_data_entry_data[CONF_CHANNEL] = self._entry_data_entry_data[CONF_CHANNEL].lower()
126 
127  return self.async_create_entryasync_create_entryasync_create_entry(
128  title=self._config_entry_name_config_entry_name, data=self._entry_data_entry_data
129  )
130 
131  @property
132  def _config_entry_name(self) -> str:
133  """Return the name of the config entry."""
134  if self._entry_data_entry_data[CONF_SOURCE] == "local":
135  return DEFAULT_NAME_CURRENT
136 
137  name = self._entry_data_entry_data[CONF_VERSION_SOURCE]
138 
139  if (channel := self._entry_data_entry_data[CONF_CHANNEL]) != DEFAULT_CHANNEL:
140  return f"{name} {channel.title()}"
141 
142  return name
ConfigFlowResult async_step_version_source(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:81
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:48
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)
bool show_advanced_options(self)
_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