Translate UpdateFailed exception in PEGELONLINE (#131380)

translate UpdateFailed exception
This commit is contained in:
Michael 2024-11-23 17:59:41 +01:00 committed by GitHub
parent fa1b7d73d5
commit 7b70f2d83b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 18 additions and 4 deletions

View File

@ -7,7 +7,7 @@ from aiopegelonline import CONNECT_ERRORS, PegelOnline, Station, StationMeasurem
from homeassistant.core import HomeAssistant
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from .const import MIN_TIME_BETWEEN_UPDATES
from .const import DOMAIN, MIN_TIME_BETWEEN_UPDATES
_LOGGER = logging.getLogger(__name__)
@ -33,4 +33,8 @@ class PegelOnlineDataUpdateCoordinator(DataUpdateCoordinator[StationMeasurements
try:
return await self.api.async_get_station_measurements(self.station.uuid)
except CONNECT_ERRORS as err:
raise UpdateFailed(f"Failed to communicate with API: {err}") from err
raise UpdateFailed(
translation_domain=DOMAIN,
translation_key="communication_error",
translation_placeholders={"error": str(err)},
) from err

View File

@ -48,5 +48,10 @@
"name": "Water temperature"
}
}
},
"exceptions": {
"communication_error": {
"message": "Failed to communicate with API: {error}"
}
}
}

View File

@ -3,6 +3,7 @@
from unittest.mock import patch
from aiohttp.client_exceptions import ClientError
import pytest
from homeassistant.components.pegel_online.const import (
CONF_STATION,
@ -23,7 +24,9 @@ from .const import (
from tests.common import MockConfigEntry, async_fire_time_changed
async def test_update_error(hass: HomeAssistant) -> None:
async def test_update_error(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Tests error during update entity."""
entry = MockConfigEntry(
domain=DOMAIN,
@ -43,9 +46,11 @@ async def test_update_error(hass: HomeAssistant) -> None:
state = hass.states.get("sensor.dresden_elbe_water_level")
assert state
pegelonline().override_side_effect(ClientError)
pegelonline().override_side_effect(ClientError("Boom"))
async_fire_time_changed(hass, utcnow() + MIN_TIME_BETWEEN_UPDATES)
await hass.async_block_till_done()
assert "Failed to communicate with API: Boom" in caplog.text
state = hass.states.get("sensor.dresden_elbe_water_level")
assert state.state == STATE_UNAVAILABLE