diff --git a/tests/helpers/snapshots/test_selector.ambr b/tests/helpers/snapshots/test_selector.ambr new file mode 100644 index 00000000000..152230b2b02 --- /dev/null +++ b/tests/helpers/snapshots/test_selector.ambr @@ -0,0 +1,33 @@ +# serializer version: 1 +# name: test_object_selector_uses_selectors + dict({ + 'selector': dict({ + 'object': dict({ + 'description_field': 'percentage', + 'fields': dict({ + 'name': dict({ + 'required': True, + 'selector': dict({ + 'text': dict({ + 'multiline': False, + 'multiple': False, + }), + }), + }), + 'percentage': dict({ + 'selector': dict({ + 'number': dict({ + 'max': 100.0, + 'min': 0.0, + 'mode': 'slider', + 'step': 1.0, + }), + }), + }), + }), + 'label_field': 'name', + 'multiple': True, + }), + }), + }) +# --- diff --git a/tests/helpers/test_selector.py b/tests/helpers/test_selector.py index 8947ea8099c..5c672edf937 100644 --- a/tests/helpers/test_selector.py +++ b/tests/helpers/test_selector.py @@ -4,6 +4,7 @@ from enum import Enum from typing import Any import pytest +from syrupy.assertion import SnapshotAssertion import voluptuous as vol from homeassistant.helpers import selector @@ -618,6 +619,43 @@ def test_object_selector_schema(schema, valid_selections, invalid_selections) -> _test_selector("object", schema, valid_selections, invalid_selections) +def test_object_selector_uses_selectors(snapshot: SnapshotAssertion) -> None: + """Test ObjectSelector custom serializer for using Selector in ObjectSelectorField.""" + + selector_type = "object" + schema = { + "fields": { + "name": { + "required": True, + "selector": selector.TextSelector(), + }, + "percentage": { + "selector": selector.NumberSelector( + selector.NumberSelectorConfig(min=0, max=100) + ), + }, + }, + "multiple": True, + "label_field": "name", + "description_field": "percentage", + } + + # Validate selector configuration + config = {selector_type: schema} + selector.validate_selector(config) + selector_instance = selector.selector(config) + + # Serialize selector + selector_instance = selector.selector({selector_type: schema}) + assert selector_instance.serialize() != { + "selector": {selector_type: selector_instance.config} + } + assert selector_instance.serialize() == snapshot() + + # Test serialized selector can be dumped to YAML + yaml_util.dump(selector_instance.serialize()) + + @pytest.mark.parametrize( ("schema", "valid_selections", "invalid_selections"), [