Skip to content

Commit

Permalink
Use new reauth helpers in weheat (home-assistant#128824)
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet authored and Andrew committed Oct 21, 2024
1 parent dd9d0cb commit 2ecf25d
Showing 1 changed file with 7 additions and 16 deletions.
23 changes: 7 additions & 16 deletions homeassistant/components/weheat/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

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.helpers.config_entry_oauth2_flow import AbstractOAuth2FlowHandler

Expand All @@ -18,8 +18,6 @@ class OAuth2FlowHandler(AbstractOAuth2FlowHandler, domain=DOMAIN):

DOMAIN = DOMAIN

reauth_entry: ConfigEntry | None = None

@property
def logger(self) -> logging.Logger:
"""Return logger."""
Expand All @@ -38,28 +36,21 @@ async def async_oauth_create_entry(self, data: dict) -> ConfigFlowResult:
user_id = await get_user_id_from_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()

return self.async_create_entry(title=ENTRY_TITLE, data=data)

if self.reauth_entry.unique_id == user_id:
return self.async_update_reload_and_abort(
self.reauth_entry,
unique_id=user_id,
data={**self.reauth_entry.data, **data},
)

return self.async_abort(reason="wrong_account")
self._abort_if_unique_id_mismatch(reason="wrong_account")
return self.async_update_reload_and_abort(
self._get_reauth_entry(), data_updates=data
)

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"]
)
return await self.async_step_reauth_confirm()

async def async_step_reauth_confirm(
Expand Down

0 comments on commit 2ecf25d

Please sign in to comment.