Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Geocaching."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 import logging
7 from typing import Any
8 
9 from geocachingapi.geocachingapi import GeocachingApi
10 
11 from homeassistant.config_entries import ConfigFlowResult
12 from homeassistant.helpers.aiohttp_client import async_get_clientsession
13 from homeassistant.helpers.config_entry_oauth2_flow import AbstractOAuth2FlowHandler
14 
15 from .const import DOMAIN, ENVIRONMENT
16 
17 
19  """Config flow to handle Geocaching OAuth2 authentication."""
20 
21  DOMAIN = DOMAIN
22  VERSION = 1
23 
24  @property
25  def logger(self) -> logging.Logger:
26  """Return logger."""
27  return logging.getLogger(__name__)
28 
29  async def async_step_reauth(
30  self, entry_data: Mapping[str, Any]
31  ) -> ConfigFlowResult:
32  """Perform reauth upon an API authentication error."""
33  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
34 
36  self, user_input: dict[str, Any] | None = None
37  ) -> ConfigFlowResult:
38  """Dialog that informs the user that reauth is required."""
39  if user_input is None:
40  return self.async_show_formasync_show_formasync_show_form(step_id="reauth_confirm")
41  return await self.async_step_userasync_step_userasync_step_user()
42 
43  async def async_oauth_create_entry(self, data: dict[str, Any]) -> ConfigFlowResult:
44  """Create an oauth config entry or update existing entry for reauth."""
45  api = GeocachingApi(
46  environment=ENVIRONMENT,
47  token=data["token"]["access_token"],
48  session=async_get_clientsession(self.hass),
49  )
50  status = await api.update()
51  if not status.user or not status.user.username:
52  return self.async_abortasync_abortasync_abort(reason="oauth_error")
53 
54  if existing_entry := await self.async_set_unique_idasync_set_unique_id(
55  status.user.username.lower()
56  ):
57  self.hass.config_entries.async_update_entry(existing_entry, data=data)
58  await self.hass.config_entries.async_reload(existing_entry.entry_id)
59  return self.async_abortasync_abortasync_abort(reason="reauth_successful")
60  return self.async_create_entryasync_create_entryasync_create_entry(title=status.user.username, data=data)
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:31
ConfigFlowResult async_oauth_create_entry(self, dict[str, Any] data)
Definition: config_flow.py:43
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:37
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_abort(self, *str reason, Mapping[str, str]|None description_placeholders=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)
_FlowResultT async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)
config_entries.ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=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)