From 45022752a0aea4587aab6e700e4f370eed108f4a Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 17 Apr 2025 22:22:08 -1000 Subject: [PATCH] Make remaining ESPHome exceptions translatable (#143184) --- homeassistant/components/esphome/entity.py | 9 ++++++++- homeassistant/components/esphome/strings.json | 9 +++++++++ homeassistant/components/esphome/update.py | 20 +++++++++++++------ 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/esphome/entity.py b/homeassistant/components/esphome/entity.py index cace3a701cd..b28decc7c70 100644 --- a/homeassistant/components/esphome/entity.py +++ b/homeassistant/components/esphome/entity.py @@ -28,6 +28,8 @@ from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity_platform import AddEntitiesCallback +from .const import DOMAIN + # Import config flow so that it's added to the registry from .entry_data import ESPHomeConfigEntry, RuntimeEntryData from .enum_mapper import EsphomeEnumMapper @@ -167,7 +169,12 @@ def convert_api_error_ha_error[**_P, _R, _EntityT: EsphomeEntity[Any, Any]]( return await func(self, *args, **kwargs) except APIConnectionError as error: raise HomeAssistantError( - f"Error communicating with device: {error}" + translation_domain=DOMAIN, + translation_key="error_communicating_with_device", + translation_placeholders={ + "device_name": self._device_info.name, + "error": str(error), + }, ) from error return handler diff --git a/homeassistant/components/esphome/strings.json b/homeassistant/components/esphome/strings.json index bfbedba5a70..e265620d2e4 100644 --- a/homeassistant/components/esphome/strings.json +++ b/homeassistant/components/esphome/strings.json @@ -184,6 +184,15 @@ "exceptions": { "action_call_failed": { "message": "Failed to execute the action call {call_name} on {device_name}: {error}" + }, + "error_communicating_with_device": { + "message": "Error communicating with the device {device_name}: {error}" + }, + "error_compiling": { + "message": "Error compiling {configuration}; Try again in ESPHome dashboard for more information." + }, + "error_uploading": { + "message": "Error during OTA of {configuration}; Try again in ESPHome dashboard for more information." } } } diff --git a/homeassistant/components/esphome/update.py b/homeassistant/components/esphome/update.py index 112e3ecde9d..9125e92a552 100644 --- a/homeassistant/components/esphome/update.py +++ b/homeassistant/components/esphome/update.py @@ -26,6 +26,7 @@ from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.util.enum import try_parse_enum +from .const import DOMAIN from .coordinator import ESPHomeDashboardCoordinator from .dashboard import async_get_dashboard from .domain_data import DomainData @@ -201,16 +202,23 @@ class ESPHomeDashboardUpdateEntity( api = coordinator.api device = coordinator.data.get(self._device_info.name) assert device is not None + configuration = device["configuration"] try: - if not await api.compile(device["configuration"]): + if not await api.compile(configuration): raise HomeAssistantError( - f"Error compiling {device['configuration']}; " - "Try again in ESPHome dashboard for more information." + translation_domain=DOMAIN, + translation_key="error_compiling", + translation_placeholders={ + "configuration": configuration, + }, ) - if not await api.upload(device["configuration"], "OTA"): + if not await api.upload(configuration, "OTA"): raise HomeAssistantError( - f"Error updating {device['configuration']} via OTA; " - "Try again in ESPHome dashboard for more information." + translation_domain=DOMAIN, + translation_key="error_uploading", + translation_placeholders={ + "configuration": configuration, + }, ) finally: await self.coordinator.async_request_refresh()