mirror of
https://github.com/home-assistant/core.git
synced 2025-07-30 02:38:10 +02:00
Use reauth helpers in husqvarna_automower (#128631)
This commit is contained in:
@ -6,7 +6,7 @@ from typing import Any
|
||||
|
||||
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
|
||||
|
||||
@ -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,
|
||||
@ -61,12 +63,8 @@ class HusqvarnaConfigFlowHandler(
|
||||
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(
|
||||
@ -74,10 +72,9 @@ class HusqvarnaConfigFlowHandler(
|
||||
) -> 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()
|
||||
|
||||
@ -85,9 +82,9 @@ class HusqvarnaConfigFlowHandler(
|
||||
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",
|
||||
|
Reference in New Issue
Block a user