mirror of
https://github.com/home-assistant/core.git
synced 2026-08-24 10:13:52 -05:00
Code quality improvements of the selector helper (#151505)
This commit is contained in:
@@ -22,8 +22,8 @@ from . import config_validation as cv
|
||||
SELECTORS: decorator.Registry[str, type[Selector]] = decorator.Registry()
|
||||
|
||||
|
||||
def _get_selector_class(config: Any) -> type[Selector]:
|
||||
"""Get selector class type."""
|
||||
def _get_selector_type_and_class(config: Any) -> tuple[str, type[Selector]]:
|
||||
"""Get selector type and class."""
|
||||
if not isinstance(config, dict):
|
||||
raise vol.Invalid("Expected a dictionary")
|
||||
|
||||
@@ -35,21 +35,18 @@ def _get_selector_class(config: Any) -> type[Selector]:
|
||||
if (selector_class := SELECTORS.get(selector_type)) is None:
|
||||
raise vol.Invalid(f"Unknown selector type {selector_type} found")
|
||||
|
||||
return selector_class
|
||||
return selector_type, selector_class
|
||||
|
||||
|
||||
def selector(config: Any) -> Selector:
|
||||
"""Instantiate a selector."""
|
||||
selector_class = _get_selector_class(config)
|
||||
selector_type = list(config)[0]
|
||||
|
||||
selector_type, selector_class = _get_selector_type_and_class(config)
|
||||
return selector_class(config[selector_type])
|
||||
|
||||
|
||||
def validate_selector(config: Any) -> dict:
|
||||
"""Validate a selector."""
|
||||
selector_class = _get_selector_class(config)
|
||||
selector_type = list(config)[0]
|
||||
selector_type, selector_class = _get_selector_type_and_class(config)
|
||||
|
||||
# Selectors can be empty
|
||||
if config[selector_type] is None:
|
||||
@@ -161,16 +158,14 @@ ENTITY_FILTER_SELECTOR_CONFIG_SCHEMA = vol.Schema(
|
||||
# is provided for backwards compatibility and remains feature frozen.
|
||||
# New filtering features should be added under the `filter` key instead.
|
||||
# https://github.com/home-assistant/frontend/pull/15302
|
||||
LEGACY_ENTITY_SELECTOR_CONFIG_SCHEMA = vol.Schema(
|
||||
{
|
||||
# Integration that provided the entity
|
||||
vol.Optional("integration"): str,
|
||||
# Domain the entity belongs to
|
||||
vol.Optional("domain"): vol.All(cv.ensure_list, [str]),
|
||||
# Device class of the entity
|
||||
vol.Optional("device_class"): vol.All(cv.ensure_list, [str]),
|
||||
}
|
||||
)
|
||||
_LEGACY_ENTITY_SELECTOR_CONFIG_SCHEMA_DICT = {
|
||||
# Integration that provided the entity
|
||||
vol.Optional("integration"): str,
|
||||
# Domain the entity belongs to
|
||||
vol.Optional("domain"): vol.All(cv.ensure_list, [str]),
|
||||
# Device class of the entity
|
||||
vol.Optional("device_class"): vol.All(cv.ensure_list, [str]),
|
||||
}
|
||||
|
||||
|
||||
class EntityFilterSelectorConfig(TypedDict, total=False):
|
||||
@@ -200,16 +195,14 @@ DEVICE_FILTER_SELECTOR_CONFIG_SCHEMA = vol.Schema(
|
||||
# is provided for backwards compatibility and remains feature frozen.
|
||||
# New filtering features should be added under the `filter` key instead.
|
||||
# https://github.com/home-assistant/frontend/pull/15302
|
||||
LEGACY_DEVICE_SELECTOR_CONFIG_SCHEMA = vol.Schema(
|
||||
{
|
||||
# Integration linked to it with a config entry
|
||||
vol.Optional("integration"): str,
|
||||
# Manufacturer of device
|
||||
vol.Optional("manufacturer"): str,
|
||||
# Model of device
|
||||
vol.Optional("model"): str,
|
||||
}
|
||||
)
|
||||
_LEGACY_DEVICE_SELECTOR_CONFIG_SCHEMA_DICT = {
|
||||
# Integration linked to it with a config entry
|
||||
vol.Optional("integration"): str,
|
||||
# Manufacturer of device
|
||||
vol.Optional("manufacturer"): str,
|
||||
# Model of device
|
||||
vol.Optional("model"): str,
|
||||
}
|
||||
|
||||
|
||||
class DeviceFilterSelectorConfig(TypedDict, total=False):
|
||||
@@ -696,9 +689,8 @@ class DeviceSelector(Selector[DeviceSelectorConfig]):
|
||||
selector_type = "device"
|
||||
|
||||
CONFIG_SCHEMA = BASE_SELECTOR_CONFIG_SCHEMA.extend(
|
||||
LEGACY_DEVICE_SELECTOR_CONFIG_SCHEMA.schema
|
||||
).extend(
|
||||
{
|
||||
**_LEGACY_DEVICE_SELECTOR_CONFIG_SCHEMA_DICT,
|
||||
# Device has to contain entities matching this selector
|
||||
vol.Optional("entity"): vol.All(
|
||||
cv.ensure_list, [ENTITY_FILTER_SELECTOR_CONFIG_SCHEMA]
|
||||
@@ -781,9 +773,8 @@ class EntitySelector(Selector[EntitySelectorConfig]):
|
||||
selector_type = "entity"
|
||||
|
||||
CONFIG_SCHEMA = BASE_SELECTOR_CONFIG_SCHEMA.extend(
|
||||
LEGACY_ENTITY_SELECTOR_CONFIG_SCHEMA.schema
|
||||
).extend(
|
||||
{
|
||||
**_LEGACY_ENTITY_SELECTOR_CONFIG_SCHEMA_DICT,
|
||||
vol.Optional("exclude_entities"): [str],
|
||||
vol.Optional("include_entities"): [str],
|
||||
vol.Optional("multiple", default=False): cv.boolean,
|
||||
|
||||
Reference in New Issue
Block a user