Fix icloud service calls (#150881)

This commit is contained in:
epenet
2025-08-19 14:19:10 +02:00
committed by GitHub
parent 69757bed52
commit c18dc9b63b

View File

@@ -8,7 +8,7 @@ from homeassistant.core import HomeAssistant, ServiceCall, callback
from homeassistant.helpers import config_validation as cv from homeassistant.helpers import config_validation as cv
from homeassistant.util import slugify from homeassistant.util import slugify
from .account import IcloudAccount from .account import IcloudAccount, IcloudConfigEntry
from .const import ( from .const import (
ATTR_ACCOUNT, ATTR_ACCOUNT,
ATTR_DEVICE_NAME, ATTR_DEVICE_NAME,
@@ -92,8 +92,10 @@ def lost_device(service: ServiceCall) -> None:
def update_account(service: ServiceCall) -> None: def update_account(service: ServiceCall) -> None:
"""Call the update function of an iCloud account.""" """Call the update function of an iCloud account."""
if (account := service.data.get(ATTR_ACCOUNT)) is None: if (account := service.data.get(ATTR_ACCOUNT)) is None:
for account in service.hass.data[DOMAIN].values(): # Update all accounts when no specific account is provided
account.keep_alive() entry: IcloudConfigEntry
for entry in service.hass.config_entries.async_loaded_entries(DOMAIN):
entry.runtime_data.keep_alive()
else: else:
_get_account(service.hass, account).keep_alive() _get_account(service.hass, account).keep_alive()
@@ -102,17 +104,12 @@ def _get_account(hass: HomeAssistant, account_identifier: str) -> IcloudAccount:
if account_identifier is None: if account_identifier is None:
return None return None
icloud_account: IcloudAccount | None = hass.data[DOMAIN].get(account_identifier) entry: IcloudConfigEntry
if icloud_account is None: for entry in hass.config_entries.async_loaded_entries(DOMAIN):
for account in hass.data[DOMAIN].values(): if entry.runtime_data.username == account_identifier:
if account.username == account_identifier: return entry.runtime_data
icloud_account = account
if icloud_account is None: raise ValueError(f"No iCloud account with username or name {account_identifier}")
raise ValueError(
f"No iCloud account with username or name {account_identifier}"
)
return icloud_account
@callback @callback