From d9eb419ecc607ab412cfd286d05042d7399aaa0c Mon Sep 17 00:00:00 2001 From: Josef Zweck <24647999+zweckj@users.noreply.github.com> Date: Fri, 27 Sep 2024 22:21:01 +0200 Subject: [PATCH] Add translation for tedee exceptions (#126963) --- homeassistant/components/tedee/lock.py | 13 ++++++++++--- homeassistant/components/tedee/strings.json | 11 +++++++++++ tests/components/tedee/test_lock.py | 13 ++++++------- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/tedee/lock.py b/homeassistant/components/tedee/lock.py index 8d5fa028e12..8f0587de8ae 100644 --- a/homeassistant/components/tedee/lock.py +++ b/homeassistant/components/tedee/lock.py @@ -10,6 +10,7 @@ from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.entity_platform import AddEntitiesCallback from . import TedeeConfigEntry +from .const import DOMAIN from .coordinator import TedeeApiCoordinator from .entity import TedeeEntity @@ -108,7 +109,9 @@ class TedeeLockEntity(TedeeEntity, LockEntity): await self.coordinator.async_request_refresh() except (TedeeClientException, Exception) as ex: raise HomeAssistantError( - f"Failed to unlock the door. Lock {self._lock.lock_id}" + translation_domain=DOMAIN, + translation_key="unlock_failed", + translation_placeholders={"lock_id": str(self._lock.lock_id)}, ) from ex async def async_lock(self, **kwargs: Any) -> None: @@ -121,7 +124,9 @@ class TedeeLockEntity(TedeeEntity, LockEntity): await self.coordinator.async_request_refresh() except (TedeeClientException, Exception) as ex: raise HomeAssistantError( - f"Failed to lock the door. Lock {self._lock.lock_id}" + translation_domain=DOMAIN, + translation_key="lock_failed", + translation_placeholders={"lock_id": str(self._lock.lock_id)}, ) from ex @@ -143,5 +148,7 @@ class TedeeLockWithLatchEntity(TedeeLockEntity): await self.coordinator.async_request_refresh() except (TedeeClientException, Exception) as ex: raise HomeAssistantError( - f"Failed to unlatch the door. Lock {self._lock.lock_id}" + translation_domain=DOMAIN, + translation_key="open_failed", + translation_placeholders={"lock_id": str(self._lock.lock_id)}, ) from ex diff --git a/homeassistant/components/tedee/strings.json b/homeassistant/components/tedee/strings.json index 0668d1370b4..b0b15b76fcd 100644 --- a/homeassistant/components/tedee/strings.json +++ b/homeassistant/components/tedee/strings.json @@ -63,5 +63,16 @@ "name": "Pullspring duration" } } + }, + "exceptions": { + "lock_failed": { + "message": "Failed to lock the door. Lock {lock_id}" + }, + "unlock_failed": { + "message": "Failed to unlock the door. Lock {lock_id}" + }, + "open_failed": { + "message": "Failed to unlatch the door. Lock {lock_id}" + } } } diff --git a/tests/components/tedee/test_lock.py b/tests/components/tedee/test_lock.py index d43cbccd48a..3f6b97e2c70 100644 --- a/tests/components/tedee/test_lock.py +++ b/tests/components/tedee/test_lock.py @@ -152,7 +152,7 @@ async def test_lock_errors( ) -> None: """Test event errors.""" mock_tedee.lock.side_effect = TedeeClientException("Boom") - with pytest.raises(HomeAssistantError, match="Failed to lock the door. Lock 12345"): + with pytest.raises(HomeAssistantError) as exc_info: await hass.services.async_call( LOCK_DOMAIN, SERVICE_LOCK, @@ -161,11 +161,10 @@ async def test_lock_errors( }, blocking=True, ) + assert exc_info.value.translation_key == "lock_failed" mock_tedee.unlock.side_effect = TedeeClientException("Boom") - with pytest.raises( - HomeAssistantError, match="Failed to unlock the door. Lock 12345" - ): + with pytest.raises(HomeAssistantError) as exc_info: await hass.services.async_call( LOCK_DOMAIN, SERVICE_UNLOCK, @@ -174,11 +173,10 @@ async def test_lock_errors( }, blocking=True, ) + assert exc_info.value.translation_key == "unlock_failed" mock_tedee.open.side_effect = TedeeClientException("Boom") - with pytest.raises( - HomeAssistantError, match="Failed to unlatch the door. Lock 12345" - ): + with pytest.raises(HomeAssistantError) as exc_info: await hass.services.async_call( LOCK_DOMAIN, SERVICE_OPEN, @@ -187,6 +185,7 @@ async def test_lock_errors( }, blocking=True, ) + assert exc_info.value.translation_key == "open_failed" @pytest.mark.parametrize(