mirror of
https://github.com/home-assistant/core.git
synced 2025-07-28 15:47:12 +00:00
Use runtime_data in icloud (#145179)
This commit is contained in:
parent
da6c6c5201
commit
bd190b9b4c
@ -4,17 +4,15 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
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
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.storage import Store
|
from homeassistant.helpers.storage import Store
|
||||||
|
|
||||||
from .account import IcloudAccount
|
from .account import IcloudAccount, IcloudConfigEntry
|
||||||
from .const import (
|
from .const import (
|
||||||
CONF_GPS_ACCURACY_THRESHOLD,
|
CONF_GPS_ACCURACY_THRESHOLD,
|
||||||
CONF_MAX_INTERVAL,
|
CONF_MAX_INTERVAL,
|
||||||
CONF_WITH_FAMILY,
|
CONF_WITH_FAMILY,
|
||||||
DOMAIN,
|
|
||||||
PLATFORMS,
|
PLATFORMS,
|
||||||
STORAGE_KEY,
|
STORAGE_KEY,
|
||||||
STORAGE_VERSION,
|
STORAGE_VERSION,
|
||||||
@ -22,11 +20,9 @@ from .const import (
|
|||||||
from .services import register_services
|
from .services import register_services
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: IcloudConfigEntry) -> bool:
|
||||||
"""Set up an iCloud account from a config entry."""
|
"""Set up an iCloud account from a config entry."""
|
||||||
|
|
||||||
hass.data.setdefault(DOMAIN, {})
|
|
||||||
|
|
||||||
username = entry.data[CONF_USERNAME]
|
username = entry.data[CONF_USERNAME]
|
||||||
password = entry.data[CONF_PASSWORD]
|
password = entry.data[CONF_PASSWORD]
|
||||||
with_family = entry.data[CONF_WITH_FAMILY]
|
with_family = entry.data[CONF_WITH_FAMILY]
|
||||||
@ -51,7 +47,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
)
|
)
|
||||||
await hass.async_add_executor_job(account.setup)
|
await hass.async_add_executor_job(account.setup)
|
||||||
|
|
||||||
hass.data[DOMAIN][entry.unique_id] = account
|
entry.runtime_data = account
|
||||||
|
|
||||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||||
|
|
||||||
@ -60,9 +56,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_unload_entry(hass: HomeAssistant, entry: IcloudConfigEntry) -> bool:
|
||||||
"""Unload a config entry."""
|
"""Unload a config entry."""
|
||||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||||
if unload_ok:
|
|
||||||
hass.data[DOMAIN].pop(entry.data[CONF_USERNAME])
|
|
||||||
return unload_ok
|
|
||||||
|
@ -58,6 +58,8 @@ from .const import (
|
|||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
type IcloudConfigEntry = ConfigEntry[IcloudAccount]
|
||||||
|
|
||||||
|
|
||||||
class IcloudAccount:
|
class IcloudAccount:
|
||||||
"""Representation of an iCloud account."""
|
"""Representation of an iCloud account."""
|
||||||
@ -71,7 +73,7 @@ class IcloudAccount:
|
|||||||
with_family: bool,
|
with_family: bool,
|
||||||
max_interval: int,
|
max_interval: int,
|
||||||
gps_accuracy_threshold: int,
|
gps_accuracy_threshold: int,
|
||||||
config_entry: ConfigEntry,
|
config_entry: IcloudConfigEntry,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize an iCloud account."""
|
"""Initialize an iCloud account."""
|
||||||
self.hass = hass
|
self.hass = hass
|
||||||
|
@ -5,13 +5,12 @@ from __future__ import annotations
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from homeassistant.components.device_tracker import TrackerEntity
|
from homeassistant.components.device_tracker import TrackerEntity
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
|
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
|
||||||
from homeassistant.helpers.device_registry import DeviceInfo
|
from homeassistant.helpers.device_registry import DeviceInfo
|
||||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||||
|
|
||||||
from .account import IcloudAccount, IcloudDevice
|
from .account import IcloudAccount, IcloudConfigEntry, IcloudDevice
|
||||||
from .const import (
|
from .const import (
|
||||||
DEVICE_LOCATION_HORIZONTAL_ACCURACY,
|
DEVICE_LOCATION_HORIZONTAL_ACCURACY,
|
||||||
DEVICE_LOCATION_LATITUDE,
|
DEVICE_LOCATION_LATITUDE,
|
||||||
@ -22,11 +21,11 @@ from .const import (
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entry: ConfigEntry,
|
entry: IcloudConfigEntry,
|
||||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up device tracker for iCloud component."""
|
"""Set up device tracker for iCloud component."""
|
||||||
account: IcloudAccount = hass.data[DOMAIN][entry.unique_id]
|
account = entry.runtime_data
|
||||||
tracked = set[str]()
|
tracked = set[str]()
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
|
@ -5,7 +5,6 @@ from __future__ import annotations
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
|
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import PERCENTAGE
|
from homeassistant.const import PERCENTAGE
|
||||||
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
|
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
|
||||||
from homeassistant.helpers.device_registry import DeviceInfo
|
from homeassistant.helpers.device_registry import DeviceInfo
|
||||||
@ -13,17 +12,17 @@ from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
|||||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||||
from homeassistant.helpers.icon import icon_for_battery_level
|
from homeassistant.helpers.icon import icon_for_battery_level
|
||||||
|
|
||||||
from .account import IcloudAccount, IcloudDevice
|
from .account import IcloudAccount, IcloudConfigEntry, IcloudDevice
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entry: ConfigEntry,
|
entry: IcloudConfigEntry,
|
||||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up device tracker for iCloud component."""
|
"""Set up device tracker for iCloud component."""
|
||||||
account: IcloudAccount = hass.data[DOMAIN][entry.unique_id]
|
account = entry.runtime_data
|
||||||
tracked = set[str]()
|
tracked = set[str]()
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
|
Loading…
x
Reference in New Issue
Block a user