Ensure icon translations aren't the same as the default (#108568)

This commit is contained in:
Franck Nijhof 2024-01-21 12:02:15 +01:00 committed by GitHub
parent ec15b0def2
commit fa485513d5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 35 additions and 19 deletions

View File

@ -27,17 +27,13 @@
"damper": {
"default": "mdi:circle",
"state": {
"closed": "mdi:circle-slice-8",
"closing": "mdi:circle",
"opening": "mdi:circle"
"closed": "mdi:circle-slice-8"
}
},
"door": {
"default": "mdi:door-open",
"state": {
"closed": "mdi:door-closed",
"closing": "mdi:door-open",
"opening": "mdi:door-open"
"closed": "mdi:door-closed"
}
},
"garage": {

View File

@ -3,7 +3,7 @@
"_": {
"default": "mdi:cast",
"state": {
"off": "mdi:cast",
"off": "mdi:cast-off",
"paused": "mdi:cast-connected",
"playing": "mdi:cast-connected"
}

View File

@ -10,7 +10,6 @@
"hail": "mdi:weather-hail",
"lightning": "mdi:weather-lightning",
"lightning-rainy": "mdi:weather-lightning-rainy",
"partlycloudy": "mdi:weather-partly-cloudy",
"pouring": "mdi:weather-pouring",
"rainy": "mdi:weather-rainy",
"snowy": "mdi:weather-snowy",

View File

@ -32,6 +32,20 @@ def require_default_icon_validator(value: dict) -> dict:
return value
def ensure_not_same_as_default(value: dict) -> dict:
"""Validate an icon isn't the same as its default icon."""
for translation_key, section in value.items():
if (default := section.get("default")) and (states := section.get("state")):
for state, icon in states.items():
if icon == default:
raise vol.Invalid(
f"The icon for state `{translation_key}.{state}` is the"
" same as the default icon and thus can be removed"
)
return value
def icon_schema(integration_type: str) -> vol.Schema:
"""Create a icon schema."""
@ -44,12 +58,15 @@ def icon_schema(integration_type: str) -> vol.Schema:
return {
marker("default"): icon_value_validator,
vol.Optional("state"): state_validator,
vol.Optional("state_attributes"): cv.schema_with_slug_keys(
{
marker("default"): icon_value_validator,
marker("state"): state_validator,
},
slug_validator=translation_key_validator,
vol.Optional("state_attributes"): vol.All(
cv.schema_with_slug_keys(
{
marker("default"): icon_value_validator,
marker("state"): state_validator,
},
slug_validator=translation_key_validator,
),
ensure_not_same_as_default,
),
}
@ -68,18 +85,22 @@ def icon_schema(integration_type: str) -> vol.Schema:
slug_validator=vol.Any("_", cv.slug),
),
require_default_icon_validator,
ensure_not_same_as_default,
)
}
)
return base_schema.extend(
{
vol.Optional("entity"): cv.schema_with_slug_keys(
vol.Optional("entity"): vol.All(
cv.schema_with_slug_keys(
icon_schema_slug(vol.Optional),
slug_validator=translation_key_validator,
cv.schema_with_slug_keys(
icon_schema_slug(vol.Optional),
slug_validator=translation_key_validator,
),
slug_validator=cv.slug,
),
slug_validator=cv.slug,
),
ensure_not_same_as_default,
)
}
)