Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config Flow for Tesla Fleet integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 import logging
7 from typing import Any
8 
9 import jwt
10 
11 from homeassistant.config_entries import SOURCE_REAUTH, ConfigFlowResult
12 from homeassistant.helpers import config_entry_oauth2_flow
13 
14 from .const import DOMAIN, LOGGER
15 from .oauth import TeslaSystemImplementation
16 
17 
19  config_entry_oauth2_flow.AbstractOAuth2FlowHandler, domain=DOMAIN
20 ):
21  """Config flow to handle Tesla Fleet API OAuth2 authentication."""
22 
23  DOMAIN = DOMAIN
24 
25  @property
26  def logger(self) -> logging.Logger:
27  """Return logger."""
28  return LOGGER
29 
30  async def async_step_user(
31  self, user_input: dict[str, Any] | None = None
32  ) -> ConfigFlowResult:
33  """Handle a flow start."""
34  self.async_register_implementation(
35  self.hass,
36  TeslaSystemImplementation(self.hass),
37  )
38 
39  return await super().async_step_user()
40 
42  self,
43  data: dict[str, Any],
44  ) -> ConfigFlowResult:
45  """Handle the initial step."""
46 
47  token = jwt.decode(
48  data["token"]["access_token"], options={"verify_signature": False}
49  )
50  uid = token["sub"]
51 
52  await self.async_set_unique_id(uid)
53  if self.sourcesource == SOURCE_REAUTH:
54  self._abort_if_unique_id_mismatch(reason="reauth_account_mismatch")
55  return self.async_update_reload_and_abort(
56  self._get_reauth_entry(), data=data
57  )
58  self._abort_if_unique_id_configured()
59  return self.async_create_entry(title=uid, data=data)
60 
61  async def async_step_reauth(
62  self, entry_data: Mapping[str, Any]
63  ) -> ConfigFlowResult:
64  """Perform reauth upon an API authentication error."""
65  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
66 
68  self, user_input: dict[str, Any] | None = None
69  ) -> ConfigFlowResult:
70  """Confirm reauth dialog."""
71  if user_input is None:
72  return self.async_show_form(
73  step_id="reauth_confirm",
74  description_placeholders={"name": "Tesla Fleet"},
75  )
76  return await self.async_step_userasync_step_user()
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:69
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:63
ConfigFlowResult async_oauth_create_entry(self, dict[str, Any] data)
Definition: config_flow.py:44
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:32