1 """The iCloud component."""
3 from __future__
import annotations
7 import voluptuous
as vol
16 from .account
import IcloudAccount
18 CONF_GPS_ACCURACY_THRESHOLD,
27 ATTRIBUTION =
"Data provided by Apple iCloud"
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"
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"
48 SERVICE_SCHEMA = vol.Schema({vol.Optional(ATTR_ACCOUNT): cv.string})
50 SERVICE_SCHEMA_PLAY_SOUND = vol.Schema(
51 {vol.Required(ATTR_ACCOUNT): cv.string, vol.Required(ATTR_DEVICE_NAME): cv.string}
54 SERVICE_SCHEMA_DISPLAY_MESSAGE = vol.Schema(
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,
63 SERVICE_SCHEMA_LOST_DEVICE = vol.Schema(
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,
74 """Set up an iCloud account from a config entry."""
76 hass.data.setdefault(DOMAIN, {})
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]
85 if entry.unique_id
is None:
86 hass.config_entries.async_update_entry(entry, unique_id=username)
88 icloud_dir = Store[Any](hass, STORAGE_VERSION, STORAGE_KEY)
97 gps_accuracy_threshold,
100 await hass.async_add_executor_job(account.setup)
102 hass.data[DOMAIN][entry.unique_id] = account
104 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
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))
112 for device
in _get_account(account).get_devices_with_name(device_name):
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)
123 for device
in _get_account(account).get_devices_with_name(device_name):
124 device.display_message(message, sound)
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)
134 for device
in _get_account(account).get_devices_with_name(device_name):
135 device.lost_device(number, message)
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():
143 _get_account(account).keep_alive()
145 def _get_account(account_identifier: str) -> IcloudAccount:
146 if account_identifier
is None:
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
155 if icloud_account
is None:
157 f
"No iCloud account with username or name {account_identifier}"
159 return icloud_account
161 hass.services.async_register(
162 DOMAIN, SERVICE_ICLOUD_PLAY_SOUND, play_sound, schema=SERVICE_SCHEMA_PLAY_SOUND
165 hass.services.async_register(
167 SERVICE_ICLOUD_DISPLAY_MESSAGE,
169 schema=SERVICE_SCHEMA_DISPLAY_MESSAGE,
172 hass.services.async_register(
174 SERVICE_ICLOUD_LOST_DEVICE,
176 schema=SERVICE_SCHEMA_LOST_DEVICE,
179 hass.services.async_register(
180 DOMAIN, SERVICE_ICLOUD_UPDATE, update_account, schema=SERVICE_SCHEMA
187 """Unload a config entry."""
188 unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
190 hass.data[DOMAIN].pop(entry.data[CONF_USERNAME])
web.Response get(self, web.Request request, str config_key)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
str slugify(str|None text, *str separator="_")