From 873b078bb37f7421e44a5186be19e23dd0da37e8 Mon Sep 17 00:00:00 2001
From: Michael <35783820+mib1185@users.noreply.github.com>
Date: Sun, 29 Dec 2024 14:07:45 +0100
Subject: [PATCH] Make PEGELONLINE recoverable (#134199)

---
 .../components/pegel_online/__init__.py       |  7 +++++-
 tests/components/pegel_online/test_init.py    | 22 +++++++++++++++++++
 2 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/homeassistant/components/pegel_online/__init__.py b/homeassistant/components/pegel_online/__init__.py
index 2c465342493..30e5f4d2a38 100644
--- a/homeassistant/components/pegel_online/__init__.py
+++ b/homeassistant/components/pegel_online/__init__.py
@@ -5,10 +5,12 @@ from __future__ import annotations
 import logging
 
 from aiopegelonline import PegelOnline
+from aiopegelonline.const import CONNECT_ERRORS
 
 from homeassistant.config_entries import ConfigEntry
 from homeassistant.const import Platform
 from homeassistant.core import HomeAssistant
+from homeassistant.exceptions import ConfigEntryNotReady
 from homeassistant.helpers.aiohttp_client import async_get_clientsession
 
 from .const import CONF_STATION
@@ -28,7 +30,10 @@ async def async_setup_entry(hass: HomeAssistant, entry: PegelOnlineConfigEntry)
     _LOGGER.debug("Setting up station with uuid %s", station_uuid)
 
     api = PegelOnline(async_get_clientsession(hass))
-    station = await api.async_get_station_details(station_uuid)
+    try:
+        station = await api.async_get_station_details(station_uuid)
+    except CONNECT_ERRORS as err:
+        raise ConfigEntryNotReady("Failed to connect") from err
 
     coordinator = PegelOnlineDataUpdateCoordinator(hass, entry.title, api, station)
 
diff --git a/tests/components/pegel_online/test_init.py b/tests/components/pegel_online/test_init.py
index c1b8f1861c4..ac153193983 100644
--- a/tests/components/pegel_online/test_init.py
+++ b/tests/components/pegel_online/test_init.py
@@ -10,6 +10,7 @@ from homeassistant.components.pegel_online.const import (
     DOMAIN,
     MIN_TIME_BETWEEN_UPDATES,
 )
+from homeassistant.config_entries import ConfigEntryState
 from homeassistant.const import STATE_UNAVAILABLE
 from homeassistant.core import HomeAssistant
 from homeassistant.util import utcnow
@@ -24,6 +25,27 @@ from .const import (
 from tests.common import MockConfigEntry, async_fire_time_changed
 
 
+async def test_setup_error(
+    hass: HomeAssistant, caplog: pytest.LogCaptureFixture
+) -> None:
+    """Tests error during config entry setup."""
+    entry = MockConfigEntry(
+        domain=DOMAIN,
+        data=MOCK_CONFIG_ENTRY_DATA_DRESDEN,
+        unique_id=MOCK_CONFIG_ENTRY_DATA_DRESDEN[CONF_STATION],
+    )
+    entry.add_to_hass(hass)
+    with patch("homeassistant.components.pegel_online.PegelOnline") as pegelonline:
+        pegelonline.return_value = PegelOnlineMock(
+            station_details=MOCK_STATION_DETAILS_DRESDEN,
+            station_measurements=MOCK_STATION_MEASUREMENT_DRESDEN,
+        )
+        pegelonline().override_side_effect(ClientError("Boom"))
+        await hass.config_entries.async_setup(entry.entry_id)
+
+    assert entry.state is ConfigEntryState.SETUP_RETRY
+
+
 async def test_update_error(
     hass: HomeAssistant, caplog: pytest.LogCaptureFixture
 ) -> None:
-- 
GitLab