Allow use of Selector in ObjectSelector fields

This commit is contained in:
G Johansson
2025-07-02 06:41:39 +00:00
parent 8e12d2028d
commit 315564a443

View File

@@ -3,6 +3,7 @@
from __future__ import annotations from __future__ import annotations
from collections.abc import Callable, Mapping, Sequence from collections.abc import Callable, Mapping, Sequence
from copy import deepcopy
from enum import StrEnum from enum import StrEnum
from functools import cache from functools import cache
import importlib import importlib
@@ -1159,7 +1160,7 @@ class ObjectSelectorField(TypedDict):
label: str label: str
required: bool required: bool
selector: dict[str, Any] selector: Required[Selector | dict[str, Any]]
class ObjectSelectorConfig(BaseSelectorConfig): class ObjectSelectorConfig(BaseSelectorConfig):
@@ -1168,7 +1169,7 @@ class ObjectSelectorConfig(BaseSelectorConfig):
fields: dict[str, ObjectSelectorField] fields: dict[str, ObjectSelectorField]
multiple: bool multiple: bool
label_field: str label_field: str
description_field: bool description_field: str
translation_key: str translation_key: str
@@ -1182,7 +1183,7 @@ class ObjectSelector(Selector[ObjectSelectorConfig]):
{ {
vol.Optional("fields"): { vol.Optional("fields"): {
str: { str: {
vol.Required("selector"): dict, vol.Required("selector"): vol.Any(Selector, dict),
vol.Optional("required"): bool, vol.Optional("required"): bool,
vol.Optional("label"): str, vol.Optional("label"): str,
} }
@@ -1198,6 +1199,17 @@ class ObjectSelector(Selector[ObjectSelectorConfig]):
"""Instantiate a selector.""" """Instantiate a selector."""
super().__init__(config) 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: def __call__(self, data: Any) -> Any:
"""Validate the passed selection.""" """Validate the passed selection."""
return data return data