mirror of
https://github.com/home-assistant/core.git
synced 2026-12-03 04:08:19 -04:00
Fetch missing Renault vehicle details (#186402)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
"""Proxy to handle account communication with Renault servers."""
|
||||
|
||||
import asyncio
|
||||
import contextlib
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
from time import time
|
||||
@@ -8,6 +9,7 @@ from typing import TYPE_CHECKING
|
||||
|
||||
from renault_api.exceptions import NotAuthenticatedException
|
||||
from renault_api.gigya.exceptions import InvalidCredentialsException
|
||||
from renault_api.kamereon.exceptions import KamereonResponseException
|
||||
from renault_api.kamereon.models import KamereonVehiclesLink
|
||||
from renault_api.renault_account import RenaultAccount
|
||||
from renault_api.renault_client import RenaultClient
|
||||
@@ -38,15 +40,21 @@ LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def _get_filtered_vehicles(account: RenaultAccount) -> list[KamereonVehiclesLink]:
|
||||
"""Filter out vehicles with missing details.
|
||||
"""Get the vehicles, filtering out those with missing details.
|
||||
|
||||
May be due to new purchases, or issue with the Renault servers.
|
||||
The vehicle list sometimes omits the details, so they are requested
|
||||
separately. They may still be missing for new purchases, or due to an
|
||||
issue with the Renault servers.
|
||||
"""
|
||||
vehicles = await account.get_vehicles()
|
||||
if not vehicles.vehicleLinks:
|
||||
return []
|
||||
result: list[KamereonVehiclesLink] = []
|
||||
for link in vehicles.vehicleLinks:
|
||||
if link.vehicleDetails is None and link.vin:
|
||||
vehicle = await account.get_api_vehicle(link.vin)
|
||||
with contextlib.suppress(KamereonResponseException):
|
||||
link.vehicleDetails = await vehicle.get_details()
|
||||
if link.vehicleDetails is None:
|
||||
LOGGER.warning("Ignoring vehicle with missing details: %s", link.vin)
|
||||
continue
|
||||
|
||||
@@ -28,6 +28,18 @@ def mock_setup_entry() -> Generator[AsyncMock]:
|
||||
yield mock_setup_entry
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def mock_get_vehicle_details() -> Generator[AsyncMock]:
|
||||
"""Fail the separate request for details missing from the vehicle list."""
|
||||
with patch(
|
||||
"renault_api.renault_vehicle.RenaultVehicle.get_details",
|
||||
side_effect=exceptions.ResourceNotFoundException(
|
||||
"err.func.wired.notFound", "Resource not found"
|
||||
),
|
||||
) as mock_get_details:
|
||||
yield mock_get_details
|
||||
|
||||
|
||||
@pytest.fixture(name="vehicle_type", params=MOCK_VEHICLES.keys())
|
||||
def get_vehicle_type(request: pytest.FixtureRequest) -> str:
|
||||
"""Parametrize vehicle type."""
|
||||
|
||||
@@ -164,6 +164,54 @@ async def test_config_flow_no_account(
|
||||
assert len(mock_setup_entry.mock_calls) == 0
|
||||
|
||||
|
||||
async def test_config_flow_missing_vehicle_details(
|
||||
hass: HomeAssistant, mock_setup_entry: AsyncMock
|
||||
) -> None:
|
||||
"""Test vehicle details missing from the vehicle list are requested separately."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
renault_vehicles = schemas.KamereonVehiclesResponseSchema.loads(
|
||||
await async_load_fixture(hass, "vehicle_zoe_40.json", DOMAIN)
|
||||
)
|
||||
renault_vehicles.vehicleLinks = renault_vehicles.vehicleLinks[:1]
|
||||
vehicle_details = renault_vehicles.vehicleLinks[0].vehicleDetails
|
||||
renault_vehicles.vehicleLinks[0].vehicleDetails = None
|
||||
renault_account = AsyncMock()
|
||||
type(renault_account).account_id = PropertyMock(return_value="account_id_1")
|
||||
renault_account.get_vehicles.return_value = renault_vehicles
|
||||
renault_account.get_api_vehicle.return_value.get_details.return_value = (
|
||||
vehicle_details
|
||||
)
|
||||
|
||||
with (
|
||||
patch(
|
||||
"renault_api.renault_session.RenaultSession.login",
|
||||
autospec=True,
|
||||
side_effect=_mock_login,
|
||||
),
|
||||
patch(
|
||||
"renault_api.renault_client.RenaultClient.get_api_accounts",
|
||||
return_value=[renault_account],
|
||||
),
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={
|
||||
RenaultConfigurationKeys.LOCALE: "fr_FR",
|
||||
RenaultConfigurationKeys.USERNAME: "email@test.com",
|
||||
RenaultConfigurationKeys.PASSWORD: "test",
|
||||
},
|
||||
)
|
||||
|
||||
renault_account.get_api_vehicle.assert_awaited_once_with("VF1ZOE40VIN")
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["result"].unique_id == "account_id_1"
|
||||
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_config_flow_multiple_accounts(
|
||||
hass: HomeAssistant, mock_setup_entry: AsyncMock
|
||||
) -> None:
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"""Tests for Renault setup process."""
|
||||
|
||||
from collections.abc import Generator
|
||||
from unittest.mock import Mock, patch
|
||||
from unittest.mock import AsyncMock, Mock, patch
|
||||
|
||||
import aiohttp
|
||||
import pytest
|
||||
@@ -283,14 +283,16 @@ async def test_account_not_found_issue_deleted_on_removal(
|
||||
@pytest.mark.usefixtures("patch_renault_account", "patch_get_vehicles")
|
||||
@pytest.mark.parametrize("vehicle_type", ["missing_details"], indirect=True)
|
||||
async def test_setup_entry_missing_vehicle_details(
|
||||
hass: HomeAssistant, config_entry: ConfigEntry
|
||||
hass: HomeAssistant, config_entry: ConfigEntry, mock_get_vehicle_details: AsyncMock
|
||||
) -> None:
|
||||
"""Test ConfigEntryNotReady when vehicleDetails is missing."""
|
||||
# In this case we are testing the condition where renault_hub fails to retrieve
|
||||
# vehicle details (see #99127).
|
||||
# vehicle details (see #99127), even when requested separately.
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Requested for each vehicle without details in the vehicle list.
|
||||
assert mock_get_vehicle_details.await_count == 2
|
||||
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
|
||||
assert config_entry.state is ConfigEntryState.SETUP_RETRY
|
||||
|
||||
|
||||
Reference in New Issue
Block a user