diff --git a/homeassistant/components/icloud/account.py b/homeassistant/components/icloud/account.py index d0e3b8059a4..fe542f6bf0f 100644 --- a/homeassistant/components/icloud/account.py +++ b/homeassistant/components/icloud/account.py @@ -15,7 +15,7 @@ from pyicloud.exceptions import ( from pyicloud.services.findmyiphone import AppleDevice from homeassistant.components.zone import async_active_zone -from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntry +from homeassistant.config_entries import ConfigEntry from homeassistant.const import ATTR_ATTRIBUTION, CONF_USERNAME from homeassistant.core import CALLBACK_TYPE, HomeAssistant from homeassistant.exceptions import ConfigEntryNotReady @@ -132,7 +132,7 @@ class IcloudAccount: self._config_entry.data[CONF_USERNAME], ) - self._require_reauth() + self._config_entry.async_start_reauth(self.hass) return try: @@ -164,7 +164,7 @@ class IcloudAccount: return if self.api.requires_2fa: - self._require_reauth() + self._config_entry.async_start_reauth(self.hass) return api_devices = {} @@ -230,19 +230,6 @@ class IcloudAccount: utcnow() + timedelta(minutes=self._fetch_interval), ) - def _require_reauth(self): - """Require the user to log in again.""" - self.hass.add_job( - self.hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_REAUTH}, - data={ - **self._config_entry.data, - "unique_id": self._config_entry.unique_id, - }, - ) - ) - def _determine_interval(self) -> int: """Calculate new interval between two API fetch (in minutes).""" intervals = {"default": self._max_interval} diff --git a/homeassistant/components/icloud/config_flow.py b/homeassistant/components/icloud/config_flow.py index a4c29acff75..e82fb230eb1 100644 --- a/homeassistant/components/icloud/config_flow.py +++ b/homeassistant/components/icloud/config_flow.py @@ -1,6 +1,10 @@ """Config flow to configure the iCloud integration.""" +from __future__ import annotations + +from collections.abc import Mapping import logging import os +from typing import Any from pyicloud import PyiCloudService from pyicloud.exceptions import ( @@ -13,6 +17,7 @@ import voluptuous as vol from homeassistant import config_entries from homeassistant.const import CONF_PASSWORD, CONF_USERNAME +from homeassistant.data_entry_flow import FlowResult from homeassistant.helpers.storage import Store from .const import ( @@ -173,22 +178,23 @@ class IcloudFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): return await self._validate_and_create_entry(user_input, "user") - async def async_step_reauth(self, user_input=None): - """Update password for a config entry that can't authenticate.""" + async def async_step_reauth(self, entry_data: Mapping[str, Any]) -> FlowResult: + """Initialise re-authentication.""" # Store existing entry data so it can be used later and set unique ID # so existing config entry can be updated - if not self._existing_entry: - await self.async_set_unique_id(user_input.pop("unique_id")) - self._existing_entry = user_input.copy() - self._description_placeholders = {"username": user_input[CONF_USERNAME]} - user_input = None + await self.async_set_unique_id(self.context["unique_id"]) + self._existing_entry = {**entry_data} + self._description_placeholders = {"username": entry_data[CONF_USERNAME]} + return await self.async_step_reauth_confirm() + async def async_step_reauth_confirm( + self, user_input: dict[str, Any] | None = None + ) -> FlowResult: + """Update password for a config entry that can't authenticate.""" if user_input is None: - return self._show_setup_form(step_id=config_entries.SOURCE_REAUTH) + return self._show_setup_form(step_id="reauth_confirm") - return await self._validate_and_create_entry( - user_input, config_entries.SOURCE_REAUTH - ) + return await self._validate_and_create_entry(user_input, "reauth_confirm") async def async_step_trusted_device(self, user_input=None, errors=None): """We need a trusted device.""" diff --git a/homeassistant/components/icloud/strings.json b/homeassistant/components/icloud/strings.json index 70ab11157d3..385dc74a0ab 100644 --- a/homeassistant/components/icloud/strings.json +++ b/homeassistant/components/icloud/strings.json @@ -10,7 +10,7 @@ "with_family": "With family" } }, - "reauth": { + "reauth_confirm": { "title": "[%key:common::config_flow::title::reauth%]", "description": "Your previously entered password for {username} is no longer working. Update your password to keep using this integration.", "data": { diff --git a/homeassistant/components/icloud/translations/en.json b/homeassistant/components/icloud/translations/en.json index 36e657011e3..65a8892c480 100644 --- a/homeassistant/components/icloud/translations/en.json +++ b/homeassistant/components/icloud/translations/en.json @@ -11,7 +11,7 @@ "validate_verification_code": "Failed to verify your verification code, try again" }, "step": { - "reauth": { + "reauth_confirm": { "data": { "password": "Password" }, diff --git a/tests/components/icloud/test_config_flow.py b/tests/components/icloud/test_config_flow.py index 3c854d468b8..598e44248fa 100644 --- a/tests/components/icloud/test_config_flow.py +++ b/tests/components/icloud/test_config_flow.py @@ -385,8 +385,8 @@ async def test_password_update(hass: HomeAssistant, service_authenticated: Magic result = await hass.config_entries.flow.async_init( DOMAIN, - context={"source": SOURCE_REAUTH}, - data={**MOCK_CONFIG, "unique_id": USERNAME}, + context={"source": SOURCE_REAUTH, "unique_id": config_entry.unique_id}, + data={**MOCK_CONFIG}, ) assert result["type"] == data_entry_flow.FlowResultType.FORM @@ -409,8 +409,8 @@ async def test_password_update_wrong_password(hass: HomeAssistant): result = await hass.config_entries.flow.async_init( DOMAIN, - context={"source": SOURCE_REAUTH}, - data={**MOCK_CONFIG, "unique_id": USERNAME}, + context={"source": SOURCE_REAUTH, "unique_id": config_entry.unique_id}, + data={**MOCK_CONFIG}, ) assert result["type"] == data_entry_flow.FlowResultType.FORM