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