mirror of
https://github.com/home-assistant/core.git
synced 2025-07-31 17:18:23 +00:00
Use reauth helpers in husqvarna_automower (#128631)
This commit is contained in:
parent
b3eca73e48
commit
1d5821abca
@ -6,7 +6,7 @@ from typing import Any
|
|||||||
|
|
||||||
from aioautomower.utils import structure_token
|
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.const import CONF_ACCESS_TOKEN, CONF_NAME, CONF_TOKEN
|
||||||
from homeassistant.helpers import config_entry_oauth2_flow
|
from homeassistant.helpers import config_entry_oauth2_flow
|
||||||
|
|
||||||
@ -26,27 +26,29 @@ class HusqvarnaConfigFlowHandler(
|
|||||||
|
|
||||||
VERSION = 1
|
VERSION = 1
|
||||||
DOMAIN = DOMAIN
|
DOMAIN = DOMAIN
|
||||||
reauth_entry: ConfigEntry | None = None
|
|
||||||
|
|
||||||
async def async_oauth_create_entry(self, data: dict[str, Any]) -> ConfigFlowResult:
|
async def async_oauth_create_entry(self, data: dict[str, Any]) -> ConfigFlowResult:
|
||||||
"""Create an entry for the flow."""
|
"""Create an entry for the flow."""
|
||||||
token = data[CONF_TOKEN]
|
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")
|
return self.async_abort(reason="missing_amc_scope")
|
||||||
user_id = token[CONF_USER_ID]
|
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"]:
|
if "amc:api" not in token["scope"]:
|
||||||
return self.async_update_reload_and_abort(
|
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:
|
self._abort_if_unique_id_mismatch(reason="wrong_account")
|
||||||
return self.async_abort(reason="wrong_account")
|
return self.async_update_reload_and_abort(reauth_entry, data=data)
|
||||||
return self.async_update_reload_and_abort(self.reauth_entry, data=data)
|
|
||||||
|
self._abort_if_unique_id_configured()
|
||||||
|
|
||||||
structured_token = structure_token(token[CONF_ACCESS_TOKEN])
|
structured_token = structure_token(token[CONF_ACCESS_TOKEN])
|
||||||
first_name = structured_token.user.first_name
|
first_name = structured_token.user.first_name
|
||||||
last_name = structured_token.user.last_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(
|
return self.async_create_entry(
|
||||||
title=f"{NAME} of {first_name} {last_name}",
|
title=f"{NAME} of {first_name} {last_name}",
|
||||||
data=data,
|
data=data,
|
||||||
@ -61,11 +63,7 @@ class HusqvarnaConfigFlowHandler(
|
|||||||
self, entry_data: Mapping[str, Any]
|
self, entry_data: Mapping[str, Any]
|
||||||
) -> ConfigFlowResult:
|
) -> ConfigFlowResult:
|
||||||
"""Perform reauth upon an API authentication error."""
|
"""Perform reauth upon an API authentication error."""
|
||||||
self.reauth_entry = self.hass.config_entries.async_get_entry(
|
if "amc:api" not in entry_data["token"]["scope"]:
|
||||||
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()
|
return await self.async_step_missing_scope()
|
||||||
return await self.async_step_reauth_confirm()
|
return await self.async_step_reauth_confirm()
|
||||||
|
|
||||||
@ -74,10 +72,9 @@ class HusqvarnaConfigFlowHandler(
|
|||||||
) -> ConfigFlowResult:
|
) -> ConfigFlowResult:
|
||||||
"""Confirm reauth dialog."""
|
"""Confirm reauth dialog."""
|
||||||
if user_input is None:
|
if user_input is None:
|
||||||
assert self.reauth_entry
|
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
step_id="reauth_confirm",
|
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()
|
return await self.async_step_user()
|
||||||
|
|
||||||
@ -85,9 +82,9 @@ class HusqvarnaConfigFlowHandler(
|
|||||||
self, user_input: dict[str, Any] | None = None
|
self, user_input: dict[str, Any] | None = None
|
||||||
) -> ConfigFlowResult:
|
) -> ConfigFlowResult:
|
||||||
"""Confirm reauth for missing scope."""
|
"""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(
|
token_structured = structure_token(
|
||||||
self.reauth_entry.data["token"]["access_token"]
|
self._get_reauth_entry().data["token"]["access_token"]
|
||||||
)
|
)
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
step_id="missing_scope",
|
step_id="missing_scope",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user