From c81d39f6512206da2a6668e4ed2a96b73e7607d9 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Tue, 29 Aug 2023 08:58:20 +0200 Subject: [PATCH] Fix Renault AssertionError (#99189) --- .../components/renault/renault_hub.py | 10 +++++++- .../fixtures/vehicle_missing_details.json | 25 +++++++++++++++++++ tests/components/renault/test_init.py | 16 ++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 tests/components/renault/fixtures/vehicle_missing_details.json diff --git a/homeassistant/components/renault/renault_hub.py b/homeassistant/components/renault/renault_hub.py index b7a9b40e2c9..49819dd919f 100644 --- a/homeassistant/components/renault/renault_hub.py +++ b/homeassistant/components/renault/renault_hub.py @@ -19,6 +19,7 @@ from homeassistant.const import ( ATTR_SW_VERSION, ) from homeassistant.core import HomeAssistant +from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.helpers import device_registry as dr from homeassistant.helpers.aiohttp_client import async_get_clientsession @@ -57,8 +58,15 @@ class RenaultHub: self._account = await self._client.get_api_account(account_id) vehicles = await self._account.get_vehicles() - device_registry = dr.async_get(self._hass) if vehicles.vehicleLinks: + if any( + vehicle_link.vehicleDetails is None + for vehicle_link in vehicles.vehicleLinks + ): + raise ConfigEntryNotReady( + "Failed to retrieve vehicle details from Renault servers" + ) + device_registry = dr.async_get(self._hass) await asyncio.gather( *( self.async_initialise_vehicle( diff --git a/tests/components/renault/fixtures/vehicle_missing_details.json b/tests/components/renault/fixtures/vehicle_missing_details.json new file mode 100644 index 00000000000..f6467e0c8f8 --- /dev/null +++ b/tests/components/renault/fixtures/vehicle_missing_details.json @@ -0,0 +1,25 @@ +{ + "accountId": "account-id-1", + "country": "FR", + "vehicleLinks": [ + { + "brand": "RENAULT", + "vin": "VF1AAAAA555777999", + "status": "ACTIVE", + "linkType": "OWNER", + "garageBrand": "RENAULT", + "annualMileage": 16000, + "mileage": 26464, + "startDate": "2017-08-07", + "createdDate": "2019-05-23T21:38:16.409008Z", + "lastModifiedDate": "2020-11-17T08:41:40.497400Z", + "ownershipStartDate": "2017-08-01", + "cancellationReason": {}, + "connectedDriver": { + "role": "MAIN_DRIVER", + "createdDate": "2019-06-17T09:49:06.880627Z", + "lastModifiedDate": "2019-06-17T09:49:06.880627Z" + } + } + ] +} diff --git a/tests/components/renault/test_init.py b/tests/components/renault/test_init.py index e1c782d06d5..0f26bf6fbdb 100644 --- a/tests/components/renault/test_init.py +++ b/tests/components/renault/test_init.py @@ -95,3 +95,19 @@ 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") +@pytest.mark.parametrize("vehicle_type", ["missing_details"], indirect=True) +async def test_setup_entry_missing_vehicle_details( + hass: HomeAssistant, config_entry: ConfigEntry +) -> 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). + await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + + assert len(hass.config_entries.async_entries(DOMAIN)) == 1 + assert config_entry.state is ConfigEntryState.SETUP_RETRY + assert not hass.data.get(DOMAIN)