Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use reauth helpers in ring #128663

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 8 additions & 16 deletions homeassistant/components/ring/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import voluptuous as vol

from homeassistant.components import dhcp
from homeassistant.config_entries import ConfigEntry, ConfigFlow, ConfigFlowResult
from homeassistant.config_entries import SOURCE_REAUTH, ConfigFlow, ConfigFlowResult
from homeassistant.const import (
CONF_DEVICE_ID,
CONF_NAME,
Expand Down Expand Up @@ -71,7 +71,6 @@ class RingConfigFlow(ConfigFlow, domain=DOMAIN):

user_pass: dict[str, Any] = {}
hardware_id: str | None = None
reauth_entry: ConfigEntry | None = None

async def async_step_dhcp(
self, discovery_info: dhcp.DhcpServiceInfo
Expand Down Expand Up @@ -132,7 +131,7 @@ async def async_step_2fa(
) -> ConfigFlowResult:
"""Handle 2fa step."""
if user_input:
if self.reauth_entry:
if self.source == SOURCE_REAUTH:
return await self.async_step_reauth_confirm(
{**self.user_pass, **user_input}
)
Expand All @@ -148,24 +147,21 @@ async def async_step_reauth(
self, entry_data: Mapping[str, Any]
) -> ConfigFlowResult:
"""Handle 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(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Dialog that informs the user that reauth is required."""
errors: dict[str, str] = {}
assert self.reauth_entry is not None

reauth_entry = self._get_reauth_entry()
if user_input:
user_input[CONF_USERNAME] = self.reauth_entry.data[CONF_USERNAME]
user_input[CONF_USERNAME] = reauth_entry.data[CONF_USERNAME]
# Reauth will use the same hardware id and re-authorise an existing
# authorised device.
if not self.hardware_id:
self.hardware_id = self.reauth_entry.data[CONF_DEVICE_ID]
self.hardware_id = reauth_entry.data[CONF_DEVICE_ID]
assert self.hardware_id
try:
token = await validate_input(self.hass, self.hardware_id, user_input)
Expand All @@ -183,19 +179,15 @@ async def async_step_reauth_confirm(
CONF_TOKEN: token,
CONF_DEVICE_ID: self.hardware_id,
}
self.hass.config_entries.async_update_entry(
self.reauth_entry, data=data
)
await self.hass.config_entries.async_reload(self.reauth_entry.entry_id)
return self.async_abort(reason="reauth_successful")
return self.async_update_reload_and_abort(reauth_entry, data=data)

return self.async_show_form(
step_id="reauth_confirm",
data_schema=STEP_REAUTH_DATA_SCHEMA,
errors=errors,
description_placeholders={
CONF_USERNAME: self.reauth_entry.data[CONF_USERNAME],
CONF_NAME: self.reauth_entry.data[CONF_USERNAME],
CONF_USERNAME: reauth_entry.data[CONF_USERNAME],
CONF_NAME: reauth_entry.data[CONF_USERNAME],
},
)

Expand Down