Add action enums to icloud (#190520)

This commit is contained in:
epenet
2026-11-05 08:53:41 +01:00
committed by GitHub
parent 92015c1715
commit cd9cc98e07
2 changed files with 51 additions and 44 deletions
-4
View File
@@ -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"
+51 -40
View File
@@ -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
)