Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for syncthing integration."""
2 
3 from typing import Any
4 
5 import aiosyncthing
6 import voluptuous as vol
7 
8 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
9 from homeassistant.const import CONF_TOKEN, CONF_URL, CONF_VERIFY_SSL
10 from homeassistant.core import HomeAssistant
11 from homeassistant.exceptions import HomeAssistantError
12 
13 from .const import DEFAULT_URL, DEFAULT_VERIFY_SSL, DOMAIN
14 
15 DATA_SCHEMA = vol.Schema(
16  {
17  vol.Required(CONF_URL, default=DEFAULT_URL): str,
18  vol.Required(CONF_TOKEN): str,
19  vol.Required(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): bool,
20  }
21 )
22 
23 
24 async def validate_input(hass: HomeAssistant, data):
25  """Validate the user input allows us to connect."""
26 
27  try:
28  async with aiosyncthing.Syncthing(
29  data[CONF_TOKEN],
30  url=data[CONF_URL],
31  verify_ssl=data[CONF_VERIFY_SSL],
32  loop=hass.loop,
33  ) as client:
34  server_id = (await client.system.status())["myID"]
35  return {"title": f"{data[CONF_URL]}", "server_id": server_id}
36  except aiosyncthing.exceptions.UnauthorizedError as error:
37  raise InvalidAuth from error
38  except Exception as error:
39  raise CannotConnect from error
40 
41 
42 class SyncThingConfigFlow(ConfigFlow, domain=DOMAIN):
43  """Handle a config flow for syncthing."""
44 
45  VERSION = 1
46 
47  async def async_step_user(
48  self, user_input: dict[str, Any] | None = None
49  ) -> ConfigFlowResult:
50  """Handle the initial step."""
51  errors = {}
52 
53  if user_input is not None:
54  try:
55  info = await validate_input(self.hass, user_input)
56  except CannotConnect:
57  errors["base"] = "cannot_connect"
58  except InvalidAuth:
59  errors[CONF_TOKEN] = "invalid_auth"
60  else:
61  await self.async_set_unique_idasync_set_unique_id(info["server_id"])
62  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
63  return self.async_create_entryasync_create_entryasync_create_entry(title=info["title"], data=user_input)
64 
65  return self.async_show_formasync_show_formasync_show_form(
66  step_id="user", data_schema=DATA_SCHEMA, errors=errors
67  )
68 
69 
71  """Error to indicate we cannot connect."""
72 
73 
75  """Error to indicate there is invalid auth."""
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:49
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)
def validate_input(HomeAssistant hass, data)
Definition: config_flow.py:24