Catch ClientConnectorError and TimeOutError in APSystems (#132027)

This commit is contained in:
Thomas55555 2024-12-23 22:49:59 +01:00 committed by Franck Nijhof
parent c2f6e5036e
commit 7ce563b0b4
No known key found for this signature in database
GPG Key ID: D62583BA8AB11CA3
2 changed files with 21 additions and 2 deletions

View File

@ -2,6 +2,8 @@
from __future__ import annotations from __future__ import annotations
from aiohttp import ClientConnectorError
from homeassistant.components.number import NumberDeviceClass, NumberEntity, NumberMode from homeassistant.components.number import NumberDeviceClass, NumberEntity, NumberMode
from homeassistant.const import UnitOfPower from homeassistant.const import UnitOfPower
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
@ -45,7 +47,13 @@ class ApSystemsMaxOutputNumber(ApSystemsEntity, NumberEntity):
async def async_update(self) -> None: async def async_update(self) -> None:
"""Set the state with the value fetched from the inverter.""" """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: async def async_set_native_value(self, value: float) -> None:
"""Set the desired output power.""" """Set the desired output power."""

View File

@ -12,7 +12,7 @@ from homeassistant.components.number import (
DOMAIN as NUMBER_DOMAIN, DOMAIN as NUMBER_DOMAIN,
SERVICE_SET_VALUE, 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.core import HomeAssistant
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
@ -46,6 +46,17 @@ async def test_number(
await hass.async_block_till_done() await hass.async_block_till_done()
state = hass.states.get(entity_id) state = hass.states.get(entity_id)
assert state.state == "50" 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") @pytest.mark.usefixtures("mock_apsystems")