mirror of
https://github.com/home-assistant/core.git
synced 2025-07-28 23:57:06 +00:00
Move icloud services to separate module (#144980)
This commit is contained in:
parent
0c5ee37721
commit
cbb092f792
@ -4,14 +4,10 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
import voluptuous as vol
|
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
||||||
from homeassistant.core import HomeAssistant, ServiceCall
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers import config_validation as cv
|
|
||||||
from homeassistant.helpers.storage import Store
|
from homeassistant.helpers.storage import Store
|
||||||
from homeassistant.util import slugify
|
|
||||||
|
|
||||||
from .account import IcloudAccount
|
from .account import IcloudAccount
|
||||||
from .const import (
|
from .const import (
|
||||||
@ -23,51 +19,7 @@ from .const import (
|
|||||||
STORAGE_KEY,
|
STORAGE_KEY,
|
||||||
STORAGE_VERSION,
|
STORAGE_VERSION,
|
||||||
)
|
)
|
||||||
|
from .services import register_services
|
||||||
ATTRIBUTION = "Data provided by Apple iCloud"
|
|
||||||
|
|
||||||
# entity attributes
|
|
||||||
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_OWNER_NAME = "owner_fullname"
|
|
||||||
|
|
||||||
# services
|
|
||||||
SERVICE_ICLOUD_PLAY_SOUND = "play_sound"
|
|
||||||
SERVICE_ICLOUD_DISPLAY_MESSAGE = "display_message"
|
|
||||||
SERVICE_ICLOUD_LOST_DEVICE = "lost_device"
|
|
||||||
SERVICE_ICLOUD_UPDATE = "update"
|
|
||||||
ATTR_ACCOUNT = "account"
|
|
||||||
ATTR_LOST_DEVICE_MESSAGE = "message"
|
|
||||||
ATTR_LOST_DEVICE_NUMBER = "number"
|
|
||||||
ATTR_LOST_DEVICE_SOUND = "sound"
|
|
||||||
|
|
||||||
SERVICE_SCHEMA = vol.Schema({vol.Optional(ATTR_ACCOUNT): cv.string})
|
|
||||||
|
|
||||||
SERVICE_SCHEMA_PLAY_SOUND = vol.Schema(
|
|
||||||
{vol.Required(ATTR_ACCOUNT): cv.string, vol.Required(ATTR_DEVICE_NAME): cv.string}
|
|
||||||
)
|
|
||||||
|
|
||||||
SERVICE_SCHEMA_DISPLAY_MESSAGE = vol.Schema(
|
|
||||||
{
|
|
||||||
vol.Required(ATTR_ACCOUNT): cv.string,
|
|
||||||
vol.Required(ATTR_DEVICE_NAME): cv.string,
|
|
||||||
vol.Required(ATTR_LOST_DEVICE_MESSAGE): cv.string,
|
|
||||||
vol.Optional(ATTR_LOST_DEVICE_SOUND): cv.boolean,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
SERVICE_SCHEMA_LOST_DEVICE = vol.Schema(
|
|
||||||
{
|
|
||||||
vol.Required(ATTR_ACCOUNT): cv.string,
|
|
||||||
vol.Required(ATTR_DEVICE_NAME): cv.string,
|
|
||||||
vol.Required(ATTR_LOST_DEVICE_NUMBER): cv.string,
|
|
||||||
vol.Required(ATTR_LOST_DEVICE_MESSAGE): cv.string,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
@ -103,82 +55,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
|
|
||||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||||
|
|
||||||
def play_sound(service: ServiceCall) -> None:
|
register_services(hass)
|
||||||
"""Play sound on the device."""
|
|
||||||
account = service.data[ATTR_ACCOUNT]
|
|
||||||
device_name: str = service.data[ATTR_DEVICE_NAME]
|
|
||||||
device_name = slugify(device_name.replace(" ", "", 99))
|
|
||||||
|
|
||||||
for device in _get_account(account).get_devices_with_name(device_name):
|
|
||||||
device.play_sound()
|
|
||||||
|
|
||||||
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]
|
|
||||||
device_name = slugify(device_name.replace(" ", "", 99))
|
|
||||||
message = service.data.get(ATTR_LOST_DEVICE_MESSAGE)
|
|
||||||
sound = service.data.get(ATTR_LOST_DEVICE_SOUND, False)
|
|
||||||
|
|
||||||
for device in _get_account(account).get_devices_with_name(device_name):
|
|
||||||
device.display_message(message, sound)
|
|
||||||
|
|
||||||
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]
|
|
||||||
device_name = slugify(device_name.replace(" ", "", 99))
|
|
||||||
number = service.data.get(ATTR_LOST_DEVICE_NUMBER)
|
|
||||||
message = service.data.get(ATTR_LOST_DEVICE_MESSAGE)
|
|
||||||
|
|
||||||
for device in _get_account(account).get_devices_with_name(device_name):
|
|
||||||
device.lost_device(number, message)
|
|
||||||
|
|
||||||
def update_account(service: ServiceCall) -> None:
|
|
||||||
"""Call the update function of an iCloud account."""
|
|
||||||
if (account := service.data.get(ATTR_ACCOUNT)) is None:
|
|
||||||
for account in hass.data[DOMAIN].values():
|
|
||||||
account.keep_alive()
|
|
||||||
else:
|
|
||||||
_get_account(account).keep_alive()
|
|
||||||
|
|
||||||
def _get_account(account_identifier: str) -> IcloudAccount:
|
|
||||||
if account_identifier is None:
|
|
||||||
return None
|
|
||||||
|
|
||||||
icloud_account: IcloudAccount | None = hass.data[DOMAIN].get(account_identifier)
|
|
||||||
if icloud_account is None:
|
|
||||||
for account in hass.data[DOMAIN].values():
|
|
||||||
if account.username == account_identifier:
|
|
||||||
icloud_account = account
|
|
||||||
|
|
||||||
if icloud_account is None:
|
|
||||||
raise ValueError(
|
|
||||||
f"No iCloud account with username or name {account_identifier}"
|
|
||||||
)
|
|
||||||
return icloud_account
|
|
||||||
|
|
||||||
hass.services.async_register(
|
|
||||||
DOMAIN, SERVICE_ICLOUD_PLAY_SOUND, play_sound, schema=SERVICE_SCHEMA_PLAY_SOUND
|
|
||||||
)
|
|
||||||
|
|
||||||
hass.services.async_register(
|
|
||||||
DOMAIN,
|
|
||||||
SERVICE_ICLOUD_DISPLAY_MESSAGE,
|
|
||||||
display_message,
|
|
||||||
schema=SERVICE_SCHEMA_DISPLAY_MESSAGE,
|
|
||||||
)
|
|
||||||
|
|
||||||
hass.services.async_register(
|
|
||||||
DOMAIN,
|
|
||||||
SERVICE_ICLOUD_LOST_DEVICE,
|
|
||||||
lost_device,
|
|
||||||
schema=SERVICE_SCHEMA_LOST_DEVICE,
|
|
||||||
)
|
|
||||||
|
|
||||||
hass.services.async_register(
|
|
||||||
DOMAIN, SERVICE_ICLOUD_UPDATE, update_account, schema=SERVICE_SCHEMA
|
|
||||||
)
|
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@ -29,6 +29,13 @@ from homeassistant.util.dt import utcnow
|
|||||||
from homeassistant.util.location import distance
|
from homeassistant.util.location import distance
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
|
ATTR_ACCOUNT_FETCH_INTERVAL,
|
||||||
|
ATTR_BATTERY,
|
||||||
|
ATTR_BATTERY_STATUS,
|
||||||
|
ATTR_DEVICE_NAME,
|
||||||
|
ATTR_DEVICE_STATUS,
|
||||||
|
ATTR_LOW_POWER_MODE,
|
||||||
|
ATTR_OWNER_NAME,
|
||||||
DEVICE_BATTERY_LEVEL,
|
DEVICE_BATTERY_LEVEL,
|
||||||
DEVICE_BATTERY_STATUS,
|
DEVICE_BATTERY_STATUS,
|
||||||
DEVICE_CLASS,
|
DEVICE_CLASS,
|
||||||
@ -49,25 +56,6 @@ from .const import (
|
|||||||
DOMAIN,
|
DOMAIN,
|
||||||
)
|
)
|
||||||
|
|
||||||
# entity attributes
|
|
||||||
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_OWNER_NAME = "owner_fullname"
|
|
||||||
|
|
||||||
# services
|
|
||||||
SERVICE_ICLOUD_PLAY_SOUND = "play_sound"
|
|
||||||
SERVICE_ICLOUD_DISPLAY_MESSAGE = "display_message"
|
|
||||||
SERVICE_ICLOUD_LOST_DEVICE = "lost_device"
|
|
||||||
SERVICE_ICLOUD_UPDATE = "update"
|
|
||||||
ATTR_ACCOUNT = "account"
|
|
||||||
ATTR_LOST_DEVICE_MESSAGE = "message"
|
|
||||||
ATTR_LOST_DEVICE_NUMBER = "number"
|
|
||||||
ATTR_LOST_DEVICE_SOUND = "sound"
|
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
@ -4,6 +4,8 @@ from homeassistant.const import Platform
|
|||||||
|
|
||||||
DOMAIN = "icloud"
|
DOMAIN = "icloud"
|
||||||
|
|
||||||
|
ATTRIBUTION = "Data provided by Apple iCloud"
|
||||||
|
|
||||||
CONF_WITH_FAMILY = "with_family"
|
CONF_WITH_FAMILY = "with_family"
|
||||||
CONF_MAX_INTERVAL = "max_interval"
|
CONF_MAX_INTERVAL = "max_interval"
|
||||||
CONF_GPS_ACCURACY_THRESHOLD = "gps_accuracy_threshold"
|
CONF_GPS_ACCURACY_THRESHOLD = "gps_accuracy_threshold"
|
||||||
@ -84,3 +86,17 @@ DEVICE_STATUS_CODES = {
|
|||||||
"203": "pending",
|
"203": "pending",
|
||||||
"204": "unregistered",
|
"204": "unregistered",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# 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"
|
||||||
|
141
homeassistant/components/icloud/services.py
Normal file
141
homeassistant/components/icloud/services.py
Normal file
@ -0,0 +1,141 @@
|
|||||||
|
"""The iCloud component."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import voluptuous as vol
|
||||||
|
|
||||||
|
from homeassistant.core import HomeAssistant, ServiceCall
|
||||||
|
from homeassistant.helpers import config_validation as cv
|
||||||
|
from homeassistant.util import slugify
|
||||||
|
|
||||||
|
from .account import IcloudAccount
|
||||||
|
from .const import (
|
||||||
|
ATTR_ACCOUNT,
|
||||||
|
ATTR_DEVICE_NAME,
|
||||||
|
ATTR_LOST_DEVICE_MESSAGE,
|
||||||
|
ATTR_LOST_DEVICE_NUMBER,
|
||||||
|
ATTR_LOST_DEVICE_SOUND,
|
||||||
|
DOMAIN,
|
||||||
|
)
|
||||||
|
|
||||||
|
# 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 = vol.Schema({vol.Optional(ATTR_ACCOUNT): cv.string})
|
||||||
|
|
||||||
|
SERVICE_SCHEMA_PLAY_SOUND = vol.Schema(
|
||||||
|
{vol.Required(ATTR_ACCOUNT): cv.string, vol.Required(ATTR_DEVICE_NAME): cv.string}
|
||||||
|
)
|
||||||
|
|
||||||
|
SERVICE_SCHEMA_DISPLAY_MESSAGE = vol.Schema(
|
||||||
|
{
|
||||||
|
vol.Required(ATTR_ACCOUNT): cv.string,
|
||||||
|
vol.Required(ATTR_DEVICE_NAME): cv.string,
|
||||||
|
vol.Required(ATTR_LOST_DEVICE_MESSAGE): cv.string,
|
||||||
|
vol.Optional(ATTR_LOST_DEVICE_SOUND): cv.boolean,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
SERVICE_SCHEMA_LOST_DEVICE = vol.Schema(
|
||||||
|
{
|
||||||
|
vol.Required(ATTR_ACCOUNT): cv.string,
|
||||||
|
vol.Required(ATTR_DEVICE_NAME): cv.string,
|
||||||
|
vol.Required(ATTR_LOST_DEVICE_NUMBER): cv.string,
|
||||||
|
vol.Required(ATTR_LOST_DEVICE_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]
|
||||||
|
device_name = slugify(device_name.replace(" ", "", 99))
|
||||||
|
|
||||||
|
for device in _get_account(service.hass, account).get_devices_with_name(
|
||||||
|
device_name
|
||||||
|
):
|
||||||
|
device.play_sound()
|
||||||
|
|
||||||
|
|
||||||
|
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]
|
||||||
|
device_name = slugify(device_name.replace(" ", "", 99))
|
||||||
|
message = service.data.get(ATTR_LOST_DEVICE_MESSAGE)
|
||||||
|
sound = service.data.get(ATTR_LOST_DEVICE_SOUND, False)
|
||||||
|
|
||||||
|
for device in _get_account(service.hass, account).get_devices_with_name(
|
||||||
|
device_name
|
||||||
|
):
|
||||||
|
device.display_message(message, sound)
|
||||||
|
|
||||||
|
|
||||||
|
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]
|
||||||
|
device_name = slugify(device_name.replace(" ", "", 99))
|
||||||
|
number = service.data.get(ATTR_LOST_DEVICE_NUMBER)
|
||||||
|
message = service.data.get(ATTR_LOST_DEVICE_MESSAGE)
|
||||||
|
|
||||||
|
for device in _get_account(service.hass, account).get_devices_with_name(
|
||||||
|
device_name
|
||||||
|
):
|
||||||
|
device.lost_device(number, message)
|
||||||
|
|
||||||
|
|
||||||
|
def update_account(service: ServiceCall) -> None:
|
||||||
|
"""Call the update function of an iCloud account."""
|
||||||
|
if (account := service.data.get(ATTR_ACCOUNT)) is None:
|
||||||
|
for account in service.hass.data[DOMAIN].values():
|
||||||
|
account.keep_alive()
|
||||||
|
else:
|
||||||
|
_get_account(service.hass, account).keep_alive()
|
||||||
|
|
||||||
|
|
||||||
|
def _get_account(hass: HomeAssistant, account_identifier: str) -> IcloudAccount:
|
||||||
|
if account_identifier is None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
icloud_account: IcloudAccount | None = hass.data[DOMAIN].get(account_identifier)
|
||||||
|
if icloud_account is None:
|
||||||
|
for account in hass.data[DOMAIN].values():
|
||||||
|
if account.username == account_identifier:
|
||||||
|
icloud_account = account
|
||||||
|
|
||||||
|
if icloud_account is None:
|
||||||
|
raise ValueError(
|
||||||
|
f"No iCloud account with username or name {account_identifier}"
|
||||||
|
)
|
||||||
|
return icloud_account
|
||||||
|
|
||||||
|
|
||||||
|
def register_services(hass: HomeAssistant) -> None:
|
||||||
|
"""Set up an iCloud account from a config entry."""
|
||||||
|
|
||||||
|
hass.services.async_register(
|
||||||
|
DOMAIN, SERVICE_ICLOUD_PLAY_SOUND, play_sound, schema=SERVICE_SCHEMA_PLAY_SOUND
|
||||||
|
)
|
||||||
|
|
||||||
|
hass.services.async_register(
|
||||||
|
DOMAIN,
|
||||||
|
SERVICE_ICLOUD_DISPLAY_MESSAGE,
|
||||||
|
display_message,
|
||||||
|
schema=SERVICE_SCHEMA_DISPLAY_MESSAGE,
|
||||||
|
)
|
||||||
|
|
||||||
|
hass.services.async_register(
|
||||||
|
DOMAIN,
|
||||||
|
SERVICE_ICLOUD_LOST_DEVICE,
|
||||||
|
lost_device,
|
||||||
|
schema=SERVICE_SCHEMA_LOST_DEVICE,
|
||||||
|
)
|
||||||
|
|
||||||
|
hass.services.async_register(
|
||||||
|
DOMAIN, SERVICE_ICLOUD_UPDATE, update_account, schema=SERVICE_SCHEMA
|
||||||
|
)
|
Loading…
x
Reference in New Issue
Block a user