Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow to configure the Tailscale integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from typing import Any
7 
8 from tailscale import Tailscale, TailscaleAuthenticationError, TailscaleError
9 import voluptuous as vol
10 
11 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
12 from homeassistant.const import CONF_API_KEY
13 from homeassistant.core import HomeAssistant
14 from homeassistant.helpers.aiohttp_client import async_get_clientsession
15 
16 from .const import CONF_TAILNET, DOMAIN
17 
18 AUTHKEYS_URL = "https://login.tailscale.com/admin/settings/keys"
19 
20 
21 async def validate_input(hass: HomeAssistant, *, tailnet: str, api_key: str) -> None:
22  """Try using the give tailnet & api key against the Tailscale API."""
23  session = async_get_clientsession(hass)
24  tailscale = Tailscale(
25  session=session,
26  api_key=api_key,
27  tailnet=tailnet,
28  )
29  await tailscale.devices()
30 
31 
32 class TailscaleFlowHandler(ConfigFlow, domain=DOMAIN):
33  """Config flow for Tailscale."""
34 
35  VERSION = 1
36 
37  async def async_step_user(
38  self, user_input: dict[str, Any] | None = None
39  ) -> ConfigFlowResult:
40  """Handle a flow initialized by the user."""
41  errors = {}
42 
43  if user_input is not None:
44  try:
45  await validate_input(
46  self.hass,
47  tailnet=user_input[CONF_TAILNET],
48  api_key=user_input[CONF_API_KEY],
49  )
50  except TailscaleAuthenticationError:
51  errors["base"] = "invalid_auth"
52  except TailscaleError:
53  errors["base"] = "cannot_connect"
54  else:
55  await self.async_set_unique_idasync_set_unique_id(user_input[CONF_TAILNET])
56  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
57  return self.async_create_entryasync_create_entryasync_create_entry(
58  title=user_input[CONF_TAILNET],
59  data={
60  CONF_TAILNET: user_input[CONF_TAILNET],
61  CONF_API_KEY: user_input[CONF_API_KEY],
62  },
63  )
64  else:
65  user_input = {}
66 
67  return self.async_show_formasync_show_formasync_show_form(
68  step_id="user",
69  description_placeholders={"authkeys_url": AUTHKEYS_URL},
70  data_schema=vol.Schema(
71  {
72  vol.Required(
73  CONF_TAILNET, default=user_input.get(CONF_TAILNET, "")
74  ): str,
75  vol.Required(
76  CONF_API_KEY, default=user_input.get(CONF_API_KEY, "")
77  ): str,
78  }
79  ),
80  errors=errors,
81  )
82 
83  async def async_step_reauth(
84  self, entry_data: Mapping[str, Any]
85  ) -> ConfigFlowResult:
86  """Handle initiation of re-authentication with Tailscale."""
87  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
88 
90  self, user_input: dict[str, Any] | None = None
91  ) -> ConfigFlowResult:
92  """Handle re-authentication with Tailscale."""
93  errors = {}
94 
95  if user_input is not None:
96  reauth_entry = self._get_reauth_entry_get_reauth_entry()
97  try:
98  await validate_input(
99  self.hass,
100  tailnet=reauth_entry.data[CONF_TAILNET],
101  api_key=user_input[CONF_API_KEY],
102  )
103  except TailscaleAuthenticationError:
104  errors["base"] = "invalid_auth"
105  except TailscaleError:
106  errors["base"] = "cannot_connect"
107  else:
108  return self.async_update_reload_and_abortasync_update_reload_and_abort(
109  reauth_entry,
110  data_updates={CONF_API_KEY: user_input[CONF_API_KEY]},
111  )
112 
113  return self.async_show_formasync_show_formasync_show_form(
114  step_id="reauth_confirm",
115  description_placeholders={"authkeys_url": AUTHKEYS_URL},
116  data_schema=vol.Schema({vol.Required(CONF_API_KEY): str}),
117  errors=errors,
118  )
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:85
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:39
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:91
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_update_reload_and_abort(self, ConfigEntry entry, *str|None|UndefinedType unique_id=UNDEFINED, str|UndefinedType title=UNDEFINED, Mapping[str, Any]|UndefinedType data=UNDEFINED, Mapping[str, Any]|UndefinedType data_updates=UNDEFINED, Mapping[str, Any]|UndefinedType options=UNDEFINED, str|UndefinedType reason=UNDEFINED, bool reload_even_if_entry_is_unchanged=True)
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)
None validate_input(HomeAssistant hass, *str tailnet, str api_key)
Definition: config_flow.py:21
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)