Reolink improve error handeling (#104301)

* Raise proper HomeAssistantError

* fix styling

* Use ServiceValidationError
This commit is contained in:
starkillerOG 2023-11-21 17:21:48 +01:00 committed by GitHub
parent dece6c8042
commit 6d529a82d7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 92 additions and 23 deletions

View file

@ -6,6 +6,7 @@ from dataclasses import dataclass
from typing import Any
from reolink_aio.api import Host
from reolink_aio.exceptions import InvalidParameterError, ReolinkError
from homeassistant.components.light import (
ATTR_BRIGHTNESS,
@ -16,6 +17,7 @@ from homeassistant.components.light import (
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import ReolinkData
@ -129,9 +131,12 @@ class ReolinkLightEntity(ReolinkChannelCoordinatorEntity, LightEntity):
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn light off."""
await self.entity_description.turn_on_off_fn(
self._host.api, self._channel, False
)
try:
await self.entity_description.turn_on_off_fn(
self._host.api, self._channel, False
)
except ReolinkError as err:
raise HomeAssistantError(err) from err
self.async_write_ha_state()
async def async_turn_on(self, **kwargs: Any) -> None:
@ -140,11 +145,19 @@ class ReolinkLightEntity(ReolinkChannelCoordinatorEntity, LightEntity):
brightness := kwargs.get(ATTR_BRIGHTNESS)
) is not None and self.entity_description.set_brightness_fn is not None:
brightness_pct = int(brightness / 255.0 * 100)
await self.entity_description.set_brightness_fn(
self._host.api, self._channel, brightness_pct
)
try:
await self.entity_description.set_brightness_fn(
self._host.api, self._channel, brightness_pct
)
except InvalidParameterError as err:
raise ServiceValidationError(err) from err
except ReolinkError as err:
raise HomeAssistantError(err) from err
await self.entity_description.turn_on_off_fn(
self._host.api, self._channel, True
)
try:
await self.entity_description.turn_on_off_fn(
self._host.api, self._channel, True
)
except ReolinkError as err:
raise HomeAssistantError(err) from err
self.async_write_ha_state()