Add cv.renamed (#143834)

This commit is contained in:
Erik Montnemery 2025-04-29 07:36:37 +02:00 committed by GitHub
parent a47f27821f
commit 16b42cc109
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 55 additions and 28 deletions

View File

@ -58,34 +58,9 @@ _MINIMAL_PLATFORM_SCHEMA = vol.Schema(
def _backward_compat_schema(value: Any | None) -> Any: def _backward_compat_schema(value: Any | None) -> Any:
"""Backward compatibility for automations.""" """Backward compatibility for automations."""
if not isinstance(value, dict): value = cv.renamed(CONF_TRIGGER, CONF_TRIGGERS)(value)
return value value = cv.renamed(CONF_ACTION, CONF_ACTIONS)(value)
return cv.renamed(CONF_CONDITION, CONF_CONDITIONS)(value)
# `trigger` has been renamed to `triggers`
if CONF_TRIGGER in value:
if CONF_TRIGGERS in value:
raise vol.Invalid(
"Cannot specify both 'trigger' and 'triggers'. Please use 'triggers' only."
)
value[CONF_TRIGGERS] = value.pop(CONF_TRIGGER)
# `condition` has been renamed to `conditions`
if CONF_CONDITION in value:
if CONF_CONDITIONS in value:
raise vol.Invalid(
"Cannot specify both 'condition' and 'conditions'. Please use 'conditions' only."
)
value[CONF_CONDITIONS] = value.pop(CONF_CONDITION)
# `action` has been renamed to `actions`
if CONF_ACTION in value:
if CONF_ACTIONS in value:
raise vol.Invalid(
"Cannot specify both 'action' and 'actions'. Please use 'actions' only."
)
value[CONF_ACTIONS] = value.pop(CONF_ACTION)
return value
PLATFORM_SCHEMA = vol.All( PLATFORM_SCHEMA = vol.All(

View File

@ -1059,6 +1059,31 @@ def removed(
) )
def renamed(
old_key: str,
new_key: str,
) -> Callable[[Any], Any]:
"""Replace key with a new key.
Fails if both the new and old key are present.
"""
def validator(value: Any) -> Any:
if not isinstance(value, dict):
return value
if old_key in value:
if new_key in value:
raise vol.Invalid(
f"Cannot specify both '{old_key}' and '{new_key}'. Please use '{new_key}' only."
)
value[new_key] = value.pop(old_key)
return value
return validator
def key_value_schemas( def key_value_schemas(
key: str, key: str,
value_schemas: dict[Hashable, VolSchemaType | Callable[[Any], dict[str, Any]]], value_schemas: dict[Hashable, VolSchemaType | Callable[[Any], dict[str, Any]]],

View File

@ -1953,3 +1953,30 @@ async def test_is_entity_service_schema(
vol.All(vol.Schema(cv.make_entity_service_schema({"some": str}))), vol.All(vol.Schema(cv.make_entity_service_schema({"some": str}))),
): ):
assert cv.is_entity_service_schema(schema) is True assert cv.is_entity_service_schema(schema) is True
def test_renamed(caplog: pytest.LogCaptureFixture, schema) -> None:
"""Test renamed."""
renamed_schema = vol.All(cv.renamed("mors", "mars"), schema)
test_data = {"mars": True}
output = renamed_schema(test_data.copy())
assert len(caplog.records) == 0
assert output == test_data
test_data = {"mors": True}
output = renamed_schema(test_data.copy())
assert len(caplog.records) == 0
assert output == {"mars": True}
test_data = {"mars": True, "mors": True}
with pytest.raises(
vol.Invalid,
match="Cannot specify both 'mors' and 'mars'. Please use 'mars' only.",
):
renamed_schema(test_data.copy())
assert len(caplog.records) == 0
# Check error handling if data is not a dict
with pytest.raises(vol.Invalid, match="expected a dictionary"):
renamed_schema([])