Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for mütesync integration."""
2 
3 from __future__ import annotations
4 
5 import asyncio
6 from typing import Any
7 
8 import aiohttp
9 import mutesync
10 import voluptuous as vol
11 
12 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
13 from homeassistant.core import HomeAssistant
14 from homeassistant.exceptions import HomeAssistantError
15 from homeassistant.helpers.aiohttp_client import async_get_clientsession
16 
17 from .const import DOMAIN
18 
19 STEP_USER_DATA_SCHEMA = vol.Schema({vol.Required("host"): str})
20 
21 
22 async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str, Any]:
23  """Validate the user input allows us to connect.
24 
25  Data has the keys from STEP_USER_DATA_SCHEMA with values provided by the user.
26  """
27  session = async_get_clientsession(hass)
28  try:
29  async with asyncio.timeout(5):
30  token = await mutesync.authenticate(session, data["host"])
31  except aiohttp.ClientResponseError as error:
32  if error.status == 403:
33  raise InvalidAuth from error
34  raise CannotConnect from error
35  except (aiohttp.ClientError, TimeoutError) as error:
36  raise CannotConnect from error
37 
38  return token
39 
40 
41 class MuteSyncConfigFlow(ConfigFlow, domain=DOMAIN):
42  """Handle a config flow for mütesync."""
43 
44  VERSION = 1
45 
46  async def async_step_user(
47  self, user_input: dict[str, Any] | None = None
48  ) -> ConfigFlowResult:
49  """Handle the initial step."""
50  if user_input is None:
51  return self.async_show_formasync_show_formasync_show_form(
52  step_id="user", data_schema=STEP_USER_DATA_SCHEMA
53  )
54 
55  errors = {}
56 
57  try:
58  token = await validate_input(self.hass, user_input)
59  except CannotConnect:
60  errors["base"] = "cannot_connect"
61  except InvalidAuth:
62  errors["base"] = "invalid_auth"
63  except Exception: # noqa: BLE001
64  errors["base"] = "unknown"
65  else:
66  return self.async_create_entryasync_create_entryasync_create_entry(
67  title=user_input["host"],
68  data={"token": token, "host": user_input["host"]},
69  )
70 
71  return self.async_show_formasync_show_formasync_show_form(
72  step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
73  )
74 
75 
77  """Error to indicate we cannot connect."""
78 
79 
81  """Error to indicate there is invalid auth."""
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:48
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)
dict[str, Any] validate_input(HomeAssistant hass, dict[str, Any] data)
Definition: config_flow.py:22
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)