2020-11-13 22:53:55 +01:00
|
|
|
"""Selectors for Home Assistant."""
|
|
|
|
from typing import Any, Callable, Dict, cast
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.util import decorator
|
|
|
|
|
|
|
|
SELECTORS = decorator.Registry()
|
|
|
|
|
|
|
|
|
|
|
|
def validate_selector(config: Any) -> Dict:
|
|
|
|
"""Validate a selector."""
|
|
|
|
if not isinstance(config, dict):
|
|
|
|
raise vol.Invalid("Expected a dictionary")
|
|
|
|
|
|
|
|
if len(config) != 1:
|
|
|
|
raise vol.Invalid(f"Only one type can be specified. Found {', '.join(config)}")
|
|
|
|
|
|
|
|
selector_type = list(config)[0]
|
|
|
|
|
2020-11-19 16:48:43 +01:00
|
|
|
selector_class = SELECTORS.get(selector_type)
|
2020-11-13 22:53:55 +01:00
|
|
|
|
2020-11-19 16:48:43 +01:00
|
|
|
if selector_class is None:
|
2020-11-13 22:53:55 +01:00
|
|
|
raise vol.Invalid(f"Unknown selector type {selector_type} found")
|
|
|
|
|
2020-11-25 15:10:04 +01:00
|
|
|
# Seletors can be empty
|
|
|
|
if config[selector_type] is None:
|
|
|
|
return {selector_type: {}}
|
|
|
|
|
2020-11-19 16:48:43 +01:00
|
|
|
return {
|
|
|
|
selector_type: cast(Dict, selector_class.CONFIG_SCHEMA(config[selector_type]))
|
|
|
|
}
|
2020-11-13 22:53:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Selector:
|
|
|
|
"""Base class for selectors."""
|
|
|
|
|
|
|
|
CONFIG_SCHEMA: Callable
|
|
|
|
|
|
|
|
|
|
|
|
@SELECTORS.register("entity")
|
|
|
|
class EntitySelector(Selector):
|
|
|
|
"""Selector of a single entity."""
|
|
|
|
|
|
|
|
CONFIG_SCHEMA = vol.Schema(
|
|
|
|
{
|
2020-11-20 15:24:42 +01:00
|
|
|
# Integration that provided the entity
|
2020-11-13 22:53:55 +01:00
|
|
|
vol.Optional("integration"): str,
|
2020-11-20 15:24:42 +01:00
|
|
|
# Domain the entity belongs to
|
2020-11-13 22:53:55 +01:00
|
|
|
vol.Optional("domain"): str,
|
2020-11-25 15:10:04 +01:00
|
|
|
# Device class of the entity
|
|
|
|
vol.Optional("device_class"): str,
|
2020-11-13 22:53:55 +01:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@SELECTORS.register("device")
|
|
|
|
class DeviceSelector(Selector):
|
|
|
|
"""Selector of a single device."""
|
|
|
|
|
|
|
|
CONFIG_SCHEMA = vol.Schema(
|
|
|
|
{
|
2020-11-20 15:24:42 +01:00
|
|
|
# Integration linked to it with a config entry
|
2020-11-13 22:53:55 +01:00
|
|
|
vol.Optional("integration"): str,
|
2020-11-20 15:24:42 +01:00
|
|
|
# Manufacturer of device
|
2020-11-13 22:53:55 +01:00
|
|
|
vol.Optional("manufacturer"): str,
|
2020-11-20 15:24:42 +01:00
|
|
|
# Model of device
|
2020-11-13 22:53:55 +01:00
|
|
|
vol.Optional("model"): str,
|
|
|
|
}
|
|
|
|
)
|