Improve reliability of homeassistant_alerts updates (#115974)

This commit is contained in:
J. Nick Koston 2024-04-22 16:51:19 +02:00 committed by GitHub
parent 65b2c1519c
commit 37d329c286
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -20,7 +20,7 @@ from homeassistant.helpers.issue_registry import (
async_create_issue, async_create_issue,
async_delete_issue, async_delete_issue,
) )
from homeassistant.helpers.start import async_at_start from homeassistant.helpers.start import async_at_started
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from homeassistant.setup import EventComponentLoaded from homeassistant.setup import EventComponentLoaded
@ -30,6 +30,8 @@ DOMAIN = "homeassistant_alerts"
UPDATE_INTERVAL = timedelta(hours=3) UPDATE_INTERVAL = timedelta(hours=3)
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
REQUEST_TIMEOUT = aiohttp.ClientTimeout(total=30)
CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN) CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN)
@ -52,7 +54,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
try: try:
response = await async_get_clientsession(hass).get( response = await async_get_clientsession(hass).get(
f"https://alerts.home-assistant.io/alerts/{alert.alert_id}.json", f"https://alerts.home-assistant.io/alerts/{alert.alert_id}.json",
timeout=aiohttp.ClientTimeout(total=30), timeout=REQUEST_TIMEOUT,
) )
except TimeoutError: except TimeoutError:
_LOGGER.warning("Error fetching %s: timeout", alert.filename) _LOGGER.warning("Error fetching %s: timeout", alert.filename)
@ -106,7 +108,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
await coordinator.async_refresh() await coordinator.async_refresh()
hass.bus.async_listen(EVENT_COMPONENT_LOADED, _component_loaded) hass.bus.async_listen(EVENT_COMPONENT_LOADED, _component_loaded)
async_at_start(hass, initial_refresh) async_at_started(hass, initial_refresh)
return True return True
@ -146,7 +148,7 @@ class AlertUpdateCoordinator(DataUpdateCoordinator[dict[str, IntegrationAlert]])
async def _async_update_data(self) -> dict[str, IntegrationAlert]: async def _async_update_data(self) -> dict[str, IntegrationAlert]:
response = await async_get_clientsession(self.hass).get( response = await async_get_clientsession(self.hass).get(
"https://alerts.home-assistant.io/alerts.json", "https://alerts.home-assistant.io/alerts.json",
timeout=aiohttp.ClientTimeout(total=10), timeout=REQUEST_TIMEOUT,
) )
alerts = await response.json() alerts = await response.json()