From cd9cc98e07148fafa9ba2d410e6fcf8e387353d1 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Thu, 5 Nov 2026 08:53:41 +0100 Subject: [PATCH] Add action enums to icloud (#190520) --- homeassistant/components/icloud/const.py | 4 - homeassistant/components/icloud/services.py | 91 ++++++++++++--------- 2 files changed, 51 insertions(+), 44 deletions(-) diff --git a/homeassistant/components/icloud/const.py b/homeassistant/components/icloud/const.py index 1d0fd975ed14..f9fd81503b4e 100644 --- a/homeassistant/components/icloud/const.py +++ b/homeassistant/components/icloud/const.py @@ -119,14 +119,10 @@ DEVICE_STATUS_CODES = { # entity / service attributes -ATTR_ACCOUNT = "account" ATTR_ACCOUNT_FETCH_INTERVAL = "account_fetch_interval" ATTR_BATTERY = "battery" ATTR_BATTERY_STATUS = "battery_status" ATTR_DEVICE_NAME = "device_name" ATTR_DEVICE_STATUS = "device_status" ATTR_LOW_POWER_MODE = "low_power_mode" -ATTR_LOST_DEVICE_MESSAGE = "message" -ATTR_LOST_DEVICE_NUMBER = "number" -ATTR_LOST_DEVICE_SOUND = "sound" ATTR_OWNER_NAME = "owner_fullname" diff --git a/homeassistant/components/icloud/services.py b/homeassistant/components/icloud/services.py index e596943566cf..a2b9d2e58cc9 100644 --- a/homeassistant/components/icloud/services.py +++ b/homeassistant/components/icloud/services.py @@ -1,5 +1,7 @@ """The iCloud component.""" +from enum import StrEnum + import probatio from homeassistant.core import HomeAssistant, ServiceCall, callback @@ -7,53 +9,62 @@ from homeassistant.helpers import config_validation as cv from homeassistant.util import slugify from .account import IcloudAccount, IcloudConfigEntry -from .const import ( - ATTR_ACCOUNT, - ATTR_DEVICE_NAME, - ATTR_LOST_DEVICE_MESSAGE, - ATTR_LOST_DEVICE_NUMBER, - ATTR_LOST_DEVICE_SOUND, - DOMAIN, +from .const import DOMAIN + + +class IcloudService(StrEnum): + """Store keys for iCloud services.""" + + UPDATE = "update" + PLAY_SOUND = "play_sound" + DISPLAY_MESSAGE = "display_message" + LOST_DEVICE = "lost_device" + + +class IcloudServiceArgument(StrEnum): + """Store keys for iCloud service arguments.""" + + ACCOUNT = "account" + DEVICE_NAME = "device_name" + MESSAGE = "message" + SOUND = "sound" + NUMBER = "number" + + +SERVICE_SCHEMA = probatio.Schema( + {probatio.Optional(IcloudServiceArgument.ACCOUNT): cv.string} ) -# services -SERVICE_ICLOUD_PLAY_SOUND = "play_sound" -SERVICE_ICLOUD_DISPLAY_MESSAGE = "display_message" -SERVICE_ICLOUD_LOST_DEVICE = "lost_device" -SERVICE_ICLOUD_UPDATE = "update" - -SERVICE_SCHEMA = probatio.Schema({probatio.Optional(ATTR_ACCOUNT): cv.string}) - SERVICE_SCHEMA_PLAY_SOUND = probatio.Schema( { - probatio.Required(ATTR_ACCOUNT): cv.string, - probatio.Required(ATTR_DEVICE_NAME): cv.string, + probatio.Required(IcloudServiceArgument.ACCOUNT): cv.string, + probatio.Required(IcloudServiceArgument.DEVICE_NAME): cv.string, } ) SERVICE_SCHEMA_DISPLAY_MESSAGE = probatio.Schema( { - probatio.Required(ATTR_ACCOUNT): cv.string, - probatio.Required(ATTR_DEVICE_NAME): cv.string, - probatio.Required(ATTR_LOST_DEVICE_MESSAGE): cv.string, - probatio.Optional(ATTR_LOST_DEVICE_SOUND): cv.boolean, + probatio.Required(IcloudServiceArgument.ACCOUNT): cv.string, + probatio.Required(IcloudServiceArgument.DEVICE_NAME): cv.string, + probatio.Required(IcloudServiceArgument.MESSAGE): cv.string, + probatio.Optional(IcloudServiceArgument.SOUND): cv.boolean, } ) SERVICE_SCHEMA_LOST_DEVICE = probatio.Schema( { - probatio.Required(ATTR_ACCOUNT): cv.string, - probatio.Required(ATTR_DEVICE_NAME): cv.string, - probatio.Required(ATTR_LOST_DEVICE_NUMBER): cv.string, - probatio.Required(ATTR_LOST_DEVICE_MESSAGE): cv.string, + probatio.Required(IcloudServiceArgument.ACCOUNT): cv.string, + probatio.Required(IcloudServiceArgument.DEVICE_NAME): cv.string, + probatio.Required(IcloudServiceArgument.NUMBER): cv.string, + probatio.Required(IcloudServiceArgument.MESSAGE): cv.string, } ) def play_sound(service: ServiceCall) -> None: """Play sound on the device.""" - account = service.data[ATTR_ACCOUNT] - device_name: str = service.data[ATTR_DEVICE_NAME] + account = service.data[IcloudServiceArgument.ACCOUNT] + device_name: str = service.data[IcloudServiceArgument.DEVICE_NAME] device_name = slugify(device_name.replace(" ", "", 99)) for device in _get_account(service.hass, account).get_devices_with_name( @@ -64,11 +75,11 @@ def play_sound(service: ServiceCall) -> None: def display_message(service: ServiceCall) -> None: """Display a message on the device.""" - account = service.data[ATTR_ACCOUNT] - device_name: str = service.data[ATTR_DEVICE_NAME] + account = service.data[IcloudServiceArgument.ACCOUNT] + device_name: str = service.data[IcloudServiceArgument.DEVICE_NAME] device_name = slugify(device_name.replace(" ", "", 99)) - message = service.data.get(ATTR_LOST_DEVICE_MESSAGE) - sound = service.data.get(ATTR_LOST_DEVICE_SOUND, False) + message = service.data.get(IcloudServiceArgument.MESSAGE) + sound = service.data.get(IcloudServiceArgument.SOUND, False) for device in _get_account(service.hass, account).get_devices_with_name( device_name @@ -78,11 +89,11 @@ def display_message(service: ServiceCall) -> None: def lost_device(service: ServiceCall) -> None: """Make the device in lost state.""" - account = service.data[ATTR_ACCOUNT] - device_name: str = service.data[ATTR_DEVICE_NAME] + account = service.data[IcloudServiceArgument.ACCOUNT] + device_name: str = service.data[IcloudServiceArgument.DEVICE_NAME] device_name = slugify(device_name.replace(" ", "", 99)) - number = service.data.get(ATTR_LOST_DEVICE_NUMBER) - message = service.data.get(ATTR_LOST_DEVICE_MESSAGE) + number = service.data.get(IcloudServiceArgument.NUMBER) + message = service.data.get(IcloudServiceArgument.MESSAGE) for device in _get_account(service.hass, account).get_devices_with_name( device_name @@ -92,7 +103,7 @@ def lost_device(service: ServiceCall) -> None: def update_account(service: ServiceCall) -> None: """Call the update function of an iCloud account.""" - if (account := service.data.get(ATTR_ACCOUNT)) is None: + if (account := service.data.get(IcloudServiceArgument.ACCOUNT)) is None: # Update all accounts when no specific account is provided entry: IcloudConfigEntry for entry in service.hass.config_entries.async_loaded_entries(DOMAIN): @@ -118,23 +129,23 @@ def async_setup_services(hass: HomeAssistant) -> None: """Register iCloud services.""" hass.services.async_register( - DOMAIN, SERVICE_ICLOUD_PLAY_SOUND, play_sound, schema=SERVICE_SCHEMA_PLAY_SOUND + DOMAIN, IcloudService.PLAY_SOUND, play_sound, schema=SERVICE_SCHEMA_PLAY_SOUND ) hass.services.async_register( DOMAIN, - SERVICE_ICLOUD_DISPLAY_MESSAGE, + IcloudService.DISPLAY_MESSAGE, display_message, schema=SERVICE_SCHEMA_DISPLAY_MESSAGE, ) hass.services.async_register( DOMAIN, - SERVICE_ICLOUD_LOST_DEVICE, + IcloudService.LOST_DEVICE, lost_device, schema=SERVICE_SCHEMA_LOST_DEVICE, ) hass.services.async_register( - DOMAIN, SERVICE_ICLOUD_UPDATE, update_account, schema=SERVICE_SCHEMA + DOMAIN, IcloudService.UPDATE, update_account, schema=SERVICE_SCHEMA )