Handle timeouts on AEMET init (#102289)

Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
This commit is contained in:
Álvaro Fernández Rojas 2023-10-19 10:42:31 +02:00 committed by GitHub
parent d00934a8f8
commit 7ec2496c81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 0 deletions

View File

@ -1,5 +1,6 @@
"""The AEMET OpenData component."""
import asyncio
import logging
from aemet_opendata.exceptions import TownNotFound
@ -8,6 +9,7 @@ from aemet_opendata.interface import AEMET, ConnectionOptions
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import aiohttp_client
from .const import (
@ -37,6 +39,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
except TownNotFound as err:
_LOGGER.error(err)
return False
except asyncio.TimeoutError as err:
raise ConfigEntryNotReady("AEMET OpenData API timed out") from err
weather_coordinator = WeatherUpdateCoordinator(hass, aemet)
await weather_coordinator.async_config_entry_first_refresh()

View File

@ -1,4 +1,5 @@
"""Define tests for the AEMET OpenData init."""
import asyncio
from unittest.mock import patch
from freezegun.api import FrozenDateTimeFactory
@ -70,3 +71,29 @@ async def test_init_town_not_found(
config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id) is False
async def test_init_api_timeout(
hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test API timeouts when loading the AEMET integration."""
hass.config.set_time_zone("UTC")
freezer.move_to("2021-01-09 12:00:00+00:00")
with patch(
"homeassistant.components.aemet.AEMET.api_call",
side_effect=asyncio.TimeoutError,
):
config_entry = MockConfigEntry(
domain=DOMAIN,
data={
CONF_API_KEY: "api-key",
CONF_LATITUDE: "0.0",
CONF_LONGITUDE: "0.0",
CONF_NAME: "AEMET",
},
)
config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id) is False