Implement reauth_confirm in icloud (#77530)

This commit is contained in:
epenet 2022-08-31 06:07:32 +02:00 committed by GitHub
parent e192c99d2f
commit 3df2ec1ed6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 33 deletions

View File

@ -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}

View File

@ -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."""

View File

@ -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": {

View File

@ -11,7 +11,7 @@
"validate_verification_code": "Failed to verify your verification code, try again"
},
"step": {
"reauth": {
"reauth_confirm": {
"data": {
"password": "Password"
},

View File

@ -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