Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow to add the integration via the UI."""
2 
3 from collections.abc import Mapping
4 import logging
5 from typing import Any
6 
7 from aioautomower.session import AutomowerSession
8 from aioautomower.utils import structure_token
9 
10 from homeassistant.config_entries import SOURCE_REAUTH, ConfigFlowResult
11 from homeassistant.const import CONF_ACCESS_TOKEN, CONF_NAME, CONF_TOKEN
12 from homeassistant.helpers import aiohttp_client, config_entry_oauth2_flow
13 from homeassistant.util import dt as dt_util
14 
15 from .api import AsyncConfigFlowAuth
16 from .const import DOMAIN, NAME
17 
18 _LOGGER = logging.getLogger(__name__)
19 
20 CONF_USER_ID = "user_id"
21 HUSQVARNA_DEV_PORTAL_URL = "https://developer.husqvarnagroup.cloud/applications"
22 
23 
25  config_entry_oauth2_flow.AbstractOAuth2FlowHandler,
26  domain=DOMAIN,
27 ):
28  """Handle a config flow."""
29 
30  VERSION = 1
31  DOMAIN = DOMAIN
32 
33  async def async_oauth_create_entry(self, data: dict[str, Any]) -> ConfigFlowResult:
34  """Create an entry for the flow."""
35  token = data[CONF_TOKEN]
36  if "amc:api" not in token["scope"] and self.sourcesource != SOURCE_REAUTH:
37  return self.async_abort(reason="missing_amc_scope")
38  user_id = token[CONF_USER_ID]
39  await self.async_set_unique_id(user_id)
40 
41  if self.sourcesource == SOURCE_REAUTH:
42  reauth_entry = self._get_reauth_entry()
43  if "amc:api" not in token["scope"]:
44  return self.async_update_reload_and_abort(
45  reauth_entry, data=data, reason="missing_amc_scope"
46  )
47  self._abort_if_unique_id_mismatch(reason="wrong_account")
48  return self.async_update_reload_and_abort(reauth_entry, data=data)
49 
50  self._abort_if_unique_id_configured()
51 
52  websession = aiohttp_client.async_get_clientsession(self.hass)
53  tz = await dt_util.async_get_time_zone(str(dt_util.DEFAULT_TIME_ZONE))
54  automower_api = AutomowerSession(AsyncConfigFlowAuth(websession, token), tz)
55  try:
56  data = await automower_api.get_status()
57  except Exception: # noqa: BLE001
58  return self.async_abort(reason="unknown")
59  if data == {}:
60  return self.async_abort(reason="no_mower_connected")
61 
62  structured_token = structure_token(token[CONF_ACCESS_TOKEN])
63  first_name = structured_token.user.first_name
64  last_name = structured_token.user.last_name
65 
66  return self.async_create_entry(
67  title=f"{NAME} of {first_name} {last_name}",
68  data=data,
69  )
70 
71  @property
72  def logger(self) -> logging.Logger:
73  """Return logger."""
74  return logging.getLogger(__name__)
75 
76  async def async_step_reauth(
77  self, entry_data: Mapping[str, Any]
78  ) -> ConfigFlowResult:
79  """Perform reauth upon an API authentication error."""
80  if "amc:api" not in entry_data["token"]["scope"]:
81  return await self.async_step_missing_scopeasync_step_missing_scope()
82  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
83 
85  self, user_input: dict[str, Any] | None = None
86  ) -> ConfigFlowResult:
87  """Confirm reauth dialog."""
88  if user_input is None:
89  return self.async_show_form(
90  step_id="reauth_confirm",
91  description_placeholders={CONF_NAME: self._get_reauth_entry().title},
92  )
93  return await self.async_step_user()
94 
96  self, user_input: dict[str, Any] | None = None
97  ) -> ConfigFlowResult:
98  """Confirm reauth for missing scope."""
99  if user_input is None and self.sourcesource == SOURCE_REAUTH:
100  token_structured = structure_token(
101  self._get_reauth_entry().data["token"]["access_token"]
102  )
103  return self.async_show_form(
104  step_id="missing_scope",
105  description_placeholders={
106  "application_url": f"{HUSQVARNA_DEV_PORTAL_URL}/{token_structured.client_id}"
107  },
108  )
109  return await self.async_step_user()
ConfigFlowResult async_step_missing_scope(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:97
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:86
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:78