Fix media selector validation (#147855)

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Paulus Schoutsen 2025-07-04 22:30:35 +02:00 committed by GitHub
parent 470baa782e
commit 6a7f4953cd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 8 deletions

View File

@ -1045,16 +1045,17 @@ class MediaSelector(Selector[MediaSelectorConfig]):
def __call__(self, data: Any) -> dict[str, str]:
"""Validate the passed selection."""
schema = self.DATA_SCHEMA.schema.copy()
schema = {
key: value
for key, value in self.DATA_SCHEMA.schema.items()
if key != "entity_id"
}
if "accept" in self.config:
# If accept is set, the entity_id field will not be present
schema.pop("entity_id", None)
else:
if "accept" not in self.config:
# If accept is not set, the entity_id field is required
schema[vol.Required("entity_id")] = cv.entity_id_or_uuid
media: dict[str, str] = self.DATA_SCHEMA(data)
media: dict[str, str] = vol.Schema(schema)(data)
return media

View File

@ -842,7 +842,16 @@ def test_theme_selector_schema(schema, valid_selections, invalid_selections) ->
"metadata": {},
},
),
(None, "abc", {}),
(
None,
"abc",
{},
# We require entity_id when accept is not set
{
"media_content_id": "abc",
"media_content_type": "def",
},
),
),
(
{
@ -859,7 +868,18 @@ def test_theme_selector_schema(schema, valid_selections, invalid_selections) ->
"metadata": {},
},
),
(None, "abc", {}),
(
None,
"abc",
{},
{
# We do not allow entity_id when accept is set
"entity_id": "sensor.abc",
"media_content_id": "abc",
"media_content_type": "def",
"metadata": {},
},
),
),
],
)