Raise UpdateFailed on fyta API error (#118318)

* Raise UpdateFailed

* Update homeassistant/components/fyta/coordinator.py

Co-authored-by: Robert Resch <robert@resch.dev>

* Remove logger

* simplify code

---------

Co-authored-by: Robert Resch <robert@resch.dev>
This commit is contained in:
dontinelli 2024-05-28 21:18:15 +02:00 committed by GitHub
parent 7f530ee0e4
commit 5eb1d72691
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 4 deletions

View File

@ -9,13 +9,14 @@ from fyta_cli.fyta_exceptions import (
FytaAuthentificationError,
FytaConnectionError,
FytaPasswordError,
FytaPlantError,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_ACCESS_TOKEN
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from .const import CONF_EXPIRATION
@ -48,7 +49,10 @@ class FytaCoordinator(DataUpdateCoordinator[dict[int, dict[str, Any]]]):
):
await self.renew_authentication()
return await self.fyta.update_all_plants()
try:
return await self.fyta.update_all_plants()
except (FytaConnectionError, FytaPlantError) as err:
raise UpdateFailed(err) from err
async def renew_authentication(self) -> bool:
"""Renew access token for FYTA API."""

View File

@ -4,7 +4,8 @@ from datetime import timedelta
from unittest.mock import AsyncMock
from freezegun.api import FrozenDateTimeFactory
from fyta_cli.fyta_exceptions import FytaConnectionError
from fyta_cli.fyta_exceptions import FytaConnectionError, FytaPlantError
import pytest
from syrupy import SnapshotAssertion
from homeassistant.const import STATE_UNAVAILABLE, Platform
@ -29,8 +30,16 @@ async def test_all_entities(
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
@pytest.mark.parametrize(
"exception",
[
FytaConnectionError,
FytaPlantError,
],
)
async def test_connection_error(
hass: HomeAssistant,
exception: Exception,
mock_fyta_connector: AsyncMock,
mock_config_entry: MockConfigEntry,
freezer: FrozenDateTimeFactory,
@ -38,7 +47,7 @@ async def test_connection_error(
"""Test connection error."""
await setup_platform(hass, mock_config_entry, [Platform.SENSOR])
mock_fyta_connector.update_all_plants.side_effect = FytaConnectionError
mock_fyta_connector.update_all_plants.side_effect = exception
freezer.tick(delta=timedelta(hours=12))
async_fire_time_changed(hass)