From 315564a44317197e0fe8b408ab2da873b411df1b Mon Sep 17 00:00:00 2001 From: G Johansson Date: Wed, 2 Jul 2025 06:41:39 +0000 Subject: [PATCH] Allow use of Selector in ObjectSelector fields --- homeassistant/helpers/selector.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/homeassistant/helpers/selector.py b/homeassistant/helpers/selector.py index ad0c909003e..952a6bafea8 100644 --- a/homeassistant/helpers/selector.py +++ b/homeassistant/helpers/selector.py @@ -3,6 +3,7 @@ from __future__ import annotations from collections.abc import Callable, Mapping, Sequence +from copy import deepcopy from enum import StrEnum from functools import cache import importlib @@ -1159,7 +1160,7 @@ class ObjectSelectorField(TypedDict): label: str required: bool - selector: dict[str, Any] + selector: Required[Selector | dict[str, Any]] class ObjectSelectorConfig(BaseSelectorConfig): @@ -1168,7 +1169,7 @@ class ObjectSelectorConfig(BaseSelectorConfig): fields: dict[str, ObjectSelectorField] multiple: bool label_field: str - description_field: bool + description_field: str translation_key: str @@ -1182,7 +1183,7 @@ class ObjectSelector(Selector[ObjectSelectorConfig]): { vol.Optional("fields"): { str: { - vol.Required("selector"): dict, + vol.Required("selector"): vol.Any(Selector, dict), vol.Optional("required"): bool, vol.Optional("label"): str, } @@ -1198,6 +1199,17 @@ class ObjectSelector(Selector[ObjectSelectorConfig]): """Instantiate a selector.""" super().__init__(config) + def serialize(self) -> dict[str, dict[str, ObjectSelectorConfig]]: + """Serialize ObjectSelector for voluptuous_serialize.""" + _config = deepcopy(self.config) + if "fields" in _config: + for items in _config["fields"].values(): + if isinstance(items["selector"], Selector): + items["selector"] = { + items["selector"].selector_type: items["selector"].config + } + return {"selector": {self.selector_type: _config}} + def __call__(self, data: Any) -> Any: """Validate the passed selection.""" return data