mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
Add translation to ServiceValidationError in Lock (#105746)
This commit is contained in:
parent
dae8c0fc38
commit
8cd0644035
@ -24,6 +24,7 @@ from homeassistant.const import (
|
|||||||
STATE_UNLOCKING,
|
STATE_UNLOCKING,
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
|
from homeassistant.exceptions import ServiceValidationError
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.config_validation import ( # noqa: F401
|
from homeassistant.helpers.config_validation import ( # noqa: F401
|
||||||
PLATFORM_SCHEMA,
|
PLATFORM_SCHEMA,
|
||||||
@ -152,8 +153,16 @@ class LockEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_):
|
|||||||
if not code:
|
if not code:
|
||||||
code = self._lock_option_default_code
|
code = self._lock_option_default_code
|
||||||
if self.code_format_cmp and not self.code_format_cmp.match(code):
|
if self.code_format_cmp and not self.code_format_cmp.match(code):
|
||||||
raise ValueError(
|
if TYPE_CHECKING:
|
||||||
f"Code '{code}' for locking {self.entity_id} doesn't match pattern {self.code_format}"
|
assert self.code_format
|
||||||
|
raise ServiceValidationError(
|
||||||
|
f"The code for {self.entity_id} doesn't match pattern {self.code_format}",
|
||||||
|
translation_domain=DOMAIN,
|
||||||
|
translation_key="add_default_code",
|
||||||
|
translation_placeholders={
|
||||||
|
"entity_id": self.entity_id,
|
||||||
|
"code_format": self.code_format,
|
||||||
|
},
|
||||||
)
|
)
|
||||||
if code:
|
if code:
|
||||||
data[ATTR_CODE] = code
|
data[ATTR_CODE] = code
|
||||||
|
@ -66,5 +66,10 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"exceptions": {
|
||||||
|
"add_default_code": {
|
||||||
|
"message": "The code for {entity_id} doesn't match pattern {code_format}."
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
"""The tests for the lock component."""
|
"""The tests for the lock component."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import re
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
@ -21,6 +22,7 @@ from homeassistant.components.lock import (
|
|||||||
LockEntityFeature,
|
LockEntityFeature,
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.exceptions import ServiceValidationError
|
||||||
import homeassistant.helpers.entity_registry as er
|
import homeassistant.helpers.entity_registry as er
|
||||||
from homeassistant.helpers.typing import UNDEFINED, UndefinedType
|
from homeassistant.helpers.typing import UNDEFINED, UndefinedType
|
||||||
|
|
||||||
@ -134,15 +136,15 @@ async def test_lock_open_with_code(
|
|||||||
state = hass.states.get(mock_lock_entity.entity_id)
|
state = hass.states.get(mock_lock_entity.entity_id)
|
||||||
assert state.attributes["code_format"] == r"^\d{4}$"
|
assert state.attributes["code_format"] == r"^\d{4}$"
|
||||||
|
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ServiceValidationError):
|
||||||
await help_test_async_lock_service(
|
await help_test_async_lock_service(
|
||||||
hass, mock_lock_entity.entity_id, SERVICE_OPEN
|
hass, mock_lock_entity.entity_id, SERVICE_OPEN
|
||||||
)
|
)
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ServiceValidationError):
|
||||||
await help_test_async_lock_service(
|
await help_test_async_lock_service(
|
||||||
hass, mock_lock_entity.entity_id, SERVICE_OPEN, code=""
|
hass, mock_lock_entity.entity_id, SERVICE_OPEN, code=""
|
||||||
)
|
)
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ServiceValidationError):
|
||||||
await help_test_async_lock_service(
|
await help_test_async_lock_service(
|
||||||
hass, mock_lock_entity.entity_id, SERVICE_OPEN, code="HELLO"
|
hass, mock_lock_entity.entity_id, SERVICE_OPEN, code="HELLO"
|
||||||
)
|
)
|
||||||
@ -170,15 +172,15 @@ async def test_lock_lock_with_code(
|
|||||||
mock_lock_entity.calls_unlock.assert_called_with(code="1234")
|
mock_lock_entity.calls_unlock.assert_called_with(code="1234")
|
||||||
assert mock_lock_entity.calls_lock.call_count == 0
|
assert mock_lock_entity.calls_lock.call_count == 0
|
||||||
|
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ServiceValidationError):
|
||||||
await help_test_async_lock_service(
|
await help_test_async_lock_service(
|
||||||
hass, mock_lock_entity.entity_id, SERVICE_LOCK
|
hass, mock_lock_entity.entity_id, SERVICE_LOCK
|
||||||
)
|
)
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ServiceValidationError):
|
||||||
await help_test_async_lock_service(
|
await help_test_async_lock_service(
|
||||||
hass, mock_lock_entity.entity_id, SERVICE_LOCK, code=""
|
hass, mock_lock_entity.entity_id, SERVICE_LOCK, code=""
|
||||||
)
|
)
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ServiceValidationError):
|
||||||
await help_test_async_lock_service(
|
await help_test_async_lock_service(
|
||||||
hass, mock_lock_entity.entity_id, SERVICE_LOCK, code="HELLO"
|
hass, mock_lock_entity.entity_id, SERVICE_LOCK, code="HELLO"
|
||||||
)
|
)
|
||||||
@ -206,15 +208,15 @@ async def test_lock_unlock_with_code(
|
|||||||
mock_lock_entity.calls_lock.assert_called_with(code="1234")
|
mock_lock_entity.calls_lock.assert_called_with(code="1234")
|
||||||
assert mock_lock_entity.calls_unlock.call_count == 0
|
assert mock_lock_entity.calls_unlock.call_count == 0
|
||||||
|
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ServiceValidationError):
|
||||||
await help_test_async_lock_service(
|
await help_test_async_lock_service(
|
||||||
hass, mock_lock_entity.entity_id, SERVICE_UNLOCK
|
hass, mock_lock_entity.entity_id, SERVICE_UNLOCK
|
||||||
)
|
)
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ServiceValidationError):
|
||||||
await help_test_async_lock_service(
|
await help_test_async_lock_service(
|
||||||
hass, mock_lock_entity.entity_id, SERVICE_UNLOCK, code=""
|
hass, mock_lock_entity.entity_id, SERVICE_UNLOCK, code=""
|
||||||
)
|
)
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ServiceValidationError):
|
||||||
await help_test_async_lock_service(
|
await help_test_async_lock_service(
|
||||||
hass, mock_lock_entity.entity_id, SERVICE_UNLOCK, code="HELLO"
|
hass, mock_lock_entity.entity_id, SERVICE_UNLOCK, code="HELLO"
|
||||||
)
|
)
|
||||||
@ -234,15 +236,15 @@ async def test_lock_with_illegal_code(
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Test lock entity with default code that does not match the code format."""
|
"""Test lock entity with default code that does not match the code format."""
|
||||||
|
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ServiceValidationError):
|
||||||
await help_test_async_lock_service(
|
await help_test_async_lock_service(
|
||||||
hass, mock_lock_entity.entity_id, SERVICE_OPEN, code="123456"
|
hass, mock_lock_entity.entity_id, SERVICE_OPEN, code="123456"
|
||||||
)
|
)
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ServiceValidationError):
|
||||||
await help_test_async_lock_service(
|
await help_test_async_lock_service(
|
||||||
hass, mock_lock_entity.entity_id, SERVICE_LOCK, code="123456"
|
hass, mock_lock_entity.entity_id, SERVICE_LOCK, code="123456"
|
||||||
)
|
)
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ServiceValidationError):
|
||||||
await help_test_async_lock_service(
|
await help_test_async_lock_service(
|
||||||
hass, mock_lock_entity.entity_id, SERVICE_UNLOCK, code="123456"
|
hass, mock_lock_entity.entity_id, SERVICE_UNLOCK, code="123456"
|
||||||
)
|
)
|
||||||
@ -344,19 +346,30 @@ async def test_lock_with_illegal_default_code(
|
|||||||
assert mock_lock_entity.state_attributes == {"code_format": r"^\d{4}$"}
|
assert mock_lock_entity.state_attributes == {"code_format": r"^\d{4}$"}
|
||||||
assert mock_lock_entity._lock_option_default_code == ""
|
assert mock_lock_entity._lock_option_default_code == ""
|
||||||
|
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ServiceValidationError):
|
||||||
await help_test_async_lock_service(
|
await help_test_async_lock_service(
|
||||||
hass, mock_lock_entity.entity_id, SERVICE_OPEN
|
hass, mock_lock_entity.entity_id, SERVICE_OPEN
|
||||||
)
|
)
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ServiceValidationError):
|
||||||
await help_test_async_lock_service(
|
await help_test_async_lock_service(
|
||||||
hass, mock_lock_entity.entity_id, SERVICE_LOCK
|
hass, mock_lock_entity.entity_id, SERVICE_LOCK
|
||||||
)
|
)
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(
|
||||||
|
ServiceValidationError,
|
||||||
|
match=re.escape(
|
||||||
|
rf"The code for lock.test_lock doesn't match pattern ^\d{{{4}}}$"
|
||||||
|
),
|
||||||
|
) as exc:
|
||||||
await help_test_async_lock_service(
|
await help_test_async_lock_service(
|
||||||
hass, mock_lock_entity.entity_id, SERVICE_UNLOCK
|
hass, mock_lock_entity.entity_id, SERVICE_UNLOCK
|
||||||
)
|
)
|
||||||
|
|
||||||
|
assert (
|
||||||
|
str(exc.value)
|
||||||
|
== rf"The code for lock.test_lock doesn't match pattern ^\d{{{4}}}$"
|
||||||
|
)
|
||||||
|
assert exc.value.translation_key == "add_default_code"
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(("enum"), list(LockEntityFeature))
|
@pytest.mark.parametrize(("enum"), list(LockEntityFeature))
|
||||||
def test_deprecated_constants(
|
def test_deprecated_constants(
|
||||||
|
@ -14,6 +14,7 @@ from homeassistant.components.lock import (
|
|||||||
)
|
)
|
||||||
from homeassistant.const import ATTR_CODE, STATE_UNKNOWN
|
from homeassistant.const import ATTR_CODE, STATE_UNKNOWN
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.exceptions import ServiceValidationError
|
||||||
import homeassistant.helpers.entity_registry as er
|
import homeassistant.helpers.entity_registry as er
|
||||||
|
|
||||||
from .common import set_node_attribute, trigger_subscription_callback
|
from .common import set_node_attribute, trigger_subscription_callback
|
||||||
@ -113,7 +114,7 @@ async def test_lock_requires_pin(
|
|||||||
# set door state to unlocked
|
# set door state to unlocked
|
||||||
set_node_attribute(door_lock, 1, 257, 0, 2)
|
set_node_attribute(door_lock, 1, 257, 0, 2)
|
||||||
|
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ServiceValidationError):
|
||||||
# Lock door using invalid code format
|
# Lock door using invalid code format
|
||||||
await trigger_subscription_callback(hass, matter_client)
|
await trigger_subscription_callback(hass, matter_client)
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user