Skip to content

Commit

Permalink
Store runtime data in entry in renault (#116454)
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet authored Apr 30, 2024
1 parent 0005f84 commit a440783
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 52 deletions.
12 changes: 8 additions & 4 deletions homeassistant/components/renault/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from .services import setup_services

CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
RenaultConfigEntry = ConfigEntry[RenaultHub]


async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
Expand All @@ -23,7 +24,9 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
return True


async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
async def async_setup_entry(
hass: HomeAssistant, config_entry: RenaultConfigEntry
) -> bool:
"""Load a config entry."""
renault_hub = RenaultHub(hass, config_entry.data[CONF_LOCALE])
try:
Expand All @@ -36,19 +39,20 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
if not login_success:
raise ConfigEntryAuthFailed

hass.data.setdefault(DOMAIN, {})
try:
await renault_hub.async_initialise(config_entry)
except aiohttp.ClientError as exc:
raise ConfigEntryNotReady from exc

hass.data[DOMAIN][config_entry.entry_id] = renault_hub
config_entry.runtime_data = renault_hub

await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)

return True


async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
async def async_unload_entry(
hass: HomeAssistant, config_entry: RenaultConfigEntry
) -> bool:
"""Unload a config entry."""
return await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS)
9 changes: 3 additions & 6 deletions homeassistant/components/renault/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@
BinarySensorEntity,
BinarySensorEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType

from .const import DOMAIN
from . import RenaultConfigEntry
from .entity import RenaultDataEntity, RenaultDataEntityDescription
from .renault_hub import RenaultHub


