Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Neato Botvac."""
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 NEATO_DOMAIN
13 
14 
16  config_entry_oauth2_flow.AbstractOAuth2FlowHandler, domain=NEATO_DOMAIN
17 ):
18  """Config flow to handle Neato Botvac OAuth2 authentication."""
19 
20  DOMAIN = NEATO_DOMAIN
21 
22  @property
23  def logger(self) -> logging.Logger:
24  """Return logger."""
25  return logging.getLogger(__name__)
26 
27  async def async_step_user(
28  self, user_input: dict[str, Any] | None = None
29  ) -> ConfigFlowResult:
30  """Create an entry for the flow."""
31  current_entries = self._async_current_entries()
32  if self.sourcesource != SOURCE_REAUTH and current_entries:
33  # Already configured
34  return self.async_abort(reason="already_configured")
35 
36  return await super().async_step_user(user_input=user_input)
37 
38  async def async_step_reauth(
39  self, entry_data: Mapping[str, Any]
40  ) -> ConfigFlowResult:
41  """Perform reauth upon migration of old entries."""
42  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
43 
45  self, user_input: dict[str, Any] | None = None
46  ) -> ConfigFlowResult:
47  """Confirm reauth upon migration of old entries."""
48  if user_input is None:
49  return self.async_show_form(step_id="reauth_confirm")
50  return await self.async_step_userasync_step_user()
51 
52  async def async_oauth_create_entry(self, data: dict[str, Any]) -> ConfigFlowResult:
53  """Create an entry for the flow. Update an entry if one already exist."""
54  current_entries = self._async_current_entries()
55  if self.sourcesource == SOURCE_REAUTH and current_entries:
56  # Update entry
57  self.hass.config_entries.async_update_entry(
58  current_entries[0], title=self.flow_impl.name, data=data
59  )
60  self.hass.async_create_task(
61  self.hass.config_entries.async_reload(current_entries[0].entry_id)
62  )
63  return self.async_abort(reason="reauth_successful")
64  return self.async_create_entry(title=self.flow_impl.name, data=data)
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:46
ConfigFlowResult async_oauth_create_entry(self, dict[str, Any] data)
Definition: config_flow.py:52
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:29
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:40