mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 18:57:06 +00:00
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:
parent
7f530ee0e4
commit
5eb1d72691
@ -9,13 +9,14 @@ from fyta_cli.fyta_exceptions import (
|
|||||||
FytaAuthentificationError,
|
FytaAuthentificationError,
|
||||||
FytaConnectionError,
|
FytaConnectionError,
|
||||||
FytaPasswordError,
|
FytaPasswordError,
|
||||||
|
FytaPlantError,
|
||||||
)
|
)
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import CONF_ACCESS_TOKEN
|
from homeassistant.const import CONF_ACCESS_TOKEN
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
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
|
from .const import CONF_EXPIRATION
|
||||||
|
|
||||||
@ -48,7 +49,10 @@ class FytaCoordinator(DataUpdateCoordinator[dict[int, dict[str, Any]]]):
|
|||||||
):
|
):
|
||||||
await self.renew_authentication()
|
await self.renew_authentication()
|
||||||
|
|
||||||
|
try:
|
||||||
return await self.fyta.update_all_plants()
|
return await self.fyta.update_all_plants()
|
||||||
|
except (FytaConnectionError, FytaPlantError) as err:
|
||||||
|
raise UpdateFailed(err) from err
|
||||||
|
|
||||||
async def renew_authentication(self) -> bool:
|
async def renew_authentication(self) -> bool:
|
||||||
"""Renew access token for FYTA API."""
|
"""Renew access token for FYTA API."""
|
||||||
|
@ -4,7 +4,8 @@ from datetime import timedelta
|
|||||||
from unittest.mock import AsyncMock
|
from unittest.mock import AsyncMock
|
||||||
|
|
||||||
from freezegun.api import FrozenDateTimeFactory
|
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 syrupy import SnapshotAssertion
|
||||||
|
|
||||||
from homeassistant.const import STATE_UNAVAILABLE, Platform
|
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)
|
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"exception",
|
||||||
|
[
|
||||||
|
FytaConnectionError,
|
||||||
|
FytaPlantError,
|
||||||
|
],
|
||||||
|
)
|
||||||
async def test_connection_error(
|
async def test_connection_error(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
|
exception: Exception,
|
||||||
mock_fyta_connector: AsyncMock,
|
mock_fyta_connector: AsyncMock,
|
||||||
mock_config_entry: MockConfigEntry,
|
mock_config_entry: MockConfigEntry,
|
||||||
freezer: FrozenDateTimeFactory,
|
freezer: FrozenDateTimeFactory,
|
||||||
@ -38,7 +47,7 @@ async def test_connection_error(
|
|||||||
"""Test connection error."""
|
"""Test connection error."""
|
||||||
await setup_platform(hass, mock_config_entry, [Platform.SENSOR])
|
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))
|
freezer.tick(delta=timedelta(hours=12))
|
||||||
async_fire_time_changed(hass)
|
async_fire_time_changed(hass)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user