From 8edbf88da1d0b9fd19a750e8b1a23db1ca6b0b93 Mon Sep 17 00:00:00 2001
From: Jan Bouwhuis <jbouwh@users.noreply.github.com>
Date: Thu, 21 Mar 2024 06:53:26 +0100
Subject: [PATCH] Fetch MaxLengthExceeded exception mesage from the translation
 cache (#113904)

* Fetch MaxLengthExceeded exception mesage from the translation cache

* Update homeassistant/components/homeassistant/strings.json

Co-authored-by: J. Nick Koston <nick@koston.org>

* Add case without homeassistant integration

* Fix test

---------

Co-authored-by: J. Nick Koston <nick@koston.org>
---
 .../components/homeassistant/strings.json      |  3 +++
 homeassistant/exceptions.py                    | 12 ++++++++----
 tests/test_core.py                             | 18 ++++++++++++++++++
 3 files changed, 29 insertions(+), 4 deletions(-)

diff --git a/homeassistant/components/homeassistant/strings.json b/homeassistant/components/homeassistant/strings.json
index 0b20f8698c2..016f3e0580d 100644
--- a/homeassistant/components/homeassistant/strings.json
+++ b/homeassistant/components/homeassistant/strings.json
@@ -174,6 +174,9 @@
     "integration_config_error": {
       "message": "Failed to process config for integration {domain} due to multiple ({errors}) errors. Check the logs for more information."
     },
+    "max_length_exceeded": {
+      "message": "Value {value} for property {property_name} has a maximum length of {max_length} characters."
+    },
     "platform_component_load_err": {
       "message": "Platform error: {domain} - {error}. Check the logs for more information."
     },
diff --git a/homeassistant/exceptions.py b/homeassistant/exceptions.py
index 63b95b570e7..e94d5fd6b57 100644
--- a/homeassistant/exceptions.py
+++ b/homeassistant/exceptions.py
@@ -281,14 +281,18 @@ class MaxLengthExceeded(HomeAssistantError):
         """Initialize error."""
         super().__init__(
             self,
-            (
-                f"Value {value} for property {property_name} has a max length of "
-                f"{max_length} characters"
-            ),
+            translation_domain="homeassistant",
+            translation_key="max_length_exceeded",
+            translation_placeholders={
+                "value": value,
+                "property_name": property_name,
+                "max_length": str(max_length),
+            },
         )
         self.value = value
         self.property_name = property_name
         self.max_length = max_length
+        self.generate_message = True
 
 
 class DependencyError(HomeAssistantError):
diff --git a/tests/test_core.py b/tests/test_core.py
index b60e6b832ce..ad67adb78b9 100644
--- a/tests/test_core.py
+++ b/tests/test_core.py
@@ -58,6 +58,7 @@ from homeassistant.exceptions import (
     ServiceNotFound,
 )
 from homeassistant.helpers.json import json_dumps
+from homeassistant.setup import async_setup_component
 from homeassistant.util.async_ import create_eager_task
 import homeassistant.util.dt as dt_util
 from homeassistant.util.read_only_dict import ReadOnlyDict
@@ -1314,9 +1315,26 @@ async def test_eventbus_max_length_exceeded(hass: HomeAssistant) -> None:
         "this_event_exceeds_the_max_character_length_even_with_the_new_limit"
     )
 
+    # Without cached translations the translation key is returned
     with pytest.raises(MaxLengthExceeded) as exc_info:
         hass.bus.async_fire(long_evt_name)
 
+    assert str(exc_info.value) == "max_length_exceeded"
+    assert exc_info.value.property_name == "event_type"
+    assert exc_info.value.max_length == 64
+    assert exc_info.value.value == long_evt_name
+
+    # Fetch translations
+    await async_setup_component(hass, "homeassistant", {})
+
+    # With cached translations the formatted message is returned
+    with pytest.raises(MaxLengthExceeded) as exc_info:
+        hass.bus.async_fire(long_evt_name)
+
+    assert (
+        str(exc_info.value)
+        == f"Value {long_evt_name} for property event_type has a maximum length of 64 characters"
+    )
     assert exc_info.value.property_name == "event_type"
     assert exc_info.value.max_length == 64
     assert exc_info.value.value == long_evt_name