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 pyicloud.services.findmyiphone import AppleDevice
from homeassistant.components.zone import async_active_zone 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.const import ATTR_ATTRIBUTION, CONF_USERNAME
from homeassistant.core import CALLBACK_TYPE, HomeAssistant from homeassistant.core import CALLBACK_TYPE, HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.exceptions import ConfigEntryNotReady
@ -132,7 +132,7 @@ class IcloudAccount:
self._config_entry.data[CONF_USERNAME], self._config_entry.data[CONF_USERNAME],
) )
self._require_reauth() self._config_entry.async_start_reauth(self.hass)
return return
try: try:
@ -164,7 +164,7 @@ class IcloudAccount:
return return
if self.api.requires_2fa: if self.api.requires_2fa:
self._require_reauth() self._config_entry.async_start_reauth(self.hass)
return return
api_devices = {} api_devices = {}
@ -230,19 +230,6 @@ class IcloudAccount:
utcnow() + timedelta(minutes=self._fetch_interval), 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: def _determine_interval(self) -> int:
"""Calculate new interval between two API fetch (in minutes).""" """Calculate new interval between two API fetch (in minutes)."""
intervals = {"default": self._max_interval} intervals = {"default": self._max_interval}

View File

@ -1,6 +1,10 @@
"""Config flow to configure the iCloud integration.""" """Config flow to configure the iCloud integration."""
from __future__ import annotations
from collections.abc import Mapping
import logging import logging
import os import os
from typing import Any
from pyicloud import PyiCloudService from pyicloud import PyiCloudService
from pyicloud.exceptions import ( from pyicloud.exceptions import (
@ -13,6 +17,7 @@ import voluptuous as vol
from homeassistant import config_entries from homeassistant import config_entries
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.storage import Store from homeassistant.helpers.storage import Store
from .const import ( from .const import (
@ -173,22 +178,23 @@ class IcloudFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
return await self._validate_and_create_entry(user_input, "user") return await self._validate_and_create_entry(user_input, "user")
async def async_step_reauth(self, user_input=None): async def async_step_reauth(self, entry_data: Mapping[str, Any]) -> FlowResult:
"""Update password for a config entry that can't authenticate.""" """Initialise re-authentication."""
# Store existing entry data so it can be used later and set unique ID # Store existing entry data so it can be used later and set unique ID
# so existing config entry can be updated # so existing config entry can be updated
if not self._existing_entry: await self.async_set_unique_id(self.context["unique_id"])
await self.async_set_unique_id(user_input.pop("unique_id")) self._existing_entry = {**entry_data}
self._existing_entry = user_input.copy() self._description_placeholders = {"username": entry_data[CONF_USERNAME]}
self._description_placeholders = {"username": user_input[CONF_USERNAME]} return await self.async_step_reauth_confirm()
user_input = None
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: 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( return await self._validate_and_create_entry(user_input, "reauth_confirm")
user_input, config_entries.SOURCE_REAUTH
)
async def async_step_trusted_device(self, user_input=None, errors=None): async def async_step_trusted_device(self, user_input=None, errors=None):
"""We need a trusted device.""" """We need a trusted device."""

View File

@ -10,7 +10,7 @@
"with_family": "With family" "with_family": "With family"
} }
}, },
"reauth": { "reauth_confirm": {
"title": "[%key:common::config_flow::title::reauth%]", "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.", "description": "Your previously entered password for {username} is no longer working. Update your password to keep using this integration.",
"data": { "data": {

View File

@ -11,7 +11,7 @@
"validate_verification_code": "Failed to verify your verification code, try again" "validate_verification_code": "Failed to verify your verification code, try again"
}, },
"step": { "step": {
"reauth": { "reauth_confirm": {
"data": { "data": {
"password": "Password" "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( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
context={"source": SOURCE_REAUTH}, context={"source": SOURCE_REAUTH, "unique_id": config_entry.unique_id},
data={**MOCK_CONFIG, "unique_id": USERNAME}, data={**MOCK_CONFIG},
) )
assert result["type"] == data_entry_flow.FlowResultType.FORM 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( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
context={"source": SOURCE_REAUTH}, context={"source": SOURCE_REAUTH, "unique_id": config_entry.unique_id},
data={**MOCK_CONFIG, "unique_id": USERNAME}, data={**MOCK_CONFIG},
) )
assert result["type"] == data_entry_flow.FlowResultType.FORM assert result["type"] == data_entry_flow.FlowResultType.FORM