Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow to configure emulated_roku component."""
2 
3 from typing import Any
4 
5 import voluptuous as vol
6 
7 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
8 from homeassistant.const import CONF_NAME
9 from homeassistant.core import HomeAssistant, callback
10 
11 from .const import CONF_LISTEN_PORT, DEFAULT_NAME, DEFAULT_PORT, DOMAIN
12 
13 
14 @callback
15 def configured_servers(hass: HomeAssistant) -> set[str]:
16  """Return a set of the configured servers."""
17  return {
18  entry.data[CONF_NAME] for entry in hass.config_entries.async_entries(DOMAIN)
19  }
20 
21 
22 class EmulatedRokuFlowHandler(ConfigFlow, domain=DOMAIN):
23  """Handle an emulated_roku config flow."""
24 
25  VERSION = 1
26 
27  async def async_step_user(
28  self, user_input: dict[str, Any] | None = None
29  ) -> ConfigFlowResult:
30  """Handle a flow initialized by the user."""
31  errors: dict[str, str] = {}
32 
33  if user_input is not None:
34  self._async_abort_entries_match_async_abort_entries_match({CONF_NAME: user_input[CONF_NAME]})
35  return self.async_create_entryasync_create_entryasync_create_entry(title=user_input[CONF_NAME], data=user_input)
36 
37  servers_num = len(configured_servers(self.hass))
38 
39  if servers_num:
40  default_name = f"{DEFAULT_NAME} {servers_num + 1}"
41  default_port = DEFAULT_PORT + servers_num
42  else:
43  default_name = DEFAULT_NAME
44  default_port = DEFAULT_PORT
45 
46  return self.async_show_formasync_show_formasync_show_form(
47  step_id="user",
48  data_schema=vol.Schema(
49  {
50  vol.Required(CONF_NAME, default=default_name): str,
51  vol.Required(CONF_LISTEN_PORT, default=default_port): vol.Coerce(
52  int
53  ),
54  }
55  ),
56  errors=errors,
57  )
58 
59  async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult:
60  """Handle a flow import."""
61  return await self.async_step_userasync_step_userasync_step_user(import_data)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:29
ConfigFlowResult async_step_import(self, dict[str, Any] import_data)
Definition: config_flow.py:59
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_step_user(self, dict[str, Any]|None user_input=None)
None _async_abort_entries_match(self, dict[str, Any]|None match_dict=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)
set[str] configured_servers(HomeAssistant hass)
Definition: config_flow.py:15