diff --git a/homeassistant/components/aemet/__init__.py b/homeassistant/components/aemet/__init__.py index bcddce5868c..13e636b2196 100644 --- a/homeassistant/components/aemet/__init__.py +++ b/homeassistant/components/aemet/__init__.py @@ -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() diff --git a/tests/components/aemet/test_init.py b/tests/components/aemet/test_init.py index 5055575e3fe..9389acf07c9 100644 --- a/tests/components/aemet/test_init.py +++ b/tests/components/aemet/test_init.py @@ -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