From ae22ec48c48625160d30632005c616dad0ce267b Mon Sep 17 00:00:00 2001 From: markoceri Date: Wed, 25 Mar 2026 19:56:48 +0100 Subject: [PATCH] fix: enhance error handling for Home Assistant API connection and entity operations --- .../homeassistant/homeassistant_api.py | 70 ++++++++++++++++++- 1 file changed, 68 insertions(+), 2 deletions(-) diff --git a/edge_mining/adapters/infrastructure/homeassistant/homeassistant_api.py b/edge_mining/adapters/infrastructure/homeassistant/homeassistant_api.py index 5e35377..eed118d 100644 --- a/edge_mining/adapters/infrastructure/homeassistant/homeassistant_api.py +++ b/edge_mining/adapters/infrastructure/homeassistant/homeassistant_api.py @@ -16,6 +16,13 @@ from typing import Optional, Tuple from homeassistant_api import Client, Domain, Entity, Service +from homeassistant_api.errors import ( + EndpointNotFoundError, + HomeassistantAPIError, + RequestError, + RequestTimeoutError, + UnauthorizedError, +) from edge_mining.adapters.infrastructure.homeassistant.utils import ( STATE_SERVICE_MAP, @@ -75,6 +82,17 @@ def connect(self) -> None: self.client.get_config() if self.logger: self.logger.info("Successfully connected to Home Assistant API.") + except UnauthorizedError as e: + if self.logger: + self.logger.error( + "Home Assistant API authentication failed during connection. " + "Please verify your access token is valid." + ) + raise ConnectionError(f"Home Assistant authentication failed: {e}") from e + except (RequestError, HomeassistantAPIError) as e: + if self.logger: + self.logger.error(f"Home Assistant API error during connection: {e}") + raise ConnectionError(f"Home Assistant API error during connection: {e}") from e except Exception as e: if self.logger: self.logger.error(f"An unexpected error occurred connecting to Home Assistant: {e}") @@ -100,9 +118,9 @@ def is_connected(self) -> bool: if self.logger: self.logger.info("Successfully connected to Home Assistant API.") return True - except Exception as e: + except (Exception, HomeassistantAPIError) as e: if self.logger: - self.logger.error(f"Connection check to Home Assistant API failed: {e}") + self.logger.error(f"Home Assistant API connection check failed: {e}") return False def get_entity_state(self, entity_id: Optional[str]) -> Tuple[Optional[str], Optional[str]]: @@ -130,6 +148,30 @@ def get_entity_state(self, entity_id: Optional[str]) -> Tuple[Optional[str], Opt if self.logger: self.logger.debug(f"Fetched HA entity '{entity_id}': State='{state}', Unit='{unit}'") return state, unit + except EndpointNotFoundError: + if self.logger: + self.logger.error( + f"Home Assistant entity '{entity_id}' does not exist. " + "Please verify the entity ID is correct in your configuration." + ) + return None, None + except UnauthorizedError: + if self.logger: + self.logger.error( + "Home Assistant API authentication failed. " "Please verify your access token is valid." + ) + return None, None + except RequestTimeoutError: + if self.logger: + self.logger.error( + f"Home Assistant API request timed out while fetching entity '{entity_id}'. " + "The server may be overloaded or unreachable." + ) + return None, None + except (RequestError, HomeassistantAPIError) as e: + if self.logger: + self.logger.error(f"Home Assistant API error while fetching entity '{entity_id}': {e}") + return None, None except Exception as e: if self.logger: self.logger.error(f"Unexpected error getting Home Assistant entity '{entity_id}': {e}") @@ -210,6 +252,30 @@ def set_entity_state(self, entity_id: Optional[str], state: str) -> bool: self.logger.debug(f"Successfully set HA entity '{entity_id}' to state '{state}'.") return True + except EndpointNotFoundError: + if self.logger: + self.logger.error( + f"Home Assistant entity '{entity_id}' does not exist. " + "Please verify the entity ID is correct in your configuration." + ) + return False + except UnauthorizedError: + if self.logger: + self.logger.error( + "Home Assistant API authentication failed. " "Please verify your access token is valid." + ) + return False + except RequestTimeoutError: + if self.logger: + self.logger.error( + f"Home Assistant API request timed out while setting entity '{entity_id}'. " + "The server may be overloaded or unreachable." + ) + return False + except (RequestError, HomeassistantAPIError) as e: + if self.logger: + self.logger.error(f"Home Assistant API error while setting entity '{entity_id}': {e}") + return False except Exception as e: if self.logger: self.logger.error(f"Unexpected error setting Home Assistant entity '{entity_id}': {e}")