From 8c014f3e4ce2d987a94d16db9e0d7c19dda89b00 Mon Sep 17 00:00:00 2001 From: Tim Laing <11019084+timlaing@users.noreply.github.com> Date: Tue, 13 Oct 2026 19:10:59 +0100 Subject: [PATCH] Bump pyicloud to 2026.10.0 (#186146) --- homeassistant/components/icloud/account.py | 11 +++++ .../components/icloud/config_flow.py | 22 ++++++--- homeassistant/components/icloud/manifest.json | 2 +- homeassistant/components/icloud/strings.json | 4 ++ requirements_all.txt | 2 +- tests/components/icloud/test_account.py | 41 +++++++++++++++- tests/components/icloud/test_config_flow.py | 48 +++++++++++++++++++ 7 files changed, 119 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/icloud/account.py b/homeassistant/components/icloud/account.py index fe494d9063d4..aa7ac0e50906 100644 --- a/homeassistant/components/icloud/account.py +++ b/homeassistant/components/icloud/account.py @@ -7,6 +7,7 @@ from typing import TYPE_CHECKING, Any from pyicloud import PyiCloudService from pyicloud.exceptions import ( + PyiCloudConnectionException, PyiCloudFailedLoginException, PyiCloudNoDevicesException, PyiCloudServiceNotActivatedException, @@ -113,8 +114,18 @@ class IcloudAccount: self._password, self._icloud_dir.path, with_family=self._with_family, + refresh_interval=None, ) + except PyiCloudConnectionException as err: + # iCloud is unreachable; this says nothing about the credentials, so + # let the config entry retry instead of asking the user to log in. + raise ConfigEntryNotReady( + translation_domain=DOMAIN, + translation_key="cannot_connect", + translation_placeholders={"error": str(err)}, + ) from err + except PyiCloudFailedLoginException: self.api = None # Login failed which means credentials/2fa need to be updated. diff --git a/homeassistant/components/icloud/config_flow.py b/homeassistant/components/icloud/config_flow.py index d52f5e787da9..0f0b539f2d1b 100644 --- a/homeassistant/components/icloud/config_flow.py +++ b/homeassistant/components/icloud/config_flow.py @@ -1,6 +1,7 @@ """Config flow to configure the iCloud integration.""" from collections.abc import Mapping +from functools import partial import logging import os from typing import TYPE_CHECKING, Any, override @@ -8,6 +9,7 @@ from typing import TYPE_CHECKING, Any, override import probatio from pyicloud import PyiCloudService from pyicloud.exceptions import ( + PyiCloudConnectionException, PyiCloudException, PyiCloudFailedLoginException, PyiCloudNoDevicesException, @@ -147,19 +149,25 @@ class IcloudFlowHandler(ConfigFlow, domain=DOMAIN): if self.api is None: try: self.api = await self.hass.async_add_executor_job( - PyiCloudService, - self._username, - self._password, - Store(self.hass, STORAGE_VERSION, STORAGE_KEY).path, - True, - None, - self._with_family, + partial( + PyiCloudService, + self._username, + self._password, + Store(self.hass, STORAGE_VERSION, STORAGE_KEY).path, + with_family=self._with_family, + refresh_interval=None, + ) ) except PyiCloudFailedLoginException as error: _LOGGER.error("Error logging into iCloud service: %s", error) self.api = None errors = {CONF_PASSWORD: "invalid_auth"} return self._show_setup_form(user_input, errors, step_id) + except PyiCloudConnectionException as error: + _LOGGER.error("Error connecting to iCloud service: %s", error) + self.api = None + errors = {"base": "cannot_connect"} + return self._show_setup_form(user_input, errors, step_id) if self.api.requires_2fa: return await self.async_step_verification_code() diff --git a/homeassistant/components/icloud/manifest.json b/homeassistant/components/icloud/manifest.json index f9bc4456ed69..cad4303e83db 100644 --- a/homeassistant/components/icloud/manifest.json +++ b/homeassistant/components/icloud/manifest.json @@ -8,5 +8,5 @@ "integration_type": "hub", "iot_class": "cloud_polling", "loggers": ["keyrings.alt", "pyicloud"], - "requirements": ["pyicloud==2.7.0"] + "requirements": ["pyicloud==2026.10.0"] } diff --git a/homeassistant/components/icloud/strings.json b/homeassistant/components/icloud/strings.json index 6073feb153c3..93ded24fc8dc 100644 --- a/homeassistant/components/icloud/strings.json +++ b/homeassistant/components/icloud/strings.json @@ -6,6 +6,7 @@ "reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]" }, "error": { + "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]", "invalid_auth": "[%key:common::config_flow::error::invalid_auth%]", "send_verification_code": "Failed to send verification code", "validate_verification_code": "Failed to verify your verification code, try again" @@ -55,6 +56,9 @@ "album_type_not_specified": { "message": "Album type not specified" }, + "cannot_connect": { + "message": "An error occurred while connecting to iCloud: {error}" + }, "config_entry_not_found": { "message": "Config entry not found for account: {entry}" }, diff --git a/requirements_all.txt b/requirements_all.txt index b4c5bf3ed499..a8ced7c8ac45 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -2311,7 +2311,7 @@ pyhomeworks==1.1.2 pyialarm==2.2.0 # homeassistant.components.icloud -pyicloud==2.7.0 +pyicloud==2026.10.0 # homeassistant.components.imou pyimouapi==1.4.0 diff --git a/tests/components/icloud/test_account.py b/tests/components/icloud/test_account.py index 7ba37caa6650..8ec308512227 100644 --- a/tests/components/icloud/test_account.py +++ b/tests/components/icloud/test_account.py @@ -2,6 +2,7 @@ from unittest.mock import MagicMock, Mock, patch +from pyicloud.exceptions import PyiCloudConnectionException import pytest from homeassistant.components.icloud.account import IcloudAccount @@ -135,7 +136,41 @@ def mock_icloud_service_fixture(): service_instance.requires_2fa = False service_mock.return_value = service_instance - yield service_instance + yield service_mock + + +async def test_setup_connection_error( + hass: HomeAssistant, + mock_store: Mock, +) -> None: + """Test a transport failure during setup asks for a retry.""" + + config_entry = MockConfigEntry( + domain=DOMAIN, data=MOCK_CONFIG, entry_id="test", unique_id=USERNAME + ) + config_entry.add_to_hass(hass) + + account = IcloudAccount( + hass, + MOCK_CONFIG[CONF_USERNAME], + MOCK_CONFIG[CONF_PASSWORD], + mock_store, + MOCK_CONFIG[CONF_WITH_FAMILY], + MOCK_CONFIG[CONF_MAX_INTERVAL], + MOCK_CONFIG[CONF_GPS_ACCURACY_THRESHOLD], + config_entry, + ) + + with ( + patch( + "homeassistant.components.icloud.account.PyiCloudService", + side_effect=PyiCloudConnectionException(reason="Connection error"), + ), + pytest.raises(ConfigEntryNotReady) as err, + ): + account.setup() + + assert err.value.translation_key == "cannot_connect" async def test_setup_success_with_devices( @@ -172,5 +207,7 @@ async def test_setup_success_with_devices( assert account.family_members_fullname["johntravolta"] == "John TRAVOLTA" # An active locate must be requested on every poll (pyicloud >= 2.3.0 # only locates at service creation, so the account has to ask for it) - assert mock_icloud_service.devices.refresh_calls == [True] + assert mock_icloud_service.return_value.devices.refresh_calls == [True] assert "device1" in account.devices + # The account polls devices itself, so pyicloud's own Find My monitor is disabled. + assert mock_icloud_service.call_args.kwargs["refresh_interval"] is None diff --git a/tests/components/icloud/test_config_flow.py b/tests/components/icloud/test_config_flow.py index 192dc50e7aeb..81c94b232768 100644 --- a/tests/components/icloud/test_config_flow.py +++ b/tests/components/icloud/test_config_flow.py @@ -4,6 +4,7 @@ from unittest.mock import MagicMock, Mock, patch from pyicloud.exceptions import ( PyiCloudAPIResponseException, + PyiCloudConnectionException, PyiCloudFailedLoginException, ) import pytest @@ -282,6 +283,53 @@ async def test_login_failed(hass: HomeAssistant) -> None: assert result["type"] is FlowResultType.CREATE_ENTRY +@pytest.mark.usefixtures("service_authenticated") +async def test_connection_error(hass: HomeAssistant) -> None: + """Test when iCloud is unreachable during login.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_USER}, + ) + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "user" + assert result["errors"] == {} + + with patch( + "homeassistant.components.icloud.config_flow.PyiCloudService", + side_effect=PyiCloudConnectionException(reason="Connection error"), + ): + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={CONF_USERNAME: USERNAME, CONF_PASSWORD: PASSWORD}, + ) + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "user" + assert result["errors"] == {"base": "cannot_connect"} + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={CONF_USERNAME: USERNAME, CONF_PASSWORD: PASSWORD}, + ) + assert result["type"] is FlowResultType.CREATE_ENTRY + + +async def test_user_disables_pyicloud_monitor( + hass: HomeAssistant, service_authenticated: MagicMock +) -> None: + """Test the config flow disables pyicloud's own Find My monitor.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_USER}, + ) + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={CONF_USERNAME: USERNAME, CONF_PASSWORD: PASSWORD}, + ) + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["result"].unique_id == USERNAME + assert service_authenticated.call_args.kwargs["refresh_interval"] is None + + @pytest.mark.usefixtures("service_authenticated_no_device") async def test_no_device(hass: HomeAssistant) -> None: """Test when we have no devices."""