Skip to content

Commit

Permalink
Catch InverterReturnedError in APSystems (home-assistant#131930)
Browse files Browse the repository at this point in the history
Co-authored-by: epenet <[email protected]>
  • Loading branch information
Thomas55555 and epenet authored Dec 3, 2024
1 parent e3885b8 commit 6a09474
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
18 changes: 14 additions & 4 deletions homeassistant/components/apsystems/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
from dataclasses import dataclass
from datetime import timedelta

from APsystemsEZ1 import APsystemsEZ1M, ReturnAlarmInfo, ReturnOutputData
from APsystemsEZ1 import (
APsystemsEZ1M,
InverterReturnedError,
ReturnAlarmInfo,
ReturnOutputData,
)

from homeassistant.core import HomeAssistant
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed

from .const import LOGGER
from .const import DOMAIN, LOGGER


@dataclass
Expand Down Expand Up @@ -43,6 +48,11 @@ async def _async_setup(self) -> None:
self.api.min_power = device_info.minPower

async def _async_update_data(self) -> ApSystemsSensorData:
output_data = await self.api.get_output_data()
alarm_info = await self.api.get_alarm_info()
try:
output_data = await self.api.get_output_data()
alarm_info = await self.api.get_alarm_info()
except InverterReturnedError:
raise UpdateFailed(
translation_domain=DOMAIN, translation_key="inverter_error"
) from None
return ApSystemsSensorData(output_data=output_data, alarm_info=alarm_info)
5 changes: 5 additions & 0 deletions homeassistant/components/apsystems/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,10 @@
"name": "Inverter status"
}
}
},
"exceptions": {
"inverter_error": {
"message": "Inverter returned an error"
}
}
}
25 changes: 25 additions & 0 deletions tests/components/apsystems/test_init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""Test the APSystem setup."""

from unittest.mock import AsyncMock

from APsystemsEZ1 import InverterReturnedError

from homeassistant.components.apsystems.const import DOMAIN
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant

from . import setup_integration

from tests.common import MockConfigEntry


async def test_update_failed(
hass: HomeAssistant,
mock_apsystems: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test update failed."""
mock_apsystems.get_output_data.side_effect = InverterReturnedError
await setup_integration(hass, mock_config_entry)
entry = hass.config_entries.async_entries(DOMAIN)[0]
assert entry.state is ConfigEntryState.SETUP_RETRY

0 comments on commit 6a09474

Please sign in to comment.