From c1753631b174be54cb3dc17b8c2c0e68e51d48a0 Mon Sep 17 00:00:00 2001 From: Simone Chemelli Date: Fri, 21 Mar 2025 16:26:51 +0100 Subject: [PATCH] Handle button presses exceptions for Vodafone Station (#140953) * Handle button presses execeptions for Vodafone Station * apply review comment --- .../components/vodafone_station/button.py | 34 +++++++++++++++++-- .../vodafone_station/quality_scale.yaml | 4 +-- .../components/vodafone_station/strings.json | 3 ++ 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/vodafone_station/button.py b/homeassistant/components/vodafone_station/button.py index 5c98c3241e9..8dda4d49c7b 100644 --- a/homeassistant/components/vodafone_station/button.py +++ b/homeassistant/components/vodafone_station/button.py @@ -4,8 +4,16 @@ from __future__ import annotations from collections.abc import Callable from dataclasses import dataclass +from json.decoder import JSONDecodeError from typing import Any, Final +from aiovodafone.exceptions import ( + AlreadyLogged, + CannotAuthenticate, + CannotConnect, + GenericLoginError, +) + from homeassistant.components.button import ( ButtonDeviceClass, ButtonEntity, @@ -13,10 +21,11 @@ from homeassistant.components.button import ( ) from homeassistant.const import EntityCategory from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from homeassistant.helpers.update_coordinator import CoordinatorEntity -from .const import _LOGGER +from .const import _LOGGER, DOMAIN from .coordinator import VodafoneConfigEntry, VodafoneStationRouter # Coordinator is used to centralize the data updates @@ -108,4 +117,25 @@ class VodafoneStationSensorEntity( async def async_press(self) -> None: """Triggers the Shelly button press service.""" - await self.entity_description.press_action(self.coordinator) + + try: + 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 diff --git a/homeassistant/components/vodafone_station/quality_scale.yaml b/homeassistant/components/vodafone_station/quality_scale.yaml index f9fa27b3032..fe114b4b324 100644 --- a/homeassistant/components/vodafone_station/quality_scale.yaml +++ b/homeassistant/components/vodafone_station/quality_scale.yaml @@ -26,9 +26,7 @@ rules: unique-config-entry: done # Silver - action-exceptions: - status: todo - comment: button presses not exception handled with HomeAssistantError + action-exceptions: done config-entry-unloading: done docs-configuration-parameters: done docs-installation-parameters: done diff --git a/homeassistant/components/vodafone_station/strings.json b/homeassistant/components/vodafone_station/strings.json index de4bc364d4b..e05e1877798 100644 --- a/homeassistant/components/vodafone_station/strings.json +++ b/homeassistant/components/vodafone_station/strings.json @@ -116,6 +116,9 @@ "update_failed": { "message": "Error fetching data: {error}" }, + "cannot_execute_action": { + "message": "Cannot execute requested action: {error}" + }, "cannot_authenticate": { "message": "Error authenticating: {error}" }