Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config Flow for Teslemetry integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from typing import Any
7 
8 from aiohttp import ClientConnectionError
9 from tesla_fleet_api import Teslemetry
10 from tesla_fleet_api.exceptions import (
11  InvalidToken,
12  SubscriptionRequired,
13  TeslaFleetError,
14 )
15 import voluptuous as vol
16 
17 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
18 from homeassistant.const import CONF_ACCESS_TOKEN
19 from homeassistant.helpers.aiohttp_client import async_get_clientsession
20 
21 from .const import DOMAIN, LOGGER
22 
23 TESLEMETRY_SCHEMA = vol.Schema({vol.Required(CONF_ACCESS_TOKEN): str})
24 DESCRIPTION_PLACEHOLDERS = {
25  "name": "Teslemetry",
26  "short_url": "teslemetry.com/console",
27  "url": "[teslemetry.com/console](https://teslemetry.com/console)",
28 }
29 
30 
31 class TeslemetryConfigFlow(ConfigFlow, domain=DOMAIN):
32  """Config Teslemetry API connection."""
33 
34  VERSION = 1
35  MINOR_VERSION = 2
36 
37  async def async_auth(self, user_input: Mapping[str, Any]) -> dict[str, str]:
38  """Reusable Auth Helper."""
39  teslemetry = Teslemetry(
40  session=async_get_clientsession(self.hass),
41  access_token=user_input[CONF_ACCESS_TOKEN],
42  )
43  try:
44  metadata = await teslemetry.metadata()
45  except InvalidToken:
46  return {CONF_ACCESS_TOKEN: "invalid_access_token"}
47  except SubscriptionRequired:
48  return {"base": "subscription_required"}
49  except ClientConnectionError:
50  return {"base": "cannot_connect"}
51  except TeslaFleetError as e:
52  LOGGER.error(e)
53  return {"base": "unknown"}
54 
55  await self.async_set_unique_idasync_set_unique_id(metadata["uid"])
56  return {}
57 
58  async def async_step_user(
59  self, user_input: Mapping[str, Any] | None = None
60  ) -> ConfigFlowResult:
61  """Get configuration from the user."""
62  errors: dict[str, str] = {}
63  if user_input and not (errors := await self.async_authasync_auth(user_input)):
64  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
65  return self.async_create_entryasync_create_entryasync_create_entry(
66  title="Teslemetry",
67  data=user_input,
68  )
69 
70  return self.async_show_formasync_show_formasync_show_form(
71  step_id="user",
72  data_schema=TESLEMETRY_SCHEMA,
73  description_placeholders=DESCRIPTION_PLACEHOLDERS,
74  errors=errors,
75  )
76 
77  async def async_step_reauth(
78  self, entry_data: Mapping[str, Any]
79  ) -> ConfigFlowResult:
80  """Handle reauth on failure."""
81  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
82 
84  self, user_input: Mapping[str, Any] | None = None
85  ) -> ConfigFlowResult:
86  """Handle users reauth credentials."""
87 
88  errors: dict[str, str] = {}
89 
90  if user_input and not (errors := await self.async_authasync_auth(user_input)):
91  return self.async_update_reload_and_abortasync_update_reload_and_abort(
92  self._get_reauth_entry_get_reauth_entry(),
93  data=user_input,
94  )
95 
96  return self.async_show_formasync_show_formasync_show_form(
97  step_id="reauth_confirm",
98  description_placeholders=DESCRIPTION_PLACEHOLDERS,
99  data_schema=TESLEMETRY_SCHEMA,
100  errors=errors,
101  )
dict[str, str] async_auth(self, Mapping[str, Any] user_input)
Definition: config_flow.py:37
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:79
ConfigFlowResult async_step_user(self, Mapping[str, Any]|None user_input=None)
Definition: config_flow.py:60
ConfigFlowResult async_step_reauth_confirm(self, Mapping[str, Any]|None user_input=None)
Definition: config_flow.py:85
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)
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)