diff --git a/homeassistant/helpers/selector.py b/homeassistant/helpers/selector.py index 4fa31ee78a2..bc24113251c 100644 --- a/homeassistant/helpers/selector.py +++ b/homeassistant/helpers/selector.py @@ -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 diff --git a/tests/helpers/test_selector.py b/tests/helpers/test_selector.py index dd8cd1c1b64..0e68992d0e4 100644 --- a/tests/helpers/test_selector.py +++ b/tests/helpers/test_selector.py @@ -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": {}, + }, + ), ), ], )