Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for JuiceNet integration."""
2 
3 import logging
4 from typing import Any
5 
6 import aiohttp
7 from pyjuicenet import Api, TokenError
8 import voluptuous as vol
9 
10 from homeassistant import core, exceptions
11 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
12 from homeassistant.const import CONF_ACCESS_TOKEN
13 from homeassistant.helpers.aiohttp_client import async_get_clientsession
14 
15 from .const import DOMAIN
16 
17 _LOGGER = logging.getLogger(__name__)
18 
19 DATA_SCHEMA = vol.Schema({vol.Required(CONF_ACCESS_TOKEN): str})
20 
21 
22 async def validate_input(hass: core.HomeAssistant, data):
23  """Validate the user input allows us to connect.
24 
25  Data has the keys from DATA_SCHEMA with values provided by the user.
26  """
27  session = async_get_clientsession(hass)
28  juicenet = Api(data[CONF_ACCESS_TOKEN], session)
29 
30  try:
31  await juicenet.get_devices()
32  except TokenError as error:
33  _LOGGER.error("Token Error %s", error)
34  raise InvalidAuth from error
35  except aiohttp.ClientError as error:
36  _LOGGER.error("Error connecting %s", error)
37  raise CannotConnect from error
38 
39  # Return info that you want to store in the config entry.
40  return {"title": "JuiceNet"}
41 
42 
43 class JuiceNetConfigFlow(ConfigFlow, domain=DOMAIN):
44  """Handle a config flow for JuiceNet."""
45 
46  VERSION = 1
47 
48  async def async_step_user(
49  self, user_input: dict[str, Any] | None = None
50  ) -> ConfigFlowResult:
51  """Handle the initial step."""
52  errors = {}
53  if user_input is not None:
54  await self.async_set_unique_idasync_set_unique_id(user_input[CONF_ACCESS_TOKEN])
55  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
56 
57  try:
58  info = await validate_input(self.hass, user_input)
59  return self.async_create_entryasync_create_entryasync_create_entry(title=info["title"], data=user_input)
60  except CannotConnect:
61  errors["base"] = "cannot_connect"
62  except InvalidAuth:
63  errors["base"] = "invalid_auth"
64  except Exception:
65  _LOGGER.exception("Unexpected exception")
66  errors["base"] = "unknown"
67 
68  return self.async_show_formasync_show_formasync_show_form(
69  step_id="user", data_schema=DATA_SCHEMA, errors=errors
70  )
71 
72  async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult:
73  """Handle import."""
74  return await self.async_step_userasync_step_userasync_step_user(import_data)
75 
76 
78  """Error to indicate we cannot connect."""
79 
80 
82  """Error to indicate there is invalid auth."""
ConfigFlowResult async_step_import(self, dict[str, Any] import_data)
Definition: config_flow.py:72
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:50
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_step_user(self, dict[str, Any]|None user_input=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(core.HomeAssistant hass, 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)