Use new reauth helpers in weheat (#128824)

This commit is contained in:
epenet 2024-10-20 15:30:49 +02:00 committed by GitHub
parent c8556f69e7
commit 4fc872a4cb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -6,7 +6,7 @@ from typing import Any
from weheat.abstractions.user import get_user_id_from_token from weheat.abstractions.user import get_user_id_from_token
from homeassistant.config_entries import ConfigEntry, ConfigFlowResult from homeassistant.config_entries import SOURCE_REAUTH, ConfigFlowResult
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_TOKEN from homeassistant.const import CONF_ACCESS_TOKEN, CONF_TOKEN
from homeassistant.helpers.config_entry_oauth2_flow import AbstractOAuth2FlowHandler from homeassistant.helpers.config_entry_oauth2_flow import AbstractOAuth2FlowHandler
@ -18,8 +18,6 @@ class OAuth2FlowHandler(AbstractOAuth2FlowHandler, domain=DOMAIN):
DOMAIN = DOMAIN DOMAIN = DOMAIN
reauth_entry: ConfigEntry | None = None
@property @property
def logger(self) -> logging.Logger: def logger(self) -> logging.Logger:
"""Return logger.""" """Return logger."""
@ -38,28 +36,21 @@ class OAuth2FlowHandler(AbstractOAuth2FlowHandler, domain=DOMAIN):
user_id = await get_user_id_from_token( user_id = await get_user_id_from_token(
API_URL, data[CONF_TOKEN][CONF_ACCESS_TOKEN] API_URL, data[CONF_TOKEN][CONF_ACCESS_TOKEN]
) )
if not self.reauth_entry: await self.async_set_unique_id(user_id)
await self.async_set_unique_id(user_id) if self.source != SOURCE_REAUTH:
self._abort_if_unique_id_configured() self._abort_if_unique_id_configured()
return self.async_create_entry(title=ENTRY_TITLE, data=data) return self.async_create_entry(title=ENTRY_TITLE, data=data)
if self.reauth_entry.unique_id == user_id: self._abort_if_unique_id_mismatch(reason="wrong_account")
return self.async_update_reload_and_abort( return self.async_update_reload_and_abort(
self.reauth_entry, self._get_reauth_entry(), data_updates=data
unique_id=user_id, )
data={**self.reauth_entry.data, **data},
)
return self.async_abort(reason="wrong_account")
async def async_step_reauth( async def async_step_reauth(
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(
self.context["entry_id"]
)
return await self.async_step_reauth_confirm() return await self.async_step_reauth_confirm()
async def async_step_reauth_confirm( async def async_step_reauth_confirm(