mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 01:37:08 +00:00
Modify Tile to store a single dataclass in hass.data
(#75459)
This commit is contained in:
parent
8c7e329754
commit
2b752355d6
@ -1,6 +1,7 @@
|
|||||||
"""The Tile component."""
|
"""The Tile component."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from dataclasses import dataclass
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from functools import partial
|
from functools import partial
|
||||||
|
|
||||||
@ -17,7 +18,7 @@ from homeassistant.helpers.entity_registry import RegistryEntry, async_migrate_e
|
|||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||||
from homeassistant.util.async_ import gather_with_concurrency
|
from homeassistant.util.async_ import gather_with_concurrency
|
||||||
|
|
||||||
from .const import DATA_COORDINATOR, DATA_TILE, DOMAIN, LOGGER
|
from .const import DOMAIN, LOGGER
|
||||||
|
|
||||||
PLATFORMS = [Platform.DEVICE_TRACKER]
|
PLATFORMS = [Platform.DEVICE_TRACKER]
|
||||||
DEVICE_TYPES = ["PHONE", "TILE"]
|
DEVICE_TYPES = ["PHONE", "TILE"]
|
||||||
@ -28,6 +29,14 @@ DEFAULT_UPDATE_INTERVAL = timedelta(minutes=2)
|
|||||||
CONF_SHOW_INACTIVE = "show_inactive"
|
CONF_SHOW_INACTIVE = "show_inactive"
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class TileData:
|
||||||
|
"""Define an object to be stored in `hass.data`."""
|
||||||
|
|
||||||
|
coordinators: dict[str, DataUpdateCoordinator]
|
||||||
|
tiles: dict[str, Tile]
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
"""Set up Tile as config entry."""
|
"""Set up Tile as config entry."""
|
||||||
|
|
||||||
@ -100,10 +109,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
|
|
||||||
await gather_with_concurrency(DEFAULT_INIT_TASK_LIMIT, *coordinator_init_tasks)
|
await gather_with_concurrency(DEFAULT_INIT_TASK_LIMIT, *coordinator_init_tasks)
|
||||||
hass.data.setdefault(DOMAIN, {})
|
hass.data.setdefault(DOMAIN, {})
|
||||||
hass.data[DOMAIN][entry.entry_id] = {
|
hass.data[DOMAIN][entry.entry_id] = TileData(coordinators=coordinators, tiles=tiles)
|
||||||
DATA_COORDINATOR: coordinators,
|
|
||||||
DATA_TILE: tiles,
|
|
||||||
}
|
|
||||||
|
|
||||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||||
|
|
||||||
|
@ -3,7 +3,4 @@ import logging
|
|||||||
|
|
||||||
DOMAIN = "tile"
|
DOMAIN = "tile"
|
||||||
|
|
||||||
DATA_COORDINATOR = "coordinator"
|
|
||||||
DATA_TILE = "tile"
|
|
||||||
|
|
||||||
LOGGER = logging.getLogger(__package__)
|
LOGGER = logging.getLogger(__package__)
|
||||||
|
@ -18,7 +18,8 @@ from homeassistant.helpers.update_coordinator import (
|
|||||||
DataUpdateCoordinator,
|
DataUpdateCoordinator,
|
||||||
)
|
)
|
||||||
|
|
||||||
from .const import DATA_COORDINATOR, DATA_TILE, DOMAIN
|
from . import TileData
|
||||||
|
from .const import DOMAIN
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -38,14 +39,12 @@ async def async_setup_entry(
|
|||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up Tile device trackers."""
|
"""Set up Tile device trackers."""
|
||||||
|
data: TileData = hass.data[DOMAIN][entry.entry_id]
|
||||||
|
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
[
|
[
|
||||||
TileDeviceTracker(
|
TileDeviceTracker(entry, data.coordinators[tile_uuid], tile)
|
||||||
entry,
|
for tile_uuid, tile in data.tiles.items()
|
||||||
hass.data[DOMAIN][entry.entry_id][DATA_COORDINATOR][tile_uuid],
|
|
||||||
tile,
|
|
||||||
)
|
|
||||||
for tile_uuid, tile in hass.data[DOMAIN][entry.entry_id][DATA_TILE].items()
|
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -3,14 +3,13 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from pytile.tile import Tile
|
|
||||||
|
|
||||||
from homeassistant.components.diagnostics import async_redact_data
|
from homeassistant.components.diagnostics import async_redact_data
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE
|
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from .const import DATA_TILE, DOMAIN
|
from . import TileData
|
||||||
|
from .const import DOMAIN
|
||||||
|
|
||||||
CONF_ALTITUDE = "altitude"
|
CONF_ALTITUDE = "altitude"
|
||||||
CONF_UUID = "uuid"
|
CONF_UUID = "uuid"
|
||||||
@ -27,8 +26,8 @@ async def async_get_config_entry_diagnostics(
|
|||||||
hass: HomeAssistant, entry: ConfigEntry
|
hass: HomeAssistant, entry: ConfigEntry
|
||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
"""Return diagnostics for a config entry."""
|
"""Return diagnostics for a config entry."""
|
||||||
tiles: dict[str, Tile] = hass.data[DOMAIN][entry.entry_id][DATA_TILE]
|
data: TileData = hass.data[DOMAIN][entry.entry_id]
|
||||||
|
|
||||||
return async_redact_data(
|
return async_redact_data(
|
||||||
{"tiles": [tile.as_dict() for tile in tiles.values()]}, TO_REDACT
|
{"tiles": [tile.as_dict() for tile in data.tiles.values()]}, TO_REDACT
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user