Handle http error in Renault initialisation (#97530)

This commit is contained in:
epenet 2023-07-31 12:17:51 +02:00 committed by GitHub
parent ed3ebdfea5
commit 927905ac84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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() raise ConfigEntryAuthFailed()
hass.data.setdefault(DOMAIN, {}) 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 hass.data[DOMAIN][config_entry.entry_id] = renault_hub

View File

@ -1,7 +1,7 @@
"""Tests for Renault setup process.""" """Tests for Renault setup process."""
from collections.abc import Generator from collections.abc import Generator
from typing import Any from typing import Any
from unittest.mock import patch from unittest.mock import Mock, patch
import aiohttp import aiohttp
import pytest import pytest
@ -76,3 +76,22 @@ async def test_setup_entry_exception(
assert len(hass.config_entries.async_entries(DOMAIN)) == 1 assert len(hass.config_entries.async_entries(DOMAIN)) == 1
assert config_entry.state is ConfigEntryState.SETUP_RETRY assert config_entry.state is ConfigEntryState.SETUP_RETRY
assert not hass.data.get(DOMAIN) 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)