Handle http error in Renault initialisation (#97530)

pull/97609/head
epenet 2023-07-31 12:17:51 +02:00 committed by Franck Nijhof
parent e473131a2c
commit ec1b24f8d6
No known key found for this signature in database
GPG Key ID: D62583BA8AB11CA3
2 changed files with 24 additions and 2 deletions

View File

@ -26,7 +26,10 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
raise ConfigEntryAuthFailed()
hass.data.setdefault(DOMAIN, {})
await renault_hub.async_initialise(config_entry)
try:
await renault_hub.async_initialise(config_entry)
except aiohttp.ClientResponseError as exc:
raise ConfigEntryNotReady() from exc
hass.data[DOMAIN][config_entry.entry_id] = renault_hub

View File

@ -1,7 +1,7 @@
"""Tests for Renault setup process."""
from collections.abc import Generator
from typing import Any
from unittest.mock import patch
from unittest.mock import Mock, patch
import aiohttp
import pytest
@ -76,3 +76,22 @@ 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")
async def test_setup_entry_kamereon_exception(
hass: HomeAssistant, config_entry: ConfigEntry
) -> None:
"""Test ConfigEntryNotReady when API raises an exception during entry setup."""
# In this case we are testing the condition where renault_hub fails to retrieve
# list of vehicles (see Gateway Time-out on #97324).
with patch(
"renault_api.renault_client.RenaultClient.get_api_account",
side_effect=aiohttp.ClientResponseError(Mock(), (), status=504),
):
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)