Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The iCloud component."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 import voluptuous as vol
8 
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
11 from homeassistant.core import HomeAssistant, ServiceCall
13 from homeassistant.helpers.storage import Store
14 from homeassistant.util import slugify
15 
16 from .account import IcloudAccount
17 from .const import (
18  CONF_GPS_ACCURACY_THRESHOLD,
19  CONF_MAX_INTERVAL,
20  CONF_WITH_FAMILY,
21  DOMAIN,
22  PLATFORMS,
23  STORAGE_KEY,
24  STORAGE_VERSION,
25 )
26 
27 ATTRIBUTION = "Data provided by Apple iCloud"
28 
29 # entity attributes
30 ATTR_ACCOUNT_FETCH_INTERVAL = "account_fetch_interval"
31 ATTR_BATTERY = "battery"
32 ATTR_BATTERY_STATUS = "battery_status"
33 ATTR_DEVICE_NAME = "device_name"
34 ATTR_DEVICE_STATUS = "device_status"
35 ATTR_LOW_POWER_MODE = "low_power_mode"
36 ATTR_OWNER_NAME = "owner_fullname"
37 
38 # services
39 SERVICE_ICLOUD_PLAY_SOUND = "play_sound"
40 SERVICE_ICLOUD_DISPLAY_MESSAGE = "display_message"
41 SERVICE_ICLOUD_LOST_DEVICE = "lost_device"
42 SERVICE_ICLOUD_UPDATE = "update"
43 ATTR_ACCOUNT = "account"
44 ATTR_LOST_DEVICE_MESSAGE = "message"
45 ATTR_LOST_DEVICE_NUMBER = "number"
46 ATTR_LOST_DEVICE_SOUND = "sound"
47 
48 SERVICE_SCHEMA = vol.Schema({vol.Optional(ATTR_ACCOUNT): cv.string})
49 
50 SERVICE_SCHEMA_PLAY_SOUND = vol.Schema(
51  {vol.Required(ATTR_ACCOUNT): cv.string, vol.Required(ATTR_DEVICE_NAME): cv.string}
52 )
53 
54 SERVICE_SCHEMA_DISPLAY_MESSAGE = vol.Schema(
55  {
56  vol.Required(ATTR_ACCOUNT): cv.string,
57  vol.Required(ATTR_DEVICE_NAME): cv.string,
58  vol.Required(ATTR_LOST_DEVICE_MESSAGE): cv.string,
59  vol.Optional(ATTR_LOST_DEVICE_SOUND): cv.boolean,
60  }
61 )
62 
63 SERVICE_SCHEMA_LOST_DEVICE = vol.Schema(
64  {
65  vol.Required(ATTR_ACCOUNT): cv.string,
66  vol.Required(ATTR_DEVICE_NAME): cv.string,
67  vol.Required(ATTR_LOST_DEVICE_NUMBER): cv.string,
68  vol.Required(ATTR_LOST_DEVICE_MESSAGE): cv.string,
69  }
70 )
71 
72 
73 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
74  """Set up an iCloud account from a config entry."""
75 
76  hass.data.setdefault(DOMAIN, {})
77 
78  username = entry.data[CONF_USERNAME]
79  password = entry.data[CONF_PASSWORD]
80  with_family = entry.data[CONF_WITH_FAMILY]
81  max_interval = entry.data[CONF_MAX_INTERVAL]
82  gps_accuracy_threshold = entry.data[CONF_GPS_ACCURACY_THRESHOLD]
83 
84  # For backwards compat
85  if entry.unique_id is None:
86  hass.config_entries.async_update_entry(entry, unique_id=username)
87 
88  icloud_dir = Store[Any](hass, STORAGE_VERSION, STORAGE_KEY)
89 
90  account = IcloudAccount(
91  hass,
92  username,
93  password,
94  icloud_dir,
95  with_family,
96  max_interval,
97  gps_accuracy_threshold,
98  entry,
99  )
100  await hass.async_add_executor_job(account.setup)
101 
102  hass.data[DOMAIN][entry.unique_id] = account
103 
104  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
105 
106  def play_sound(service: ServiceCall) -> None:
107  """Play sound on the device."""
108  account = service.data[ATTR_ACCOUNT]
109  device_name: str = service.data[ATTR_DEVICE_NAME]
110  device_name = slugify(device_name.replace(" ", "", 99))
111 
112  for device in _get_account(account).get_devices_with_name(device_name):
113  device.play_sound()
114 
115  def display_message(service: ServiceCall) -> None:
116  """Display a message on the device."""
117  account = service.data[ATTR_ACCOUNT]
118  device_name: str = service.data[ATTR_DEVICE_NAME]
119  device_name = slugify(device_name.replace(" ", "", 99))
120  message = service.data.get(ATTR_LOST_DEVICE_MESSAGE)
121  sound = service.data.get(ATTR_LOST_DEVICE_SOUND, False)
122 
123  for device in _get_account(account).get_devices_with_name(device_name):
124  device.display_message(message, sound)
125 
126  def lost_device(service: ServiceCall) -> None:
127  """Make the device in lost state."""
128  account = service.data[ATTR_ACCOUNT]
129  device_name: str = service.data[ATTR_DEVICE_NAME]
130  device_name = slugify(device_name.replace(" ", "", 99))
131  number = service.data.get(ATTR_LOST_DEVICE_NUMBER)
132  message = service.data.get(ATTR_LOST_DEVICE_MESSAGE)
133 
134  for device in _get_account(account).get_devices_with_name(device_name):
135  device.lost_device(number, message)
136 
137  def update_account(service: ServiceCall) -> None:
138  """Call the update function of an iCloud account."""
139  if (account := service.data.get(ATTR_ACCOUNT)) is None:
140  for account in hass.data[DOMAIN].values():
141  account.keep_alive()
142  else:
143  _get_account(account).keep_alive()
144 
145  def _get_account(account_identifier: str) -> IcloudAccount:
146  if account_identifier is None:
147  return None
148 
149  icloud_account: IcloudAccount | None = hass.data[DOMAIN].get(account_identifier)
150  if icloud_account is None:
151  for account in hass.data[DOMAIN].values():
152  if account.username == account_identifier:
153  icloud_account = account
154 
155  if icloud_account is None:
156  raise ValueError(
157  f"No iCloud account with username or name {account_identifier}"
158  )
159  return icloud_account
160 
161  hass.services.async_register(
162  DOMAIN, SERVICE_ICLOUD_PLAY_SOUND, play_sound, schema=SERVICE_SCHEMA_PLAY_SOUND
163  )
164 
165  hass.services.async_register(
166  DOMAIN,
167  SERVICE_ICLOUD_DISPLAY_MESSAGE,
168  display_message,
169  schema=SERVICE_SCHEMA_DISPLAY_MESSAGE,
170  )
171 
172  hass.services.async_register(
173  DOMAIN,
174  SERVICE_ICLOUD_LOST_DEVICE,
175  lost_device,
176  schema=SERVICE_SCHEMA_LOST_DEVICE,
177  )
178 
179  hass.services.async_register(
180  DOMAIN, SERVICE_ICLOUD_UPDATE, update_account, schema=SERVICE_SCHEMA
181  )
182 
183  return True
184 
185 
186 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
187  """Unload a config entry."""
188  unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
189  if unload_ok:
190  hass.data[DOMAIN].pop(entry.data[CONF_USERNAME])
191  return unload_ok
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:186
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:73
str slugify(str|None text, *str separator="_")
Definition: __init__.py:41