mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 17:27:10 +00:00
Use entry runtime data in Teslemetry (#117283)
* runtime_data * runtime_data * Remove some code * format * Fix missing entry.runtime_data
This commit is contained in:
parent
f318a3b5e2
commit
7509ccff40
@ -119,9 +119,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
)
|
||||
|
||||
# Setup Platforms
|
||||
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = TeslemetryData(
|
||||
vehicles, energysites, scopes
|
||||
)
|
||||
entry.runtime_data = TeslemetryData(vehicles, energysites, scopes)
|
||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||
|
||||
return True
|
||||
@ -130,5 +128,5 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
"""Unload Teslemetry Config."""
|
||||
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
||||
hass.data[DOMAIN].pop(entry.entry_id)
|
||||
del entry.runtime_data
|
||||
return unload_ok
|
||||
|
@ -17,7 +17,7 @@ from homeassistant.const import ATTR_TEMPERATURE, PRECISION_HALVES, UnitOfTemper
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from .const import DOMAIN, TeslemetryClimateSide
|
||||
from .const import TeslemetryClimateSide
|
||||
from .entity import TeslemetryVehicleEntity
|
||||
from .models import TeslemetryVehicleData
|
||||
|
||||
@ -29,11 +29,12 @@ async def async_setup_entry(
|
||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||
) -> None:
|
||||
"""Set up the Teslemetry Climate platform from a config entry."""
|
||||
data = hass.data[DOMAIN][entry.entry_id]
|
||||
|
||||
async_add_entities(
|
||||
TeslemetryClimateEntity(vehicle, TeslemetryClimateSide.DRIVER, data.scopes)
|
||||
for vehicle in data.vehicles
|
||||
TeslemetryClimateEntity(
|
||||
vehicle, TeslemetryClimateSide.DRIVER, entry.runtime_data.scopes
|
||||
)
|
||||
for vehicle in entry.runtime_data.vehicles
|
||||
)
|
||||
|
||||
|
||||
|
@ -8,8 +8,6 @@ from homeassistant.components.diagnostics import async_redact_data
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .const import DOMAIN
|
||||
|
||||
VEHICLE_REDACT = [
|
||||
"id",
|
||||
"user_id",
|
||||
@ -32,12 +30,9 @@ async def async_get_config_entry_diagnostics(
|
||||
hass: HomeAssistant, config_entry: ConfigEntry
|
||||
) -> dict[str, Any]:
|
||||
"""Return diagnostics for a config entry."""
|
||||
vehicles = [
|
||||
x.coordinator.data for x in hass.data[DOMAIN][config_entry.entry_id].vehicles
|
||||
]
|
||||
vehicles = [x.coordinator.data for x in config_entry.runtime_data.vehicles]
|
||||
energysites = [
|
||||
x.live_coordinator.data
|
||||
for x in hass.data[DOMAIN][config_entry.entry_id].energysites
|
||||
x.live_coordinator.data for x in config_entry.runtime_data.energysites
|
||||
]
|
||||
|
||||
# Return only the relevant children
|
||||
|
@ -34,7 +34,6 @@ from homeassistant.helpers.typing import StateType
|
||||
from homeassistant.util import dt as dt_util
|
||||
from homeassistant.util.variance import ignore_variance
|
||||
|
||||
from .const import DOMAIN
|
||||
from .entity import (
|
||||
TeslemetryEnergyInfoEntity,
|
||||
TeslemetryEnergyLiveEntity,
|
||||
@ -417,35 +416,33 @@ async def async_setup_entry(
|
||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||
) -> None:
|
||||
"""Set up the Teslemetry sensor platform from a config entry."""
|
||||
data = hass.data[DOMAIN][entry.entry_id]
|
||||
|
||||
async_add_entities(
|
||||
chain(
|
||||
( # Add vehicles
|
||||
TeslemetryVehicleSensorEntity(vehicle, description)
|
||||
for vehicle in data.vehicles
|
||||
for vehicle in entry.runtime_data.vehicles
|
||||
for description in VEHICLE_DESCRIPTIONS
|
||||
),
|
||||
( # Add vehicles time sensors
|
||||
TeslemetryVehicleTimeSensorEntity(vehicle, description)
|
||||
for vehicle in data.vehicles
|
||||
for vehicle in entry.runtime_data.vehicles
|
||||
for description in VEHICLE_TIME_DESCRIPTIONS
|
||||
),
|
||||
( # Add energy site live
|
||||
TeslemetryEnergyLiveSensorEntity(energysite, description)
|
||||
for energysite in data.energysites
|
||||
for energysite in entry.runtime_data.energysites
|
||||
for description in ENERGY_LIVE_DESCRIPTIONS
|
||||
if description.key in energysite.live_coordinator.data
|
||||
),
|
||||
( # Add wall connectors
|
||||
TeslemetryWallConnectorSensorEntity(energysite, din, description)
|
||||
for energysite in data.energysites
|
||||
for energysite in entry.runtime_data.energysites
|
||||
for din in energysite.live_coordinator.data.get("wall_connectors", {})
|
||||
for description in WALL_CONNECTOR_DESCRIPTIONS
|
||||
),
|
||||
( # Add energy site info
|
||||
TeslemetryEnergyInfoSensorEntity(energysite, description)
|
||||
for energysite in data.energysites
|
||||
for energysite in entry.runtime_data.energysites
|
||||
for description in ENERGY_INFO_DESCRIPTIONS
|
||||
if description.key in energysite.info_coordinator.data
|
||||
),
|
||||
|
@ -25,11 +25,10 @@ async def setup_platform(hass: HomeAssistant, platforms: list[Platform] | None =
|
||||
|
||||
if platforms is None:
|
||||
await hass.config_entries.async_setup(mock_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
else:
|
||||
with patch("homeassistant.components.teslemetry.PLATFORMS", platforms):
|
||||
await hass.config_entries.async_setup(mock_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
return mock_entry
|
||||
|
||||
@ -41,6 +40,7 @@ def assert_entities(
|
||||
snapshot: SnapshotAssertion,
|
||||
) -> None:
|
||||
"""Test that all entities match their snapshot."""
|
||||
|
||||
entity_entries = er.async_entries_for_config_entry(entity_registry, entry_id)
|
||||
|
||||
assert entity_entries
|
||||
|
@ -10,6 +10,7 @@ from tesla_fleet_api.exceptions import (
|
||||
)
|
||||
|
||||
from homeassistant.components.teslemetry.coordinator import VEHICLE_INTERVAL
|
||||
from homeassistant.components.teslemetry.models import TeslemetryData
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
@ -30,9 +31,11 @@ async def test_load_unload(hass: HomeAssistant) -> None:
|
||||
|
||||
entry = await setup_platform(hass)
|
||||
assert entry.state is ConfigEntryState.LOADED
|
||||
assert isinstance(entry.runtime_data, TeslemetryData)
|
||||
assert await hass.config_entries.async_unload(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
assert entry.state is ConfigEntryState.NOT_LOADED
|
||||
assert not hasattr(entry, "runtime_data")
|
||||
|
||||
|
||||
@pytest.mark.parametrize(("side_effect", "state"), ERRORS)
|
||||
|
Loading…
x
Reference in New Issue
Block a user