Add translation for tedee exceptions (#126963)

This commit is contained in:
Josef Zweck 2024-09-27 22:21:01 +02:00 committed by GitHub
parent 8a266aac34
commit d9eb419ecc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 27 additions and 10 deletions

View File

@ -10,6 +10,7 @@ from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import TedeeConfigEntry from . import TedeeConfigEntry
from .const import DOMAIN
from .coordinator import TedeeApiCoordinator from .coordinator import TedeeApiCoordinator
from .entity import TedeeEntity from .entity import TedeeEntity
@ -108,7 +109,9 @@ class TedeeLockEntity(TedeeEntity, LockEntity):
await self.coordinator.async_request_refresh() await self.coordinator.async_request_refresh()
except (TedeeClientException, Exception) as ex: except (TedeeClientException, Exception) as ex:
raise HomeAssistantError( 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 ) from ex
async def async_lock(self, **kwargs: Any) -> None: async def async_lock(self, **kwargs: Any) -> None:
@ -121,7 +124,9 @@ class TedeeLockEntity(TedeeEntity, LockEntity):
await self.coordinator.async_request_refresh() await self.coordinator.async_request_refresh()
except (TedeeClientException, Exception) as ex: except (TedeeClientException, Exception) as ex:
raise HomeAssistantError( 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 ) from ex
@ -143,5 +148,7 @@ class TedeeLockWithLatchEntity(TedeeLockEntity):
await self.coordinator.async_request_refresh() await self.coordinator.async_request_refresh()
except (TedeeClientException, Exception) as ex: except (TedeeClientException, Exception) as ex:
raise HomeAssistantError( 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 ) from ex

View File

@ -63,5 +63,16 @@
"name": "Pullspring duration" "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}"
}
} }
} }

View File

@ -152,7 +152,7 @@ async def test_lock_errors(
) -> None: ) -> None:
"""Test event errors.""" """Test event errors."""
mock_tedee.lock.side_effect = TedeeClientException("Boom") 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( await hass.services.async_call(
LOCK_DOMAIN, LOCK_DOMAIN,
SERVICE_LOCK, SERVICE_LOCK,
@ -161,11 +161,10 @@ async def test_lock_errors(
}, },
blocking=True, blocking=True,
) )
assert exc_info.value.translation_key == "lock_failed"
mock_tedee.unlock.side_effect = TedeeClientException("Boom") mock_tedee.unlock.side_effect = TedeeClientException("Boom")
with pytest.raises( with pytest.raises(HomeAssistantError) as exc_info:
HomeAssistantError, match="Failed to unlock the door. Lock 12345"
):
await hass.services.async_call( await hass.services.async_call(
LOCK_DOMAIN, LOCK_DOMAIN,
SERVICE_UNLOCK, SERVICE_UNLOCK,
@ -174,11 +173,10 @@ async def test_lock_errors(
}, },
blocking=True, blocking=True,
) )
assert exc_info.value.translation_key == "unlock_failed"
mock_tedee.open.side_effect = TedeeClientException("Boom") mock_tedee.open.side_effect = TedeeClientException("Boom")
with pytest.raises( with pytest.raises(HomeAssistantError) as exc_info:
HomeAssistantError, match="Failed to unlatch the door. Lock 12345"
):
await hass.services.async_call( await hass.services.async_call(
LOCK_DOMAIN, LOCK_DOMAIN,
SERVICE_OPEN, SERVICE_OPEN,
@ -187,6 +185,7 @@ async def test_lock_errors(
}, },
blocking=True, blocking=True,
) )
assert exc_info.value.translation_key == "open_failed"
@pytest.mark.parametrize( @pytest.mark.parametrize(