Skip to content

Commit

Permalink
Use reauth helpers in husqvarna_automower (home-assistant#128631)
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet authored Oct 18, 2024
1 parent b3eca73 commit 1d5821a
Showing 1 changed file with 17 additions and 20 deletions.
37 changes: 17 additions & 20 deletions homeassistant/components/husqvarna_automower/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from aioautomower.utils import structure_token

from homeassistant.config_entries import ConfigEntry, ConfigFlowResult
from homeassistant.config_entries import SOURCE_REAUTH, ConfigFlowResult
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_NAME, CONF_TOKEN
from homeassistant.helpers import config_entry_oauth2_flow

Expand All @@ -26,27 +26,29 @@ class HusqvarnaConfigFlowHandler(

VERSION = 1
DOMAIN = DOMAIN
reauth_entry: ConfigEntry | None = None

async def async_oauth_create_entry(self, data: dict[str, Any]) -> ConfigFlowResult:
"""Create an entry for the flow."""
token = data[CONF_TOKEN]
if "amc:api" not in token["scope"] and not self.reauth_entry:
if "amc:api" not in token["scope"] and self.source != SOURCE_REAUTH:
return self.async_abort(reason="missing_amc_scope")
user_id = token[CONF_USER_ID]
if self.reauth_entry:
await self.async_set_unique_id(user_id)

if self.source == SOURCE_REAUTH:
reauth_entry = self._get_reauth_entry()
if "amc:api" not in token["scope"]:
return self.async_update_reload_and_abort(
self.reauth_entry, data=data, reason="missing_amc_scope"
reauth_entry, data=data, reason="missing_amc_scope"
)
if self.reauth_entry.unique_id != user_id:
return self.async_abort(reason="wrong_account")
return self.async_update_reload_and_abort(self.reauth_entry, data=data)
self._abort_if_unique_id_mismatch(reason="wrong_account")
return self.async_update_reload_and_abort(reauth_entry, data=data)

self._abort_if_unique_id_configured()

structured_token = structure_token(token[CONF_ACCESS_TOKEN])
first_name = structured_token.user.first_name
last_name = structured_token.user.last_name
await self.async_set_unique_id(user_id)
self._abort_if_unique_id_configured()
return self.async_create_entry(
title=f"{NAME} of {first_name} {last_name}",
data=data,
Expand All @@ -61,33 +63,28 @@ async def async_step_reauth(
self, entry_data: Mapping[str, Any]
) -> ConfigFlowResult:
"""Perform reauth upon an API authentication error."""
self.reauth_entry = self.hass.config_entries.async_get_entry(
self.context["entry_id"]
)
if self.reauth_entry is not None:
if "amc:api" not in self.reauth_entry.data["token"]["scope"]:
return await self.async_step_missing_scope()
if "amc:api" not in entry_data["token"]["scope"]:
return await self.async_step_missing_scope()
return await self.async_step_reauth_confirm()

async def async_step_reauth_confirm(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Confirm reauth dialog."""
if user_input is None:
assert self.reauth_entry
return self.async_show_form(
step_id="reauth_confirm",
description_placeholders={CONF_NAME: self.reauth_entry.title},
description_placeholders={CONF_NAME: self._get_reauth_entry().title},
)
return await self.async_step_user()

async def async_step_missing_scope(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Confirm reauth for missing scope."""
if user_input is None and self.reauth_entry is not None:
if user_input is None and self.source == SOURCE_REAUTH:
token_structured = structure_token(
self.reauth_entry.data["token"]["access_token"]
self._get_reauth_entry().data["token"]["access_token"]
)
return self.async_show_form(
step_id="missing_scope",
Expand Down

0 comments on commit 1d5821a

Please sign in to comment.