Skip to content

Commit 6a09474

Browse files
Thomas55555epenet
andauthored
Catch InverterReturnedError in APSystems (home-assistant#131930)
Co-authored-by: epenet <[email protected]>
1 parent e3885b8 commit 6a09474

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

homeassistant/components/apsystems/coordinator.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@
55
from dataclasses import dataclass
66
from datetime import timedelta
77

8-
from APsystemsEZ1 import APsystemsEZ1M, ReturnAlarmInfo, ReturnOutputData
8+
from APsystemsEZ1 import (
9+
APsystemsEZ1M,
10+
InverterReturnedError,
11+
ReturnAlarmInfo,
12+
ReturnOutputData,
13+
)
914

1015
from homeassistant.core import HomeAssistant
1116
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
1217

13-
from .const import LOGGER
18+
from .const import DOMAIN, LOGGER
1419

1520

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

4550
async def _async_update_data(self) -> ApSystemsSensorData:
46-
output_data = await self.api.get_output_data()
47-
alarm_info = await self.api.get_alarm_info()
51+
try:
52+
output_data = await self.api.get_output_data()
53+
alarm_info = await self.api.get_alarm_info()
54+
except InverterReturnedError:
55+
raise UpdateFailed(
56+
translation_domain=DOMAIN, translation_key="inverter_error"
57+
) from None
4858
return ApSystemsSensorData(output_data=output_data, alarm_info=alarm_info)

homeassistant/components/apsystems/strings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,10 @@
7272
"name": "Inverter status"
7373
}
7474
}
75+
},
76+
"exceptions": {
77+
"inverter_error": {
78+
"message": "Inverter returned an error"
79+
}
7580
}
7681
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""Test the APSystem setup."""
2+
3+
from unittest.mock import AsyncMock
4+
5+
from APsystemsEZ1 import InverterReturnedError
6+
7+
from homeassistant.components.apsystems.const import DOMAIN
8+
from homeassistant.config_entries import ConfigEntryState
9+
from homeassistant.core import HomeAssistant
10+
11+
from . import setup_integration
12+
13+
from tests.common import MockConfigEntry
14+
15+
16+
async def test_update_failed(
17+
hass: HomeAssistant,
18+
mock_apsystems: AsyncMock,
19+
mock_config_entry: MockConfigEntry,
20+
) -> None:
21+
"""Test update failed."""
22+
mock_apsystems.get_output_data.side_effect = InverterReturnedError
23+
await setup_integration(hass, mock_config_entry)
24+
entry = hass.config_entries.async_entries(DOMAIN)[0]
25+
assert entry.state is ConfigEntryState.SETUP_RETRY

0 commit comments

Comments
 (0)