2016-03-28 01:48:51 +00:00
|
|
|
"""Helpers for config validation using voluptuous."""
|
2019-07-31 19:25:30 +00:00
|
|
|
from datetime import (
|
2019-12-09 15:42:10 +00:00
|
|
|
date as date_sys,
|
2019-07-31 19:25:30 +00:00
|
|
|
datetime as datetime_sys,
|
|
|
|
time as time_sys,
|
2019-12-09 15:42:10 +00:00
|
|
|
timedelta,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-12-09 15:42:10 +00:00
|
|
|
import inspect
|
|
|
|
import logging
|
2019-06-08 05:18:02 +00:00
|
|
|
from numbers import Number
|
2019-12-09 15:42:10 +00:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
from socket import _GLOBAL_DEFAULT_TIMEOUT
|
|
|
|
from typing import Any, Callable, Dict, List, Optional, TypeVar, Union
|
2019-02-08 10:14:50 +00:00
|
|
|
from urllib.parse import urlparse
|
2019-03-28 04:53:11 +00:00
|
|
|
from uuid import UUID
|
2016-08-07 23:26:35 +00:00
|
|
|
|
2019-02-08 10:14:50 +00:00
|
|
|
from pkg_resources import parse_version
|
2019-10-02 20:14:52 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
import voluptuous_serialize
|
2016-03-28 01:48:51 +00:00
|
|
|
|
|
|
|
from homeassistant.const import (
|
2019-12-09 15:42:10 +00:00
|
|
|
ATTR_AREA_ID,
|
|
|
|
ATTR_ENTITY_ID,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_ABOVE,
|
|
|
|
CONF_ALIAS,
|
|
|
|
CONF_BELOW,
|
|
|
|
CONF_CONDITION,
|
2019-09-09 17:40:22 +00:00
|
|
|
CONF_DEVICE_ID,
|
2019-09-05 14:49:32 +00:00
|
|
|
CONF_DOMAIN,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_ENTITY_ID,
|
|
|
|
CONF_ENTITY_NAMESPACE,
|
2019-09-05 14:49:32 +00:00
|
|
|
CONF_FOR,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_PLATFORM,
|
|
|
|
CONF_SCAN_INTERVAL,
|
2019-09-05 14:49:32 +00:00
|
|
|
CONF_STATE,
|
2019-12-09 15:42:10 +00:00
|
|
|
CONF_TIMEOUT,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_UNIT_SYSTEM_IMPERIAL,
|
|
|
|
CONF_UNIT_SYSTEM_METRIC,
|
|
|
|
CONF_VALUE_TEMPLATE,
|
|
|
|
ENTITY_MATCH_ALL,
|
|
|
|
SUN_EVENT_SUNRISE,
|
|
|
|
SUN_EVENT_SUNSET,
|
|
|
|
TEMP_CELSIUS,
|
|
|
|
TEMP_FAHRENHEIT,
|
|
|
|
WEEKDAYS,
|
|
|
|
__version__,
|
|
|
|
)
|
2019-12-09 15:42:10 +00:00
|
|
|
from homeassistant.core import split_entity_id, valid_entity_id
|
2016-09-28 04:29:55 +00:00
|
|
|
from homeassistant.exceptions import TemplateError
|
2019-02-08 10:14:50 +00:00
|
|
|
from homeassistant.helpers.logging import KeywordStyleAdapter
|
|
|
|
from homeassistant.util import slugify as util_slugify
|
2019-12-09 15:42:10 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
2019-07-21 16:59:02 +00:00
|
|
|
|
2019-09-20 15:23:34 +00:00
|
|
|
# mypy: allow-untyped-calls, allow-untyped-defs
|
2019-07-21 16:59:02 +00:00
|
|
|
# mypy: no-check-untyped-defs, no-warn-return-any
|
2016-03-28 01:48:51 +00:00
|
|
|
# pylint: disable=invalid-name
|
|
|
|
|
2016-04-28 10:03:57 +00:00
|
|
|
TIME_PERIOD_ERROR = "offset {} should be format 'HH:MM' or 'HH:MM:SS'"
|
2019-01-26 22:09:41 +00:00
|
|
|
|
2016-04-28 10:03:57 +00:00
|
|
|
|
2016-04-03 17:19:09 +00:00
|
|
|
# Home Assistant types
|
2016-04-01 03:19:59 +00:00
|
|
|
byte = vol.All(vol.Coerce(int), vol.Range(min=0, max=255))
|
|
|
|
small_float = vol.All(vol.Coerce(float), vol.Range(min=0, max=1))
|
2016-04-07 19:19:28 +00:00
|
|
|
positive_int = vol.All(vol.Coerce(int), vol.Range(min=0))
|
2019-07-31 19:25:30 +00:00
|
|
|
latitude = vol.All(
|
|
|
|
vol.Coerce(float), vol.Range(min=-90, max=90), msg="invalid latitude"
|
|
|
|
)
|
|
|
|
longitude = vol.All(
|
|
|
|
vol.Coerce(float), vol.Range(min=-180, max=180), msg="invalid longitude"
|
|
|
|
)
|
2018-03-09 07:57:21 +00:00
|
|
|
gps = vol.ExactSequence([latitude, longitude])
|
2016-05-03 05:05:09 +00:00
|
|
|
sun_event = vol.All(vol.Lower, vol.Any(SUN_EVENT_SUNSET, SUN_EVENT_SUNRISE))
|
2016-08-17 03:55:29 +00:00
|
|
|
port = vol.All(vol.Coerce(int), vol.Range(min=1, max=65535))
|
2016-04-03 17:19:09 +00:00
|
|
|
|
2016-08-07 23:26:35 +00:00
|
|
|
# typing typevar
|
2019-07-31 19:25:30 +00:00
|
|
|
T = TypeVar("T")
|
2016-08-07 23:26:35 +00:00
|
|
|
|
2016-04-03 17:19:09 +00:00
|
|
|
|
2016-04-21 22:52:20 +00:00
|
|
|
# Adapted from:
|
|
|
|
# https://github.com/alecthomas/voluptuous/issues/115#issuecomment-144464666
|
2016-08-07 23:26:35 +00:00
|
|
|
def has_at_least_one_key(*keys: str) -> Callable:
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Validate that at least one key exists."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2016-08-07 23:26:35 +00:00
|
|
|
def validate(obj: Dict) -> Dict:
|
2016-04-21 22:52:20 +00:00
|
|
|
"""Test keys exist in dict."""
|
|
|
|
if not isinstance(obj, dict):
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid("expected dictionary")
|
2016-04-21 22:52:20 +00:00
|
|
|
|
|
|
|
for k in obj.keys():
|
|
|
|
if k in keys:
|
|
|
|
return obj
|
2019-09-27 15:48:48 +00:00
|
|
|
raise vol.Invalid("must contain at least one of {}.".format(", ".join(keys)))
|
2016-04-21 22:52:20 +00:00
|
|
|
|
|
|
|
return validate
|
|
|
|
|
|
|
|
|
2019-09-20 15:23:34 +00:00
|
|
|
def has_at_most_one_key(*keys: str) -> Callable[[Dict], Dict]:
|
2019-02-08 10:14:50 +00:00
|
|
|
"""Validate that zero keys exist or one key exists."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2019-02-08 10:14:50 +00:00
|
|
|
def validate(obj: Dict) -> Dict:
|
|
|
|
"""Test zero keys exist or one key exists in dict."""
|
|
|
|
if not isinstance(obj, dict):
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid("expected dictionary")
|
2019-02-08 10:14:50 +00:00
|
|
|
|
|
|
|
if len(set(keys) & set(obj)) > 1:
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid("must contain at most one of {}.".format(", ".join(keys)))
|
2019-02-08 10:14:50 +00:00
|
|
|
return obj
|
|
|
|
|
|
|
|
return validate
|
|
|
|
|
|
|
|
|
2016-08-07 23:26:35 +00:00
|
|
|
def boolean(value: Any) -> bool:
|
2016-04-03 17:19:09 +00:00
|
|
|
"""Validate and coerce a boolean value."""
|
2019-06-08 05:18:02 +00:00
|
|
|
if isinstance(value, bool):
|
|
|
|
return value
|
2016-04-03 17:19:09 +00:00
|
|
|
if isinstance(value, str):
|
2019-06-08 05:18:02 +00:00
|
|
|
value = value.lower().strip()
|
2019-07-31 19:25:30 +00:00
|
|
|
if value in ("1", "true", "yes", "on", "enable"):
|
2016-04-03 17:19:09 +00:00
|
|
|
return True
|
2019-07-31 19:25:30 +00:00
|
|
|
if value in ("0", "false", "no", "off", "disable"):
|
2016-04-03 17:19:09 +00:00
|
|
|
return False
|
2019-06-08 05:18:02 +00:00
|
|
|
elif isinstance(value, Number):
|
2019-07-21 16:59:02 +00:00
|
|
|
# type ignore: https://github.com/python/mypy/issues/3186
|
|
|
|
return value != 0 # type: ignore
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid("invalid boolean value {}".format(value))
|
2016-03-28 01:48:51 +00:00
|
|
|
|
|
|
|
|
2017-01-25 06:04:44 +00:00
|
|
|
def isdevice(value):
|
|
|
|
"""Validate that value is a real device."""
|
|
|
|
try:
|
|
|
|
os.stat(value)
|
|
|
|
return str(value)
|
|
|
|
except OSError:
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid("No device at {} found".format(value))
|
2017-01-25 06:04:44 +00:00
|
|
|
|
|
|
|
|
2018-05-05 14:00:36 +00:00
|
|
|
def matches_regex(regex):
|
|
|
|
"""Validate that the value is a string that matches a regex."""
|
|
|
|
regex = re.compile(regex)
|
|
|
|
|
|
|
|
def validator(value: Any) -> str:
|
|
|
|
"""Validate that value matches the given regex."""
|
|
|
|
if not isinstance(value, str):
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid("not a string value: {}".format(value))
|
2018-05-05 14:00:36 +00:00
|
|
|
|
|
|
|
if not regex.match(value):
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid(
|
|
|
|
"value {} does not match regular expression {}".format(
|
|
|
|
value, regex.pattern
|
|
|
|
)
|
|
|
|
)
|
2018-05-05 14:00:36 +00:00
|
|
|
|
|
|
|
return value
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2018-05-05 14:00:36 +00:00
|
|
|
return validator
|
|
|
|
|
|
|
|
|
|
|
|
def is_regex(value):
|
|
|
|
"""Validate that a string is a valid regular expression."""
|
|
|
|
try:
|
|
|
|
r = re.compile(value)
|
|
|
|
return r
|
|
|
|
except TypeError:
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid(
|
|
|
|
"value {} is of the wrong type for a regular " "expression".format(value)
|
|
|
|
)
|
2018-05-05 14:00:36 +00:00
|
|
|
except re.error:
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid("value {} is not a valid regular expression".format(value))
|
2018-05-05 14:00:36 +00:00
|
|
|
|
|
|
|
|
2016-09-01 13:35:00 +00:00
|
|
|
def isfile(value: Any) -> str:
|
2016-04-07 17:52:25 +00:00
|
|
|
"""Validate that the value is an existing file."""
|
2016-09-01 13:35:00 +00:00
|
|
|
if value is None:
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid("None is not file")
|
2016-09-03 23:32:43 +00:00
|
|
|
file_in = os.path.expanduser(str(value))
|
2016-09-01 13:35:00 +00:00
|
|
|
|
|
|
|
if not os.path.isfile(file_in):
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid("not a file")
|
2016-09-01 13:35:00 +00:00
|
|
|
if not os.access(file_in, os.R_OK):
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid("file not readable")
|
2016-09-01 13:35:00 +00:00
|
|
|
return file_in
|
2016-04-07 17:52:25 +00:00
|
|
|
|
|
|
|
|
2017-10-25 02:36:27 +00:00
|
|
|
def isdir(value: Any) -> str:
|
|
|
|
"""Validate that the value is an existing dir."""
|
|
|
|
if value is None:
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid("not a directory")
|
2017-10-25 02:36:27 +00:00
|
|
|
dir_in = os.path.expanduser(str(value))
|
|
|
|
|
|
|
|
if not os.path.isdir(dir_in):
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid("not a directory")
|
2017-10-25 02:36:27 +00:00
|
|
|
if not os.access(dir_in, os.R_OK):
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid("directory not readable")
|
2017-10-25 02:36:27 +00:00
|
|
|
return dir_in
|
|
|
|
|
|
|
|
|
2019-08-10 23:30:33 +00:00
|
|
|
def ensure_list(value: Union[T, List[T], None]) -> List[T]:
|
2016-04-04 19:18:58 +00:00
|
|
|
"""Wrap value in list if it is not one."""
|
2017-01-05 19:33:22 +00:00
|
|
|
if value is None:
|
|
|
|
return []
|
2016-04-04 19:18:58 +00:00
|
|
|
return value if isinstance(value, list) else [value]
|
|
|
|
|
|
|
|
|
2016-08-07 23:26:35 +00:00
|
|
|
def entity_id(value: Any) -> str:
|
2016-03-28 01:48:51 +00:00
|
|
|
"""Validate Entity ID."""
|
2016-04-10 22:20:20 +00:00
|
|
|
value = string(value).lower()
|
2016-03-28 01:48:51 +00:00
|
|
|
if valid_entity_id(value):
|
|
|
|
return value
|
2019-01-26 22:09:41 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid("Entity ID {} is an invalid entity id".format(value))
|
2016-03-28 01:48:51 +00:00
|
|
|
|
|
|
|
|
2019-08-10 23:30:33 +00:00
|
|
|
def entity_ids(value: Union[str, List]) -> List[str]:
|
2016-03-28 01:48:51 +00:00
|
|
|
"""Validate Entity IDs."""
|
2016-06-09 03:55:08 +00:00
|
|
|
if value is None:
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid("Entity IDs can not be None")
|
2016-03-28 01:48:51 +00:00
|
|
|
if isinstance(value, str):
|
2019-07-31 19:25:30 +00:00
|
|
|
value = [ent_id.strip() for ent_id in value.split(",")]
|
2016-03-28 01:48:51 +00:00
|
|
|
|
2016-04-10 22:20:20 +00:00
|
|
|
return [entity_id(ent_id) for ent_id in value]
|
2016-03-28 01:48:51 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
comp_entity_ids = vol.Any(vol.All(vol.Lower, ENTITY_MATCH_ALL), entity_ids)
|
2018-12-13 09:07:59 +00:00
|
|
|
|
|
|
|
|
2019-09-20 15:23:34 +00:00
|
|
|
def entity_domain(domain: str) -> Callable[[Any], str]:
|
2018-02-26 07:48:21 +00:00
|
|
|
"""Validate that entity belong to domain."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2018-02-26 07:48:21 +00:00
|
|
|
def validate(value: Any) -> str:
|
|
|
|
"""Test if entity domain is domain."""
|
|
|
|
ent_domain = entities_domain(domain)
|
|
|
|
return ent_domain(value)[0]
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2018-02-26 07:48:21 +00:00
|
|
|
return validate
|
|
|
|
|
|
|
|
|
2019-09-20 15:23:34 +00:00
|
|
|
def entities_domain(domain: str) -> Callable[[Union[str, List]], List[str]]:
|
2018-02-26 07:48:21 +00:00
|
|
|
"""Validate that entities belong to domain."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2019-08-10 23:30:33 +00:00
|
|
|
def validate(values: Union[str, List]) -> List[str]:
|
2018-02-26 07:48:21 +00:00
|
|
|
"""Test if entity domain is domain."""
|
|
|
|
values = entity_ids(values)
|
|
|
|
for ent_id in values:
|
|
|
|
if split_entity_id(ent_id)[0] != domain:
|
|
|
|
raise vol.Invalid(
|
2019-07-31 19:25:30 +00:00
|
|
|
"Entity ID '{}' does not belong to domain '{}'".format(
|
|
|
|
ent_id, domain
|
|
|
|
)
|
|
|
|
)
|
2018-02-26 07:48:21 +00:00
|
|
|
return values
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2018-02-26 07:48:21 +00:00
|
|
|
return validate
|
|
|
|
|
|
|
|
|
2016-09-28 04:29:55 +00:00
|
|
|
def enum(enumClass):
|
|
|
|
"""Create validator for specified enum."""
|
|
|
|
return vol.All(vol.In(enumClass.__members__), enumClass.__getitem__)
|
|
|
|
|
|
|
|
|
2016-03-28 01:48:51 +00:00
|
|
|
def icon(value):
|
|
|
|
"""Validate icon."""
|
|
|
|
value = str(value)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if ":" in value:
|
2016-03-28 01:48:51 +00:00
|
|
|
return value
|
|
|
|
|
2019-08-02 21:20:07 +00:00
|
|
|
raise vol.Invalid('Icons should be specified in the form "prefix:name"')
|
2016-03-28 01:48:51 +00:00
|
|
|
|
|
|
|
|
2016-04-21 22:52:20 +00:00
|
|
|
time_period_dict = vol.All(
|
2019-07-31 19:25:30 +00:00
|
|
|
dict,
|
|
|
|
vol.Schema(
|
|
|
|
{
|
|
|
|
"days": vol.Coerce(int),
|
|
|
|
"hours": vol.Coerce(int),
|
|
|
|
"minutes": vol.Coerce(int),
|
|
|
|
"seconds": vol.Coerce(int),
|
|
|
|
"milliseconds": vol.Coerce(int),
|
|
|
|
}
|
|
|
|
),
|
|
|
|
has_at_least_one_key("days", "hours", "minutes", "seconds", "milliseconds"),
|
|
|
|
lambda value: timedelta(**value),
|
|
|
|
)
|
2016-04-21 22:52:20 +00:00
|
|
|
|
|
|
|
|
2019-09-20 15:23:34 +00:00
|
|
|
def time(value: Any) -> time_sys:
|
2017-09-28 21:57:49 +00:00
|
|
|
"""Validate and transform a time."""
|
|
|
|
if isinstance(value, time_sys):
|
|
|
|
return value
|
|
|
|
|
|
|
|
try:
|
|
|
|
time_val = dt_util.parse_time(value)
|
|
|
|
except TypeError:
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid("Not a parseable type")
|
2017-09-28 21:57:49 +00:00
|
|
|
|
|
|
|
if time_val is None:
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid("Invalid time specified: {}".format(value))
|
2017-09-28 21:57:49 +00:00
|
|
|
|
|
|
|
return time_val
|
|
|
|
|
|
|
|
|
2019-09-20 15:23:34 +00:00
|
|
|
def date(value: Any) -> date_sys:
|
2017-09-28 21:57:49 +00:00
|
|
|
"""Validate and transform a date."""
|
|
|
|
if isinstance(value, date_sys):
|
|
|
|
return value
|
|
|
|
|
|
|
|
try:
|
|
|
|
date_val = dt_util.parse_date(value)
|
|
|
|
except TypeError:
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid("Not a parseable type")
|
2017-09-28 21:57:49 +00:00
|
|
|
|
|
|
|
if date_val is None:
|
|
|
|
raise vol.Invalid("Could not parse date")
|
|
|
|
|
|
|
|
return date_val
|
|
|
|
|
|
|
|
|
2016-08-07 23:26:35 +00:00
|
|
|
def time_period_str(value: str) -> timedelta:
|
2016-04-04 19:18:58 +00:00
|
|
|
"""Validate and transform time offset."""
|
2016-04-28 10:03:57 +00:00
|
|
|
if isinstance(value, int):
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid("Make sure you wrap time values in quotes")
|
2019-02-27 21:10:40 +00:00
|
|
|
if not isinstance(value, str):
|
2016-04-28 10:03:57 +00:00
|
|
|
raise vol.Invalid(TIME_PERIOD_ERROR.format(value))
|
2016-04-04 19:18:58 +00:00
|
|
|
|
|
|
|
negative_offset = False
|
2019-07-31 19:25:30 +00:00
|
|
|
if value.startswith("-"):
|
2016-04-04 19:18:58 +00:00
|
|
|
negative_offset = True
|
|
|
|
value = value[1:]
|
2019-07-31 19:25:30 +00:00
|
|
|
elif value.startswith("+"):
|
2016-04-04 19:18:58 +00:00
|
|
|
value = value[1:]
|
|
|
|
|
|
|
|
try:
|
2019-07-31 19:25:30 +00:00
|
|
|
parsed = [int(x) for x in value.split(":")]
|
2016-04-04 19:18:58 +00:00
|
|
|
except ValueError:
|
2016-04-28 10:03:57 +00:00
|
|
|
raise vol.Invalid(TIME_PERIOD_ERROR.format(value))
|
2016-04-04 19:18:58 +00:00
|
|
|
|
|
|
|
if len(parsed) == 2:
|
|
|
|
hour, minute = parsed
|
|
|
|
second = 0
|
|
|
|
elif len(parsed) == 3:
|
|
|
|
hour, minute, second = parsed
|
|
|
|
else:
|
2016-04-28 10:03:57 +00:00
|
|
|
raise vol.Invalid(TIME_PERIOD_ERROR.format(value))
|
2016-04-04 19:18:58 +00:00
|
|
|
|
|
|
|
offset = timedelta(hours=hour, minutes=minute, seconds=second)
|
|
|
|
|
|
|
|
if negative_offset:
|
|
|
|
offset *= -1
|
|
|
|
|
|
|
|
return offset
|
|
|
|
|
|
|
|
|
2016-10-08 01:08:33 +00:00
|
|
|
def time_period_seconds(value: Union[int, str]) -> timedelta:
|
|
|
|
"""Validate and transform seconds to a time offset."""
|
|
|
|
try:
|
|
|
|
return timedelta(seconds=int(value))
|
|
|
|
except (ValueError, TypeError):
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid("Expected seconds, got {}".format(value))
|
2016-10-08 01:08:33 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
time_period = vol.Any(time_period_str, time_period_seconds, timedelta, time_period_dict)
|
2016-04-21 22:52:20 +00:00
|
|
|
|
|
|
|
|
2016-04-04 19:18:58 +00:00
|
|
|
def match_all(value):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Validate that matches all values."""
|
2016-04-04 19:18:58 +00:00
|
|
|
return value
|
|
|
|
|
|
|
|
|
2016-08-07 23:26:35 +00:00
|
|
|
def positive_timedelta(value: timedelta) -> timedelta:
|
2016-04-21 22:52:20 +00:00
|
|
|
"""Validate timedelta is positive."""
|
|
|
|
if value < timedelta(0):
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid("Time period should be positive")
|
2016-04-21 22:52:20 +00:00
|
|
|
return value
|
|
|
|
|
|
|
|
|
2019-10-02 20:14:52 +00:00
|
|
|
positive_time_period_dict = vol.All(time_period_dict, positive_timedelta)
|
|
|
|
|
|
|
|
|
2019-08-10 23:30:33 +00:00
|
|
|
def remove_falsy(value: List[T]) -> List[T]:
|
2019-04-03 02:43:06 +00:00
|
|
|
"""Remove falsy values from a list."""
|
|
|
|
return [v for v in value if v]
|
|
|
|
|
|
|
|
|
2016-04-03 17:19:09 +00:00
|
|
|
def service(value):
|
|
|
|
"""Validate service."""
|
|
|
|
# Services use same format as entities so we can use same helper.
|
2019-10-12 19:28:47 +00:00
|
|
|
value = string(value).lower()
|
2016-04-03 17:19:09 +00:00
|
|
|
if valid_entity_id(value):
|
|
|
|
return value
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid("Service {} does not match format <domain>.<name>".format(value))
|
2016-04-03 17:19:09 +00:00
|
|
|
|
|
|
|
|
2019-01-21 17:45:11 +00:00
|
|
|
def schema_with_slug_keys(value_schema: Union[T, Callable]) -> Callable:
|
|
|
|
"""Ensure dicts have slugs as keys.
|
|
|
|
|
|
|
|
Replacement of vol.Schema({cv.slug: value_schema}) to prevent misleading
|
|
|
|
"Extra keys" errors from voluptuous.
|
|
|
|
"""
|
|
|
|
schema = vol.Schema({str: value_schema})
|
|
|
|
|
|
|
|
def verify(value: Dict) -> Dict:
|
|
|
|
"""Validate all keys are slugs and then the value_schema."""
|
2019-01-22 00:36:04 +00:00
|
|
|
if not isinstance(value, dict):
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid("expected dictionary")
|
2019-01-22 00:36:04 +00:00
|
|
|
|
2019-01-21 17:45:11 +00:00
|
|
|
for key in value.keys():
|
2019-05-30 11:33:26 +00:00
|
|
|
slug(key)
|
2019-01-26 22:09:41 +00:00
|
|
|
|
2019-01-21 17:45:11 +00:00
|
|
|
return schema(value)
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2019-01-21 17:45:11 +00:00
|
|
|
return verify
|
|
|
|
|
|
|
|
|
|
|
|
def slug(value: Any) -> str:
|
2016-04-03 17:19:09 +00:00
|
|
|
"""Validate value is a valid slug."""
|
|
|
|
if value is None:
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid("Slug should not be None")
|
2016-04-03 17:19:09 +00:00
|
|
|
value = str(value)
|
2016-10-08 21:40:50 +00:00
|
|
|
slg = util_slugify(value)
|
2016-04-03 17:19:09 +00:00
|
|
|
if value == slg:
|
|
|
|
return value
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid("invalid slug {} (try {})".format(value, slg))
|
2016-04-03 17:19:09 +00:00
|
|
|
|
|
|
|
|
2019-01-21 17:45:11 +00:00
|
|
|
def slugify(value: Any) -> str:
|
2016-10-08 21:40:50 +00:00
|
|
|
"""Coerce a value to a slug."""
|
|
|
|
if value is None:
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid("Slug should not be None")
|
2016-10-08 21:40:50 +00:00
|
|
|
slg = util_slugify(str(value))
|
2017-04-24 03:41:09 +00:00
|
|
|
if slg:
|
2016-10-08 21:40:50 +00:00
|
|
|
return slg
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid("Unable to slugify {}".format(value))
|
2016-10-08 21:40:50 +00:00
|
|
|
|
|
|
|
|
2016-08-07 23:26:35 +00:00
|
|
|
def string(value: Any) -> str:
|
2016-04-02 07:51:03 +00:00
|
|
|
"""Coerce value to string, except for None."""
|
2018-10-07 10:35:44 +00:00
|
|
|
if value is None:
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid("string value is None")
|
2018-10-07 10:35:44 +00:00
|
|
|
if isinstance(value, (list, dict)):
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid("value should be a string")
|
2018-10-07 10:35:44 +00:00
|
|
|
|
|
|
|
return str(value)
|
2016-04-02 07:51:03 +00:00
|
|
|
|
|
|
|
|
2019-09-20 15:23:34 +00:00
|
|
|
def temperature_unit(value: Any) -> str:
|
2016-03-28 01:48:51 +00:00
|
|
|
"""Validate and transform temperature unit."""
|
2016-04-03 17:19:09 +00:00
|
|
|
value = str(value).upper()
|
2019-07-31 19:25:30 +00:00
|
|
|
if value == "C":
|
2016-04-20 03:30:44 +00:00
|
|
|
return TEMP_CELSIUS
|
2019-07-31 19:25:30 +00:00
|
|
|
if value == "F":
|
2016-04-03 17:19:09 +00:00
|
|
|
return TEMP_FAHRENHEIT
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid("invalid temperature unit (expected C or F)")
|
2016-04-03 17:19:09 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
unit_system = vol.All(
|
|
|
|
vol.Lower, vol.Any(CONF_UNIT_SYSTEM_METRIC, CONF_UNIT_SYSTEM_IMPERIAL)
|
|
|
|
)
|
2016-07-31 20:24:49 +00:00
|
|
|
|
|
|
|
|
2016-04-03 17:19:09 +00:00
|
|
|
def template(value):
|
|
|
|
"""Validate a jinja2 template."""
|
2019-05-01 02:54:25 +00:00
|
|
|
from homeassistant.helpers import template as template_helper
|
|
|
|
|
2016-04-03 17:19:09 +00:00
|
|
|
if value is None:
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid("template value is None")
|
2019-02-27 21:10:40 +00:00
|
|
|
if isinstance(value, (list, dict, template_helper.Template)):
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid("template value should be a string")
|
2016-04-03 17:19:09 +00:00
|
|
|
|
2016-09-28 04:29:55 +00:00
|
|
|
value = template_helper.Template(str(value))
|
|
|
|
|
2016-04-03 17:19:09 +00:00
|
|
|
try:
|
2016-09-28 04:29:55 +00:00
|
|
|
value.ensure_valid()
|
2016-04-03 17:19:09 +00:00
|
|
|
return value
|
2016-09-28 04:29:55 +00:00
|
|
|
except TemplateError as ex:
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid("invalid template ({})".format(ex))
|
2016-03-28 01:48:51 +00:00
|
|
|
|
|
|
|
|
2016-09-08 16:19:47 +00:00
|
|
|
def template_complex(value):
|
|
|
|
"""Validate a complex jinja2 template."""
|
|
|
|
if isinstance(value, list):
|
2019-02-06 19:15:27 +00:00
|
|
|
return_value = value.copy()
|
|
|
|
for idx, element in enumerate(return_value):
|
|
|
|
return_value[idx] = template_complex(element)
|
|
|
|
return return_value
|
2016-09-08 16:19:47 +00:00
|
|
|
if isinstance(value, dict):
|
2019-02-06 19:15:27 +00:00
|
|
|
return_value = value.copy()
|
|
|
|
for key, element in return_value.items():
|
|
|
|
return_value[key] = template_complex(element)
|
|
|
|
return return_value
|
2019-12-03 22:15:45 +00:00
|
|
|
if isinstance(value, str):
|
|
|
|
return template(value)
|
|
|
|
return value
|
2016-09-08 16:19:47 +00:00
|
|
|
|
|
|
|
|
2016-11-25 05:52:10 +00:00
|
|
|
def datetime(value):
|
|
|
|
"""Validate datetime."""
|
|
|
|
if isinstance(value, datetime_sys):
|
|
|
|
return value
|
|
|
|
|
|
|
|
try:
|
|
|
|
date_val = dt_util.parse_datetime(value)
|
|
|
|
except TypeError:
|
|
|
|
date_val = None
|
|
|
|
|
|
|
|
if date_val is None:
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid("Invalid datetime specified: {}".format(value))
|
2016-11-25 05:52:10 +00:00
|
|
|
|
|
|
|
return date_val
|
|
|
|
|
|
|
|
|
2016-03-28 01:48:51 +00:00
|
|
|
def time_zone(value):
|
|
|
|
"""Validate timezone."""
|
|
|
|
if dt_util.get_time_zone(value) is not None:
|
|
|
|
return value
|
|
|
|
raise vol.Invalid(
|
2019-07-31 19:25:30 +00:00
|
|
|
"Invalid time zone passed in. Valid options can be found here: "
|
|
|
|
"http://en.wikipedia.org/wiki/List_of_tz_database_time_zones"
|
|
|
|
)
|
2016-04-03 17:19:09 +00:00
|
|
|
|
2016-11-19 05:47:59 +00:00
|
|
|
|
2016-04-28 10:03:57 +00:00
|
|
|
weekdays = vol.All(ensure_list, [vol.In(WEEKDAYS)])
|
|
|
|
|
2016-04-03 17:19:09 +00:00
|
|
|
|
2016-10-22 09:05:00 +00:00
|
|
|
def socket_timeout(value):
|
|
|
|
"""Validate timeout float > 0.0.
|
|
|
|
|
|
|
|
None coerced to socket._GLOBAL_DEFAULT_TIMEOUT bare object.
|
|
|
|
"""
|
|
|
|
if value is None:
|
|
|
|
return _GLOBAL_DEFAULT_TIMEOUT
|
2018-07-23 08:16:05 +00:00
|
|
|
try:
|
|
|
|
float_value = float(value)
|
|
|
|
if float_value > 0.0:
|
|
|
|
return float_value
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid("Invalid socket timeout value." " float > 0.0 required.")
|
2018-07-23 08:16:05 +00:00
|
|
|
except Exception as _:
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid("Invalid socket timeout: {err}".format(err=_))
|
2016-10-22 09:05:00 +00:00
|
|
|
|
|
|
|
|
2016-08-19 11:41:01 +00:00
|
|
|
# pylint: disable=no-value-for-parameter
|
|
|
|
def url(value: Any) -> str:
|
|
|
|
"""Validate an URL."""
|
|
|
|
url_in = str(value)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if urlparse(url_in).scheme in ["http", "https"]:
|
2016-08-19 11:41:01 +00:00
|
|
|
return vol.Schema(vol.Url())(url_in)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid("invalid url")
|
2016-08-19 11:41:01 +00:00
|
|
|
|
|
|
|
|
2016-10-25 04:49:49 +00:00
|
|
|
def x10_address(value):
|
|
|
|
"""Validate an x10 address."""
|
2019-07-31 19:25:30 +00:00
|
|
|
regex = re.compile(r"([A-Pa-p]{1})(?:[2-9]|1[0-6]?)$")
|
2016-10-25 04:49:49 +00:00
|
|
|
if not regex.match(value):
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid("Invalid X10 Address")
|
2016-10-25 04:49:49 +00:00
|
|
|
return str(value).lower()
|
|
|
|
|
|
|
|
|
2019-03-28 04:53:11 +00:00
|
|
|
def uuid4_hex(value):
|
|
|
|
"""Validate a v4 UUID in hex format."""
|
|
|
|
try:
|
|
|
|
result = UUID(value, version=4)
|
|
|
|
except (ValueError, AttributeError, TypeError) as error:
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid("Invalid Version4 UUID", error_message=str(error))
|
2019-03-28 04:53:11 +00:00
|
|
|
|
|
|
|
if result.hex != value.lower():
|
|
|
|
# UUID() will create a uuid4 if input is invalid
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid("Invalid Version4 UUID")
|
2019-03-28 04:53:11 +00:00
|
|
|
|
|
|
|
return result.hex
|
|
|
|
|
|
|
|
|
2019-08-10 23:30:33 +00:00
|
|
|
def ensure_list_csv(value: Any) -> List:
|
2017-01-22 19:19:50 +00:00
|
|
|
"""Ensure that input is a list or make one from comma-separated string."""
|
|
|
|
if isinstance(value, str):
|
2019-07-31 19:25:30 +00:00
|
|
|
return [member.strip() for member in value.split(",")]
|
2017-01-22 19:19:50 +00:00
|
|
|
return ensure_list(value)
|
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
def deprecated(
|
|
|
|
key: str,
|
|
|
|
replacement_key: Optional[str] = None,
|
|
|
|
invalidation_version: Optional[str] = None,
|
|
|
|
default: Optional[Any] = None,
|
2019-09-20 15:23:34 +00:00
|
|
|
) -> Callable[[Dict], Dict]:
|
2019-02-08 10:14:50 +00:00
|
|
|
"""
|
|
|
|
Log key as deprecated and provide a replacement (if exists).
|
|
|
|
|
|
|
|
Expected behavior:
|
|
|
|
- Outputs the appropriate deprecation warning if key is detected
|
|
|
|
- Processes schema moving the value from key to replacement_key
|
|
|
|
- Processes schema changing nothing if only replacement_key provided
|
|
|
|
- No warning if only replacement_key provided
|
|
|
|
- No warning if neither key nor replacement_key are provided
|
|
|
|
- Adds replacement_key with default value in this case
|
|
|
|
- Once the invalidation_version is crossed, raises vol.Invalid if key
|
|
|
|
is detected
|
|
|
|
"""
|
2019-07-11 07:38:58 +00:00
|
|
|
module = inspect.getmodule(inspect.stack()[1][0])
|
|
|
|
if module is not None:
|
|
|
|
module_name = module.__name__
|
|
|
|
else:
|
2019-10-05 09:59:34 +00:00
|
|
|
# If Python is unable to access the sources files, the call stack frame
|
|
|
|
# will be missing information, so let's guard.
|
2019-07-11 07:38:58 +00:00
|
|
|
# https://github.com/home-assistant/home-assistant/issues/24982
|
2019-09-27 19:57:59 +00:00
|
|
|
module_name = __name__
|
2018-01-10 08:06:26 +00:00
|
|
|
|
2019-02-08 10:14:50 +00:00
|
|
|
if replacement_key and invalidation_version:
|
2019-07-31 19:25:30 +00:00
|
|
|
warning = (
|
|
|
|
"The '{key}' option (with value '{value}') is"
|
|
|
|
" deprecated, please replace it with '{replacement_key}'."
|
|
|
|
" This option will become invalid in version"
|
|
|
|
" {invalidation_version}"
|
|
|
|
)
|
2019-02-08 10:14:50 +00:00
|
|
|
elif replacement_key:
|
2019-07-31 19:25:30 +00:00
|
|
|
warning = (
|
|
|
|
"The '{key}' option (with value '{value}') is"
|
|
|
|
" deprecated, please replace it with '{replacement_key}'"
|
|
|
|
)
|
2019-02-08 10:14:50 +00:00
|
|
|
elif invalidation_version:
|
2019-07-31 19:25:30 +00:00
|
|
|
warning = (
|
|
|
|
"The '{key}' option (with value '{value}') is"
|
|
|
|
" deprecated, please remove it from your configuration."
|
|
|
|
" This option will become invalid in version"
|
|
|
|
" {invalidation_version}"
|
|
|
|
)
|
2019-02-08 10:14:50 +00:00
|
|
|
else:
|
2019-07-31 19:25:30 +00:00
|
|
|
warning = (
|
|
|
|
"The '{key}' option (with value '{value}') is"
|
|
|
|
" deprecated, please remove it from your configuration"
|
|
|
|
)
|
2019-02-08 10:14:50 +00:00
|
|
|
|
2019-09-20 15:23:34 +00:00
|
|
|
def check_for_invalid_version(value: Optional[Any]) -> None:
|
2019-02-08 10:14:50 +00:00
|
|
|
"""Raise error if current version has reached invalidation."""
|
|
|
|
if not invalidation_version:
|
|
|
|
return
|
|
|
|
|
|
|
|
if parse_version(__version__) >= parse_version(invalidation_version):
|
|
|
|
raise vol.Invalid(
|
|
|
|
warning.format(
|
|
|
|
key=key,
|
|
|
|
value=value,
|
|
|
|
replacement_key=replacement_key,
|
2019-07-31 19:25:30 +00:00
|
|
|
invalidation_version=invalidation_version,
|
2019-02-08 10:14:50 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2019-09-20 15:23:34 +00:00
|
|
|
def validator(config: Dict) -> Dict:
|
2018-01-10 08:06:26 +00:00
|
|
|
"""Check if key is in config and log warning."""
|
|
|
|
if key in config:
|
2019-02-08 10:14:50 +00:00
|
|
|
value = config[key]
|
|
|
|
check_for_invalid_version(value)
|
|
|
|
KeywordStyleAdapter(logging.getLogger(module_name)).warning(
|
|
|
|
warning,
|
|
|
|
key=key,
|
|
|
|
value=value,
|
|
|
|
replacement_key=replacement_key,
|
2019-07-31 19:25:30 +00:00
|
|
|
invalidation_version=invalidation_version,
|
2019-02-08 10:14:50 +00:00
|
|
|
)
|
|
|
|
if replacement_key:
|
|
|
|
config.pop(key)
|
|
|
|
else:
|
|
|
|
value = default
|
2019-07-21 16:59:02 +00:00
|
|
|
keys = [key]
|
|
|
|
if replacement_key:
|
|
|
|
keys.append(replacement_key)
|
|
|
|
if value is not None and (
|
2019-07-31 19:25:30 +00:00
|
|
|
replacement_key not in config or default == config.get(replacement_key)
|
|
|
|
):
|
2019-07-21 16:59:02 +00:00
|
|
|
config[replacement_key] = value
|
2019-02-08 10:14:50 +00:00
|
|
|
|
2019-07-21 16:59:02 +00:00
|
|
|
return has_at_most_one_key(*keys)(config)
|
2018-01-10 08:06:26 +00:00
|
|
|
|
|
|
|
return validator
|
|
|
|
|
|
|
|
|
2016-04-03 17:19:09 +00:00
|
|
|
# Validator helpers
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2016-04-04 19:18:58 +00:00
|
|
|
def key_dependency(key, dependency):
|
|
|
|
"""Validate that all dependencies exist for key."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2016-04-04 19:18:58 +00:00
|
|
|
def validator(value):
|
|
|
|
"""Test dependencies."""
|
|
|
|
if not isinstance(value, dict):
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid("key dependencies require a dict")
|
2016-04-04 19:18:58 +00:00
|
|
|
if key in value and dependency not in value:
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid(
|
|
|
|
'dependency violation - key "{}" requires '
|
|
|
|
'key "{}" to exist'.format(key, dependency)
|
|
|
|
)
|
2016-04-03 17:19:09 +00:00
|
|
|
|
2016-04-04 19:18:58 +00:00
|
|
|
return value
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2016-04-04 19:18:58 +00:00
|
|
|
return validator
|
2016-04-03 17:19:09 +00:00
|
|
|
|
|
|
|
|
2019-10-02 20:14:52 +00:00
|
|
|
def custom_serializer(schema):
|
|
|
|
"""Serialize additional types for voluptuous_serialize."""
|
|
|
|
if schema is positive_time_period_dict:
|
|
|
|
return {"type": "positive_time_period_dict"}
|
|
|
|
|
|
|
|
return voluptuous_serialize.UNSUPPORTED
|
|
|
|
|
|
|
|
|
2016-04-03 17:19:09 +00:00
|
|
|
# Schemas
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_PLATFORM): string,
|
|
|
|
vol.Optional(CONF_ENTITY_NAMESPACE): string,
|
|
|
|
vol.Optional(CONF_SCAN_INTERVAL): time_period,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
PLATFORM_SCHEMA_BASE = PLATFORM_SCHEMA.extend({}, extra=vol.ALLOW_EXTRA)
|
|
|
|
|
2019-12-03 00:23:12 +00:00
|
|
|
|
|
|
|
def make_entity_service_schema(
|
|
|
|
schema: dict, *, extra: int = vol.PREVENT_EXTRA
|
|
|
|
) -> vol.All:
|
|
|
|
"""Create an entity service schema."""
|
|
|
|
return vol.All(
|
|
|
|
vol.Schema(
|
|
|
|
{
|
|
|
|
**schema,
|
|
|
|
vol.Optional(ATTR_ENTITY_ID): comp_entity_ids,
|
|
|
|
vol.Optional(ATTR_AREA_ID): vol.All(ensure_list, [str]),
|
|
|
|
},
|
|
|
|
extra=extra,
|
|
|
|
),
|
|
|
|
has_at_least_one_key(ATTR_ENTITY_ID, ATTR_AREA_ID),
|
|
|
|
)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
|
|
|
|
EVENT_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_ALIAS): string,
|
|
|
|
vol.Required("event"): string,
|
|
|
|
vol.Optional("event_data"): dict,
|
|
|
|
vol.Optional("event_data_template"): {match_all: template_complex},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
SERVICE_SCHEMA = vol.All(
|
|
|
|
vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_ALIAS): string,
|
|
|
|
vol.Exclusive("service", "service name"): service,
|
|
|
|
vol.Exclusive("service_template", "service name"): template,
|
|
|
|
vol.Optional("data"): dict,
|
|
|
|
vol.Optional("data_template"): {match_all: template_complex},
|
|
|
|
vol.Optional(CONF_ENTITY_ID): comp_entity_ids,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
has_at_least_one_key("service", "service_template"),
|
|
|
|
)
|
|
|
|
|
|
|
|
NUMERIC_STATE_CONDITION_SCHEMA = vol.All(
|
|
|
|
vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_CONDITION): "numeric_state",
|
|
|
|
vol.Required(CONF_ENTITY_ID): entity_id,
|
|
|
|
CONF_BELOW: vol.Coerce(float),
|
|
|
|
CONF_ABOVE: vol.Coerce(float),
|
|
|
|
vol.Optional(CONF_VALUE_TEMPLATE): template,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
has_at_least_one_key(CONF_BELOW, CONF_ABOVE),
|
|
|
|
)
|
|
|
|
|
|
|
|
STATE_CONDITION_SCHEMA = vol.All(
|
|
|
|
vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_CONDITION): "state",
|
|
|
|
vol.Required(CONF_ENTITY_ID): entity_id,
|
2019-09-05 14:49:32 +00:00
|
|
|
vol.Required(CONF_STATE): str,
|
|
|
|
vol.Optional(CONF_FOR): vol.All(time_period, positive_timedelta),
|
2019-07-31 19:25:30 +00:00
|
|
|
# To support use_trigger_value in automation
|
|
|
|
# Deprecated 2016/04/25
|
|
|
|
vol.Optional("from"): str,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
key_dependency("for", "state"),
|
|
|
|
)
|
|
|
|
|
|
|
|
SUN_CONDITION_SCHEMA = vol.All(
|
|
|
|
vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_CONDITION): "sun",
|
|
|
|
vol.Optional("before"): sun_event,
|
|
|
|
vol.Optional("before_offset"): time_period,
|
|
|
|
vol.Optional("after"): vol.All(
|
|
|
|
vol.Lower, vol.Any(SUN_EVENT_SUNSET, SUN_EVENT_SUNRISE)
|
|
|
|
),
|
|
|
|
vol.Optional("after_offset"): time_period,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
has_at_least_one_key("before", "after"),
|
|
|
|
)
|
|
|
|
|
|
|
|
TEMPLATE_CONDITION_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_CONDITION): "template",
|
|
|
|
vol.Required(CONF_VALUE_TEMPLATE): template,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
TIME_CONDITION_SCHEMA = vol.All(
|
|
|
|
vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_CONDITION): "time",
|
|
|
|
"before": time,
|
|
|
|
"after": time,
|
|
|
|
"weekday": weekdays,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
has_at_least_one_key("before", "after", "weekday"),
|
|
|
|
)
|
|
|
|
|
|
|
|
ZONE_CONDITION_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_CONDITION): "zone",
|
|
|
|
vol.Required(CONF_ENTITY_ID): entity_id,
|
|
|
|
"zone": entity_id,
|
|
|
|
# To support use_trigger_value in automation
|
|
|
|
# Deprecated 2016/04/25
|
|
|
|
vol.Optional("event"): vol.Any("enter", "leave"),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
AND_CONDITION_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_CONDITION): "and",
|
|
|
|
vol.Required("conditions"): vol.All(
|
|
|
|
ensure_list,
|
|
|
|
# pylint: disable=unnecessary-lambda
|
|
|
|
[lambda value: CONDITION_SCHEMA(value)],
|
|
|
|
),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
OR_CONDITION_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_CONDITION): "or",
|
|
|
|
vol.Required("conditions"): vol.All(
|
|
|
|
ensure_list,
|
|
|
|
# pylint: disable=unnecessary-lambda
|
|
|
|
[lambda value: CONDITION_SCHEMA(value)],
|
|
|
|
),
|
|
|
|
}
|
|
|
|
)
|
2016-04-28 10:03:57 +00:00
|
|
|
|
2019-09-24 21:57:05 +00:00
|
|
|
DEVICE_CONDITION_BASE_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_CONDITION): "device",
|
|
|
|
vol.Required(CONF_DEVICE_ID): str,
|
|
|
|
vol.Required(CONF_DOMAIN): str,
|
|
|
|
}
|
2019-09-05 14:49:32 +00:00
|
|
|
)
|
|
|
|
|
2019-09-24 21:57:05 +00:00
|
|
|
DEVICE_CONDITION_SCHEMA = DEVICE_CONDITION_BASE_SCHEMA.extend({}, extra=vol.ALLOW_EXTRA)
|
|
|
|
|
2019-09-04 03:36:04 +00:00
|
|
|
CONDITION_SCHEMA: vol.Schema = vol.Any(
|
2016-04-28 10:03:57 +00:00
|
|
|
NUMERIC_STATE_CONDITION_SCHEMA,
|
|
|
|
STATE_CONDITION_SCHEMA,
|
|
|
|
SUN_CONDITION_SCHEMA,
|
|
|
|
TEMPLATE_CONDITION_SCHEMA,
|
|
|
|
TIME_CONDITION_SCHEMA,
|
|
|
|
ZONE_CONDITION_SCHEMA,
|
2016-05-11 04:49:58 +00:00
|
|
|
AND_CONDITION_SCHEMA,
|
|
|
|
OR_CONDITION_SCHEMA,
|
2019-09-05 14:49:32 +00:00
|
|
|
DEVICE_CONDITION_SCHEMA,
|
2019-09-04 03:36:04 +00:00
|
|
|
)
|
2016-04-28 10:03:57 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
_SCRIPT_DELAY_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_ALIAS): string,
|
|
|
|
vol.Required("delay"): vol.Any(
|
|
|
|
vol.All(time_period, positive_timedelta), template, template_complex
|
|
|
|
),
|
|
|
|
}
|
|
|
|
)
|
2016-04-21 22:52:20 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
_SCRIPT_WAIT_TEMPLATE_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_ALIAS): string,
|
|
|
|
vol.Required("wait_template"): template,
|
|
|
|
vol.Optional(CONF_TIMEOUT): vol.All(time_period, positive_timedelta),
|
|
|
|
vol.Optional("continue_on_timeout"): boolean,
|
|
|
|
}
|
|
|
|
)
|
2017-02-12 21:27:53 +00:00
|
|
|
|
2019-09-24 21:57:05 +00:00
|
|
|
DEVICE_ACTION_BASE_SCHEMA = vol.Schema(
|
|
|
|
{vol.Required(CONF_DEVICE_ID): string, vol.Required(CONF_DOMAIN): str}
|
2019-09-05 23:26:22 +00:00
|
|
|
)
|
|
|
|
|
2019-09-24 21:57:05 +00:00
|
|
|
DEVICE_ACTION_SCHEMA = DEVICE_ACTION_BASE_SCHEMA.extend({}, extra=vol.ALLOW_EXTRA)
|
|
|
|
|
2019-10-05 20:30:43 +00:00
|
|
|
_SCRIPT_SCENE_SCHEMA = vol.Schema({vol.Required("scene"): entity_domain("scene")})
|
|
|
|
|
2016-04-21 22:52:20 +00:00
|
|
|
SCRIPT_SCHEMA = vol.All(
|
|
|
|
ensure_list,
|
2019-07-31 19:25:30 +00:00
|
|
|
[
|
|
|
|
vol.Any(
|
|
|
|
SERVICE_SCHEMA,
|
|
|
|
_SCRIPT_DELAY_SCHEMA,
|
|
|
|
_SCRIPT_WAIT_TEMPLATE_SCHEMA,
|
|
|
|
EVENT_SCHEMA,
|
|
|
|
CONDITION_SCHEMA,
|
2019-09-05 23:26:22 +00:00
|
|
|
DEVICE_ACTION_SCHEMA,
|
2019-10-05 20:30:43 +00:00
|
|
|
_SCRIPT_SCENE_SCHEMA,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
|
|
|
],
|
2016-04-21 22:52:20 +00:00
|
|
|
)
|