From 5eb1d72691a0c8e8be07c0d5fbc7d3fdbcfc2f08 Mon Sep 17 00:00:00 2001 From: dontinelli <73341522+dontinelli@users.noreply.github.com> Date: Tue, 28 May 2024 21:18:15 +0200 Subject: [PATCH] Raise UpdateFailed on fyta API error (#118318) * Raise UpdateFailed * Update homeassistant/components/fyta/coordinator.py Co-authored-by: Robert Resch * Remove logger * simplify code --------- Co-authored-by: Robert Resch --- homeassistant/components/fyta/coordinator.py | 8 ++++++-- tests/components/fyta/test_sensor.py | 13 +++++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/fyta/coordinator.py b/homeassistant/components/fyta/coordinator.py index 021bddf2cf8..db79f21eb53 100644 --- a/homeassistant/components/fyta/coordinator.py +++ b/homeassistant/components/fyta/coordinator.py @@ -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.""" diff --git a/tests/components/fyta/test_sensor.py b/tests/components/fyta/test_sensor.py index 0c73cbd41d2..e33c54695e5 100644 --- a/tests/components/fyta/test_sensor.py +++ b/tests/components/fyta/test_sensor.py @@ -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)