Handle button presses exceptions for Vodafone Station (#140953)

* Handle button presses execeptions for Vodafone Station

* apply review comment
This commit is contained in:
Simone Chemelli 2025-03-21 16:26:51 +01:00 committed by GitHub
parent f84a46680d
commit c1753631b1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 36 additions and 5 deletions

View File

@ -4,8 +4,16 @@ from __future__ import annotations
from collections.abc import Callable from collections.abc import Callable
from dataclasses import dataclass from dataclasses import dataclass
from json.decoder import JSONDecodeError
from typing import Any, Final from typing import Any, Final
from aiovodafone.exceptions import (
AlreadyLogged,
CannotAuthenticate,
CannotConnect,
GenericLoginError,
)
from homeassistant.components.button import ( from homeassistant.components.button import (
ButtonDeviceClass, ButtonDeviceClass,
ButtonEntity, ButtonEntity,
@ -13,10 +21,11 @@ from homeassistant.components.button import (
) )
from homeassistant.const import EntityCategory from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import _LOGGER from .const import _LOGGER, DOMAIN
from .coordinator import VodafoneConfigEntry, VodafoneStationRouter from .coordinator import VodafoneConfigEntry, VodafoneStationRouter
# Coordinator is used to centralize the data updates # Coordinator is used to centralize the data updates
@ -108,4 +117,25 @@ class VodafoneStationSensorEntity(
async def async_press(self) -> None: async def async_press(self) -> None:
"""Triggers the Shelly button press service.""" """Triggers the Shelly button press service."""
try:
await self.entity_description.press_action(self.coordinator) await self.entity_description.press_action(self.coordinator)
except CannotAuthenticate as err:
self.coordinator.config_entry.async_start_reauth(self.hass)
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="cannot_authenticate",
translation_placeholders={"error": repr(err)},
) from err
except (
CannotConnect,
AlreadyLogged,
GenericLoginError,
JSONDecodeError,
) as err:
self.coordinator.last_update_success = False
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="cannot_execute_action",
translation_placeholders={"error": repr(err)},
) from err

View File

@ -26,9 +26,7 @@ rules:
unique-config-entry: done unique-config-entry: done
# Silver # Silver
action-exceptions: action-exceptions: done
status: todo
comment: button presses not exception handled with HomeAssistantError
config-entry-unloading: done config-entry-unloading: done
docs-configuration-parameters: done docs-configuration-parameters: done
docs-installation-parameters: done docs-installation-parameters: done

View File

@ -116,6 +116,9 @@
"update_failed": { "update_failed": {
"message": "Error fetching data: {error}" "message": "Error fetching data: {error}"
}, },
"cannot_execute_action": {
"message": "Cannot execute requested action: {error}"
},
"cannot_authenticate": { "cannot_authenticate": {
"message": "Error authenticating: {error}" "message": "Error authenticating: {error}"
} }