Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow to configure the AdGuard Home integration."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from adguardhome import AdGuardHome, AdGuardHomeConnectionError
8 import voluptuous as vol
9 
10 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
11 from homeassistant.const import (
12  CONF_HOST,
13  CONF_PASSWORD,
14  CONF_PORT,
15  CONF_SSL,
16  CONF_USERNAME,
17  CONF_VERIFY_SSL,
18 )
19 from homeassistant.helpers.aiohttp_client import async_get_clientsession
20 from homeassistant.helpers.service_info.hassio import HassioServiceInfo
21 
22 from .const import DOMAIN
23 
24 
25 class AdGuardHomeFlowHandler(ConfigFlow, domain=DOMAIN):
26  """Handle a AdGuard Home config flow."""
27 
28  VERSION = 1
29 
30  _hassio_discovery: dict[str, Any] | None = None
31 
32  async def _show_setup_form(
33  self, errors: dict[str, str] | None = None
34  ) -> ConfigFlowResult:
35  """Show the setup form to the user."""
36  return self.async_show_formasync_show_formasync_show_form(
37  step_id="user",
38  data_schema=vol.Schema(
39  {
40  vol.Required(CONF_HOST): str,
41  vol.Required(CONF_PORT, default=3000): vol.Coerce(int),
42  vol.Optional(CONF_USERNAME): str,
43  vol.Optional(CONF_PASSWORD): str,
44  vol.Required(CONF_SSL, default=True): bool,
45  vol.Required(CONF_VERIFY_SSL, default=True): bool,
46  }
47  ),
48  errors=errors or {},
49  )
50 
51  async def _show_hassio_form(
52  self, errors: dict[str, str] | None = None
53  ) -> ConfigFlowResult:
54  """Show the Hass.io confirmation form to the user."""
55  assert self._hassio_discovery_hassio_discovery
56  return self.async_show_formasync_show_formasync_show_form(
57  step_id="hassio_confirm",
58  description_placeholders={"addon": self._hassio_discovery_hassio_discovery["addon"]},
59  errors=errors or {},
60  )
61 
62  async def async_step_user(
63  self, user_input: dict[str, Any] | None = None
64  ) -> ConfigFlowResult:
65  """Handle a flow initiated by the user."""
66  if user_input is None:
67  return await self._show_setup_form_show_setup_form(user_input)
68 
69  self._async_abort_entries_match_async_abort_entries_match(
70  {CONF_HOST: user_input[CONF_HOST], CONF_PORT: user_input[CONF_PORT]}
71  )
72 
73  errors = {}
74 
75  session = async_get_clientsession(self.hass, user_input[CONF_VERIFY_SSL])
76 
77  username: str | None = user_input.get(CONF_USERNAME)
78  password: str | None = user_input.get(CONF_PASSWORD)
79  adguard = AdGuardHome(
80  user_input[CONF_HOST],
81  port=user_input[CONF_PORT],
82  username=username,
83  password=password,
84  tls=user_input[CONF_SSL],
85  verify_ssl=user_input[CONF_VERIFY_SSL],
86  session=session,
87  )
88 
89  try:
90  await adguard.version()
91  except AdGuardHomeConnectionError:
92  errors["base"] = "cannot_connect"
93  return await self._show_setup_form_show_setup_form(errors)
94 
95  return self.async_create_entryasync_create_entryasync_create_entry(
96  title=user_input[CONF_HOST],
97  data={
98  CONF_HOST: user_input[CONF_HOST],
99  CONF_PASSWORD: user_input.get(CONF_PASSWORD),
100  CONF_PORT: user_input[CONF_PORT],
101  CONF_SSL: user_input[CONF_SSL],
102  CONF_USERNAME: user_input.get(CONF_USERNAME),
103  CONF_VERIFY_SSL: user_input[CONF_VERIFY_SSL],
104  },
105  )
106 
107  async def async_step_hassio(
108  self, discovery_info: HassioServiceInfo
109  ) -> ConfigFlowResult:
110  """Prepare configuration for a Hass.io AdGuard Home add-on.
111 
112  This flow is triggered by the discovery component.
113  """
114  await self._async_handle_discovery_without_unique_id_async_handle_discovery_without_unique_id()
115 
116  self._hassio_discovery_hassio_discovery = discovery_info.config
117  return await self.async_step_hassio_confirm()
118 
119  async def async_step_hassio_confirm(
120  self, user_input: dict[str, Any] | None = None
121  ) -> ConfigFlowResult:
122  """Confirm Supervisor discovery."""
123  if user_input is None:
124  return await self._show_hassio_form()
125 
126  errors = {}
127 
128  session = async_get_clientsession(self.hass, False)
129 
130  assert self._hassio_discovery_hassio_discovery
131  adguard = AdGuardHome(
132  self._hassio_discovery_hassio_discovery[CONF_HOST],
133  port=self._hassio_discovery_hassio_discovery[CONF_PORT],
134  tls=False,
135  session=session,
136  )
137 
138  try:
139  await adguard.version()
140  except AdGuardHomeConnectionError:
141  errors["base"] = "cannot_connect"
142  return await self._show_hassio_form(errors)
143 
144  return self.async_create_entryasync_create_entryasync_create_entry(
145  title=self._hassio_discovery_hassio_discovery["addon"],
146  data={
147  CONF_HOST: self._hassio_discovery_hassio_discovery[CONF_HOST],
148  CONF_PORT: self._hassio_discovery_hassio_discovery[CONF_PORT],
149  CONF_PASSWORD: None,
150  CONF_SSL: False,
151  CONF_USERNAME: None,
152  CONF_VERIFY_SSL: True,
153  },
154  )
ConfigFlowResult _show_setup_form(self, dict[str, str]|None errors=None)
Definition: config_flow.py:34
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_hassio(self, HassioServiceInfo discovery_info)
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)
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)