Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config Flow for Tessie integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from http import HTTPStatus
7 from typing import Any
8 
9 from aiohttp import ClientConnectionError, ClientResponseError
10 from tessie_api import get_state_of_all_vehicles
11 import voluptuous as vol
12 
13 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
14 from homeassistant.const import CONF_ACCESS_TOKEN
15 from homeassistant.helpers.aiohttp_client import async_get_clientsession
16 
17 from .const import DOMAIN
18 
19 TESSIE_SCHEMA = vol.Schema({vol.Required(CONF_ACCESS_TOKEN): str})
20 DESCRIPTION_PLACEHOLDERS = {
21  "name": "Tessie",
22  "url": "[my.tessie.com/settings/api](https://my.tessie.com/settings/api)",
23 }
24 
25 
26 class TessieConfigFlow(ConfigFlow, domain=DOMAIN):
27  """Config Tessie API connection."""
28 
29  VERSION = 1
30 
31  async def async_step_user(
32  self, user_input: Mapping[str, Any] | None = None
33  ) -> ConfigFlowResult:
34  """Get configuration from the user."""
35  errors: dict[str, str] = {}
36  if user_input:
37  self._async_abort_entries_match_async_abort_entries_match(dict(user_input))
38  try:
39  await get_state_of_all_vehicles(
40  session=async_get_clientsession(self.hass),
41  api_key=user_input[CONF_ACCESS_TOKEN],
42  only_active=True,
43  )
44  except ClientResponseError as e:
45  if e.status == HTTPStatus.UNAUTHORIZED:
46  errors[CONF_ACCESS_TOKEN] = "invalid_access_token"
47  else:
48  errors["base"] = "unknown"
49  except ClientConnectionError:
50  errors["base"] = "cannot_connect"
51  else:
52  return self.async_create_entryasync_create_entryasync_create_entry(
53  title="Tessie",
54  data=user_input,
55  )
56 
57  return self.async_show_formasync_show_formasync_show_form(
58  step_id="user",
59  data_schema=TESSIE_SCHEMA,
60  description_placeholders=DESCRIPTION_PLACEHOLDERS,
61  errors=errors,
62  )
63 
64  async def async_step_reauth(
65  self, entry_data: Mapping[str, Any]
66  ) -> ConfigFlowResult:
67  """Handle re-auth."""
68  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
69 
71  self, user_input: Mapping[str, Any] | None = None
72  ) -> ConfigFlowResult:
73  """Get update API Key from the user."""
74  errors: dict[str, str] = {}
75 
76  if user_input:
77  try:
78  await get_state_of_all_vehicles(
79  session=async_get_clientsession(self.hass),
80  api_key=user_input[CONF_ACCESS_TOKEN],
81  )
82  except ClientResponseError as e:
83  if e.status == HTTPStatus.UNAUTHORIZED:
84  errors[CONF_ACCESS_TOKEN] = "invalid_access_token"
85  else:
86  errors["base"] = "unknown"
87  except ClientConnectionError:
88  errors["base"] = "cannot_connect"
89  else:
90  return self.async_update_reload_and_abortasync_update_reload_and_abort(
91  self._get_reauth_entry_get_reauth_entry(), data=user_input
92  )
93 
94  return self.async_show_formasync_show_formasync_show_form(
95  step_id="reauth_confirm",
96  data_schema=TESSIE_SCHEMA,
97  description_placeholders=DESCRIPTION_PLACEHOLDERS,
98  errors=errors,
99  )
ConfigFlowResult async_step_user(self, Mapping[str, Any]|None user_input=None)
Definition: config_flow.py:33
ConfigFlowResult async_step_reauth_confirm(self, Mapping[str, Any]|None user_input=None)
Definition: config_flow.py:72
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:66
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)
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)