@dataclass(frozen=True, kw_only=True)
Expand All @@ -35,14 +33,13 @@ class RenaultBinarySensorEntityDescription(

async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: RenaultConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Renault entities from config entry."""
proxy: RenaultHub = hass.data[DOMAIN][config_entry.entry_id]
entities: list[RenaultBinarySensor] = [
RenaultBinarySensor(vehicle, description)
for vehicle in proxy.vehicles.values()
for vehicle in config_entry.runtime_data.vehicles.values()
for description in BINARY_SENSOR_TYPES
if description.coordinator in vehicle.coordinators
]
Expand Down
9 changes: 3 additions & 6 deletions homeassistant/components/renault/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@
from typing import Any

from homeassistant.components.button import ButtonEntity, ButtonEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .const import DOMAIN
from . import RenaultConfigEntry
from .entity import RenaultEntity
from .renault_hub import RenaultHub


@dataclass(frozen=True, kw_only=True)
Expand All @@ -26,14 +24,13 @@ class RenaultButtonEntityDescription(ButtonEntityDescription):

async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: RenaultConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Renault entities from config entry."""
proxy: RenaultHub = hass.data[DOMAIN][config_entry.entry_id]
entities: list[RenaultButtonEntity] = [
RenaultButtonEntity(vehicle, description)
for vehicle in proxy.vehicles.values()
for vehicle in config_entry.runtime_data.vehicles.values()
for description in BUTTON_TYPES
if not description.requires_electricity or vehicle.details.uses_electricity()
]
Expand Down
9 changes: 3 additions & 6 deletions homeassistant/components/renault/device_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,22 @@
from renault_api.kamereon.models import KamereonVehicleLocationData

from homeassistant.components.device_tracker import SourceType, TrackerEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .const import DOMAIN
from . import RenaultConfigEntry
from .entity import RenaultDataEntity, RenaultDataEntityDescription
from .renault_hub import RenaultHub


async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: RenaultConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Renault entities from config entry."""
proxy: RenaultHub = hass.data[DOMAIN][config_entry.entry_id]
entities: list[RenaultDeviceTracker] = [
RenaultDeviceTracker(vehicle, description)
for vehicle in proxy.vehicles.values()
for vehicle in config_entry.runtime_data.vehicles.values()
for description in DEVICE_TRACKER_TYPES
if description.coordinator in vehicle.coordinators
]
Expand Down
16 changes: 6 additions & 10 deletions homeassistant/components/renault/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
from typing import Any

from homeassistant.components.diagnostics import async_redact_data
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceEntry

from . import RenaultHub
from .const import CONF_KAMEREON_ACCOUNT_ID, DOMAIN
from . import RenaultConfigEntry
from .const import CONF_KAMEREON_ACCOUNT_ID
from .renault_vehicle import RenaultVehicleProxy

TO_REDACT = {
Expand All @@ -27,30 +26,27 @@


async def async_get_config_entry_diagnostics(
hass: HomeAssistant, entry: ConfigEntry
hass: HomeAssistant, entry: RenaultConfigEntry
) -> dict[str, Any]:
"""Return diagnostics for a config entry."""
renault_hub: RenaultHub = hass.data[DOMAIN][entry.entry_id]

return {
"entry": {
"title": entry.title,
"data": async_redact_data(entry.data, TO_REDACT),
},
"vehicles": [
_get_vehicle_diagnostics(vehicle)
for vehicle in renault_hub.vehicles.values()
for vehicle in entry.runtime_data.vehicles.values()
],
}


async def async_get_device_diagnostics(
hass: HomeAssistant, entry: ConfigEntry, device: DeviceEntry
hass: HomeAssistant, entry: RenaultConfigEntry, device: DeviceEntry
) -> dict[str, Any]:
"""Return diagnostics for a device."""
renault_hub: RenaultHub = hass.data[DOMAIN][entry.entry_id]
vin = next(iter(device.identifiers))[1]
vehicle = renault_hub.vehicles[vin]
vehicle = entry.runtime_data.vehicles[vin]

return _get_vehicle_diagnostics(vehicle)

Expand Down
9 changes: 3 additions & 6 deletions homeassistant/components/renault/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@
from renault_api.kamereon.models import KamereonVehicleBatteryStatusData

from homeassistant.components.select import SelectEntity, SelectEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType

from .const import DOMAIN
from . import RenaultConfigEntry
from .entity import RenaultDataEntity, RenaultDataEntityDescription
from .renault_hub import RenaultHub


@dataclass(frozen=True, kw_only=True)
Expand All @@ -29,14 +27,13 @@ class RenaultSelectEntityDescription(

async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: RenaultConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Renault entities from config entry."""
proxy: RenaultHub = hass.data[DOMAIN][config_entry.entry_id]
entities: list[RenaultSelectEntity] = [
RenaultSelectEntity(vehicle, description)
for vehicle in proxy.vehicles.values()
for vehicle in config_entry.runtime_data.vehicles.values()
for description in SENSOR_TYPES
if description.coordinator in vehicle.coordinators
]
Expand Down
9 changes: 3 additions & 6 deletions homeassistant/components/renault/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
PERCENTAGE,
UnitOfEnergy,
Expand All @@ -36,10 +35,9 @@
from homeassistant.helpers.typing import StateType
from homeassistant.util.dt import as_utc, parse_datetime

from .const import DOMAIN
from . import RenaultConfigEntry
from .coordinator import T
from .entity import RenaultDataEntity, RenaultDataEntityDescription
from .renault_hub import RenaultHub
from .renault_vehicle import RenaultVehicleProxy


Expand All @@ -58,14 +56,13 @@ class RenaultSensorEntityDescription(

async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: RenaultConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Renault entities from config entry."""
proxy: RenaultHub = hass.data[DOMAIN][config_entry.entry_id]
entities: list[RenaultSensor[Any]] = [
description.entity_class(vehicle, description)
for vehicle in proxy.vehicles.values()
for vehicle in config_entry.runtime_data.vehicles.values()
for description in SENSOR_TYPES
if description.coordinator in vehicle.coordinators
and (not description.requires_fuel or vehicle.details.uses_fuel())
Expand Down
15 changes: 11 additions & 4 deletions homeassistant/components/renault/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@

import voluptuous as vol

from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant, ServiceCall
from homeassistant.helpers import config_validation as cv, device_registry as dr

from .const import DOMAIN
from .renault_hub import RenaultHub
from .renault_vehicle import RenaultVehicleProxy

if TYPE_CHECKING:
from . import RenaultConfigEntry

LOGGER = logging.getLogger(__name__)

ATTR_SCHEDULES = "schedules"
Expand Down Expand Up @@ -116,9 +119,13 @@ def get_vehicle_proxy(service_call_data: Mapping) -> RenaultVehicleProxy:
if device_entry is None:
raise ValueError(f"Unable to find device with id: {device_id}")

proxy: RenaultHub
for proxy in hass.data[DOMAIN].values():
for vin, vehicle in proxy.vehicles.items():
loaded_entries: list[RenaultConfigEntry] = [
entry
for entry in hass.config_entries.async_entries(DOMAIN)
if entry.state == ConfigEntryState.LOADED
]
for entry in loaded_entries:
for vin, vehicle in entry.runtime_data.vehicles.items():
if (DOMAIN, vin) in device_entry.identifiers:
return vehicle
raise ValueError(f"Unable to find vehicle with VIN: {device_entry.identifiers}")
Expand Down
4 changes: 0 additions & 4 deletions tests/components/renault/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ async def test_setup_entry_bad_password(

assert len(hass.config_entries.async_entries(DOMAIN)) == 1
assert config_entry.state is ConfigEntryState.SETUP_ERROR
assert not hass.data.get(DOMAIN)


@pytest.mark.parametrize("side_effect", [aiohttp.ClientConnectionError, GigyaException])
Expand All @@ -76,7 +75,6 @@ async def test_setup_entry_exception(

assert len(hass.config_entries.async_entries(DOMAIN)) == 1
assert config_entry.state is ConfigEntryState.SETUP_RETRY
assert not hass.data.get(DOMAIN)


@pytest.mark.usefixtures("patch_renault_account")
Expand All @@ -95,7 +93,6 @@ async def test_setup_entry_kamereon_exception(

assert len(hass.config_entries.async_entries(DOMAIN)) == 1
assert config_entry.state is ConfigEntryState.SETUP_RETRY
assert not hass.data.get(DOMAIN)


@pytest.mark.usefixtures("patch_renault_account", "patch_get_vehicles")
Expand All @@ -111,4 +108,3 @@ async def test_setup_entry_missing_vehicle_details(

assert len(hass.config_entries.async_entries(DOMAIN)) == 1
assert config_entry.state is ConfigEntryState.SETUP_RETRY
assert not hass.data.get(DOMAIN)

0 comments on commit a440783

Please sign in to comment.