Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for yolink."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 import logging
7 from typing import Any
8 
9 from homeassistant.config_entries import SOURCE_REAUTH, ConfigFlowResult
10 from homeassistant.helpers import config_entry_oauth2_flow
11 
12 from .const import DOMAIN
13 
14 
16  config_entry_oauth2_flow.AbstractOAuth2FlowHandler, domain=DOMAIN
17 ):
18  """Config flow to handle yolink OAuth2 authentication."""
19 
20  DOMAIN = DOMAIN
21 
22  @property
23  def logger(self) -> logging.Logger:
24  """Return logger."""
25  return logging.getLogger(__name__)
26 
27  @property
28  def extra_authorize_data(self) -> dict:
29  """Extra data that needs to be appended to the authorize url."""
30  scopes = ["create"]
31  return {"scope": " ".join(scopes)}
32 
33  async def async_step_reauth(
34  self, entry_data: Mapping[str, Any]
35  ) -> ConfigFlowResult:
36  """Perform reauth upon an API authentication error."""
37  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
38 
39  async def async_step_reauth_confirm(self, user_input=None) -> ConfigFlowResult:
40  """Dialog that informs the user that reauth is required."""
41  if user_input is None:
42  return self.async_show_form(step_id="reauth_confirm")
43  return await self.async_step_userasync_step_user()
44 
45  async def async_oauth_create_entry(self, data: dict) -> ConfigFlowResult:
46  """Create an oauth config entry or update existing entry for reauth."""
47  if self.sourcesource == SOURCE_REAUTH:
48  return self.async_update_reload_and_abort(
49  self._get_reauth_entry(), data_updates=data
50  )
51  return self.async_create_entry(title="YoLink", data=data)
52 
53  async def async_step_user(
54  self, user_input: dict[str, Any] | None = None
55  ) -> ConfigFlowResult:
56  """Handle a flow start."""
57  existing_entry = await self.async_set_unique_id(DOMAIN)
58  if existing_entry and self.sourcesource != SOURCE_REAUTH:
59  return self.async_abort(reason="already_configured")
60  return await super().async_step_user(user_input)