Skip to content

Commit

Permalink
Catch ClientConnectorError and TimeOutError in APSystems (#132027)
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas55555 authored and frenck committed Dec 24, 2024
1 parent c2f6e50 commit 7ce563b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
10 changes: 9 additions & 1 deletion homeassistant/components/apsystems/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from __future__ import annotations

from aiohttp import ClientConnectorError

from homeassistant.components.number import NumberDeviceClass, NumberEntity, NumberMode
from homeassistant.const import UnitOfPower
from homeassistant.core import HomeAssistant
Expand Down Expand Up @@ -45,7 +47,13 @@ def __init__(

async def async_update(self) -> None:
"""Set the state with the value fetched from the inverter."""
self._attr_native_value = await self._api.get_max_power()
try:
status = await self._api.get_max_power()
except (TimeoutError, ClientConnectorError):
self._attr_available = False
else:
self._attr_available = True
self._attr_native_value = status

async def async_set_native_value(self, value: float) -> None:
"""Set the desired output power."""
Expand Down
13 changes: 12 additions & 1 deletion tests/components/apsystems/test_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
DOMAIN as NUMBER_DOMAIN,
SERVICE_SET_VALUE,
)
from homeassistant.const import ATTR_ENTITY_ID, Platform
from homeassistant.const import ATTR_ENTITY_ID, STATE_UNAVAILABLE, Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er

Expand Down Expand Up @@ -46,6 +46,17 @@ async def test_number(
await hass.async_block_till_done()
state = hass.states.get(entity_id)
assert state.state == "50"
mock_apsystems.get_max_power.side_effect = TimeoutError()
await hass.services.async_call(
NUMBER_DOMAIN,
SERVICE_SET_VALUE,
service_data={ATTR_VALUE: 50.1},
target={ATTR_ENTITY_ID: entity_id},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get(entity_id)
assert state.state == STATE_UNAVAILABLE


@pytest.mark.usefixtures("mock_apsystems")
Expand Down

0 comments on commit 7ce563b

Please sign in to comment.