Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Speedtest.net."""
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, OptionsFlow
10 from homeassistant.core import callback
11 
12 from . import SpeedTestConfigEntry
13 from .const import (
14  CONF_SERVER_ID,
15  CONF_SERVER_NAME,
16  DEFAULT_NAME,
17  DEFAULT_SERVER,
18  DOMAIN,
19 )
20 
21 
22 class SpeedTestFlowHandler(ConfigFlow, domain=DOMAIN):
23  """Handle Speedtest.net config flow."""
24 
25  VERSION = 1
26 
27  @staticmethod
28  @callback
30  config_entry: SpeedTestConfigEntry,
31  ) -> SpeedTestOptionsFlowHandler:
32  """Get the options flow for this handler."""
34 
35  async def async_step_user(
36  self, user_input: dict[str, Any] | None = None
37  ) -> ConfigFlowResult:
38  """Handle a flow initialized by the user."""
39  if self._async_current_entries_async_current_entries():
40  return self.async_abortasync_abortasync_abort(reason="single_instance_allowed")
41 
42  if user_input is None:
43  return self.async_show_formasync_show_formasync_show_form(step_id="user")
44 
45  return self.async_create_entryasync_create_entryasync_create_entry(title=DEFAULT_NAME, data=user_input)
46 
47 
49  """Handle SpeedTest options."""
50 
51  def __init__(self) -> None:
52  """Initialize options flow."""
53  self._servers_servers: dict = {}
54 
55  async def async_step_init(
56  self, user_input: dict[str, Any] | None = None
57  ) -> ConfigFlowResult:
58  """Manage the options."""
59  errors: dict[str, str] = {}
60 
61  if user_input is not None:
62  server_name = user_input[CONF_SERVER_NAME]
63  if server_name != "*Auto Detect":
64  server_id = self._servers_servers[server_name]["id"]
65  user_input[CONF_SERVER_ID] = server_id
66  else:
67  user_input[CONF_SERVER_ID] = None
68 
69  return self.async_create_entryasync_create_entry(title="", data=user_input)
70 
71  self._servers_servers = self.config_entryconfig_entryconfig_entry.runtime_data.servers
72 
73  options = {
74  vol.Optional(
75  CONF_SERVER_NAME,
76  default=self.config_entryconfig_entryconfig_entry.options.get(CONF_SERVER_NAME, DEFAULT_SERVER),
77  ): vol.In(self._servers_servers.keys()),
78  }
79 
80  return self.async_show_formasync_show_form(
81  step_id="init", data_schema=vol.Schema(options), errors=errors
82  )
SpeedTestOptionsFlowHandler async_get_options_flow(SpeedTestConfigEntry config_entry)
Definition: config_flow.py:31
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:37
ConfigFlowResult async_step_init(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:57
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)
list[ConfigEntry] _async_current_entries(self, bool|None include_ignore=None)
ConfigFlowResult async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=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)
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)
_FlowResultT async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)