Catch exceptions in service calls by buttons/switches in pyLoad integration (#120701)
* Catch exceptions in service calls by buttons/switches * changes * more changes * update tests
This commit is contained in:
parent
c029c534d6
commit
4fb0621027
5 changed files with 151 additions and 7 deletions
|
@ -7,13 +7,15 @@ from dataclasses import dataclass
|
|||
from enum import StrEnum
|
||||
from typing import Any
|
||||
|
||||
from pyloadapi.api import PyLoadAPI
|
||||
from pyloadapi.api import CannotConnect, InvalidAuth, PyLoadAPI
|
||||
|
||||
from homeassistant.components.button import ButtonEntity, ButtonEntityDescription
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ServiceValidationError
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import PyLoadConfigEntry
|
||||
from .const import DOMAIN
|
||||
from .entity import BasePyLoadEntity
|
||||
|
||||
|
||||
|
@ -80,4 +82,15 @@ class PyLoadBinarySensor(BasePyLoadEntity, ButtonEntity):
|
|||
|
||||
async def async_press(self) -> None:
|
||||
"""Handle the button press."""
|
||||
await self.entity_description.press_fn(self.coordinator.pyload)
|
||||
try:
|
||||
await self.entity_description.press_fn(self.coordinator.pyload)
|
||||
except CannotConnect as e:
|
||||
raise ServiceValidationError(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="service_call_exception",
|
||||
) from e
|
||||
except InvalidAuth as e:
|
||||
raise ServiceValidationError(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="service_call_auth_exception",
|
||||
) from e
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue