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:
Mr. Bubbles 2024-06-28 20:11:03 +02:00 committed by GitHub
parent c029c534d6
commit 4fb0621027
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 151 additions and 7 deletions

View file

@ -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