Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow to configure the Whois integration."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 import voluptuous as vol
8 import whois
9 from whois.exceptions import (
10  FailedParsingWhoisOutput,
11  UnknownDateFormat,
12  UnknownTld,
13  WhoisCommandFailed,
14 )
15 
16 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
17 from homeassistant.const import CONF_DOMAIN
18 
19 from .const import DOMAIN
20 
21 
22 class WhoisFlowHandler(ConfigFlow, domain=DOMAIN):
23  """Config flow for Whois."""
24 
25  VERSION = 1
26 
27  imported_name: str | None = None
28 
29  async def async_step_user(
30  self, user_input: dict[str, Any] | None = None
31  ) -> ConfigFlowResult:
32  """Handle a flow initialized by the user."""
33  errors = {}
34 
35  if user_input is not None:
36  domain = user_input[CONF_DOMAIN].lower()
37 
38  await self.async_set_unique_idasync_set_unique_id(domain)
39  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
40 
41  try:
42  await self.hass.async_add_executor_job(whois.query, domain)
43  except UnknownTld:
44  errors["base"] = "unknown_tld"
45  except WhoisCommandFailed:
46  errors["base"] = "whois_command_failed"
47  except FailedParsingWhoisOutput:
48  errors["base"] = "unexpected_response"
49  except UnknownDateFormat:
50  errors["base"] = "unknown_date_format"
51  else:
52  return self.async_create_entryasync_create_entryasync_create_entry(
53  title=self.imported_name or user_input[CONF_DOMAIN],
54  data={
55  CONF_DOMAIN: domain,
56  },
57  )
58  else:
59  user_input = {}
60 
61  return self.async_show_formasync_show_formasync_show_form(
62  step_id="user",
63  data_schema=vol.Schema(
64  {
65  vol.Required(
66  CONF_DOMAIN, default=user_input.get(CONF_DOMAIN, "")
67  ): str,
68  }
69  ),
70  errors=errors,
71  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:31
None _abort_if_unique_id_configured(self, dict[str, Any]|None updates=None, bool reload_on_update=True, *str error="already_configured")
ConfigEntry|None async_set_unique_id(self, str|None unique_id=None, *bool raise_on_progress=True)
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)
_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)