Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Ping (ICMP) integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 import voluptuous as vol
9 
11  CONF_CONSIDER_HOME,
12  DEFAULT_CONSIDER_HOME,
13 )
14 from homeassistant.config_entries import (
15  ConfigEntry,
16  ConfigFlow,
17  ConfigFlowResult,
18  OptionsFlow,
19 )
20 from homeassistant.const import CONF_HOST
21 from homeassistant.core import callback
22 from homeassistant.helpers import selector
23 from homeassistant.util.network import is_ip_address
24 
25 from .const import CONF_PING_COUNT, DEFAULT_PING_COUNT, DOMAIN
26 
27 _LOGGER = logging.getLogger(__name__)
28 
29 
30 def _clean_user_input(user_input: dict[str, Any]) -> dict[str, Any]:
31  """Clean up the user input."""
32  user_input[CONF_HOST] = user_input[CONF_HOST].strip()
33  return user_input
34 
35 
36 class PingConfigFlow(ConfigFlow, domain=DOMAIN):
37  """Handle a config flow for Ping."""
38 
39  VERSION = 1
40 
41  async def async_step_user(
42  self, user_input: dict[str, Any] | None = None
43  ) -> ConfigFlowResult:
44  """Handle the initial step."""
45  if user_input is None:
46  return self.async_show_formasync_show_formasync_show_form(
47  step_id="user",
48  data_schema=vol.Schema(
49  {
50  vol.Required(CONF_HOST): str,
51  }
52  ),
53  )
54 
55  user_input = _clean_user_input(user_input)
56  if not is_ip_address(user_input[CONF_HOST]):
57  self.async_abortasync_abortasync_abort(reason="invalid_ip_address")
58 
59  self._async_abort_entries_match_async_abort_entries_match({CONF_HOST: user_input[CONF_HOST]})
60  return self.async_create_entryasync_create_entryasync_create_entry(
61  title=user_input[CONF_HOST],
62  data={},
63  options={
64  **user_input,
65  CONF_PING_COUNT: DEFAULT_PING_COUNT,
66  CONF_CONSIDER_HOME: DEFAULT_CONSIDER_HOME.seconds,
67  },
68  )
69 
70  @staticmethod
71  @callback
73  config_entry: ConfigEntry,
74  ) -> OptionsFlow:
75  """Create the options flow."""
76  return OptionsFlowHandler()
77 
78 
80  """Handle an options flow for Ping."""
81 
82  async def async_step_init(
83  self, user_input: dict[str, Any] | None = None
84  ) -> ConfigFlowResult:
85  """Manage the options."""
86  if user_input is not None:
87  return self.async_create_entryasync_create_entry(title="", data=_clean_user_input(user_input))
88 
89  return self.async_show_formasync_show_form(
90  step_id="init",
91  data_schema=vol.Schema(
92  {
93  vol.Required(
94  CONF_HOST, default=self.config_entryconfig_entryconfig_entry.options[CONF_HOST]
95  ): str,
96  vol.Optional(
97  CONF_PING_COUNT,
98  default=self.config_entryconfig_entryconfig_entry.options[CONF_PING_COUNT],
99  ): selector.NumberSelector(
100  selector.NumberSelectorConfig(
101  min=1, max=100, mode=selector.NumberSelectorMode.BOX
102  )
103  ),
104  vol.Optional(
105  CONF_CONSIDER_HOME,
106  default=self.config_entryconfig_entryconfig_entry.options.get(
107  CONF_CONSIDER_HOME, DEFAULT_CONSIDER_HOME.seconds
108  ),
109  ): int,
110  }
111  ),
112  )
ConfigFlowResult async_step_init(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:84
OptionsFlow async_get_options_flow(ConfigEntry config_entry)
Definition: config_flow.py:74
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:43
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_abort(self, *str reason, Mapping[str, str]|None description_placeholders=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)
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)
dict[str, Any] _clean_user_input(dict[str, Any] user_input)
Definition: config_flow.py:30
bool is_ip_address(str address)
Definition: network.py:63