예제 #1
0
    def validator(value):
        from esphome.yaml_util import make_data_base

        value = make_data_base(one_of_validator(value))
        cls = value.__class__
        value.__class__ = cls.__class__(cls.__name__ + "Enum", (cls, core.EnumValue), {})
        value.enum_value = mapping[value]
        return value
예제 #2
0
def lambda_(value):
    """Coerce this configuration option to a lambda."""
    if not isinstance(value, Lambda):
        value = make_data_base(Lambda(string_strict(value)), value)
    entity_id_parts = re.split(LAMBDA_ENTITY_ID_PROG, value.value)
    if len(entity_id_parts) != 1:
        entity_ids = " ".join("'{}'".format(entity_id_parts[i])
                              for i in range(1, len(entity_id_parts), 2))
        raise Invalid(
            "Lambda contains reference to entity-id-style ID {}. "
            "The id() wrapper only works for ESPHome-internal types. For importing "
            "states from Home Assistant use the 'homeassistant' sensor platforms."
            "".format(entity_ids))
    return value
예제 #3
0
def _expand_substitutions(substitutions, value, path, ignore_missing):
    if "$" not in value:
        return value

    orig_value = value

    i = 0
    while True:
        m = VARIABLE_PROG.search(value, i)
        if not m:
            # Nothing more to match. Done
            break

        i, j = m.span(0)
        name = m.group(1)
        if name.startswith("{") and name.endswith("}"):
            name = name[1:-1]
        if name not in substitutions:
            if not ignore_missing:
                _LOGGER.warning(
                    "Found '%s' (see %s) which looks like a substitution, but '%s' was "
                    "not declared",
                    orig_value,
                    "->".join(str(x) for x in path),
                    name,
                )
            i = j
            continue

        sub = substitutions[name]
        tail = value[j:]
        value = value[:i] + sub
        i = len(value)
        value += tail

    # orig_value can also already be a lambda with esp_range info, and only
    # a plain string is sent in orig_value
    if isinstance(orig_value, ESPHomeDataBase):
        # even though string can get larger or smaller, the range should point
        # to original document marks
        return make_data_base(value, orig_value)

    return value
예제 #4
0
    "BOTH": UARTDirection.UART_DIRECTION_BOTH,
}

# The reason for having CONF_BYTES at 150 by default:
#
# The log message buffer size is 512 bytes by default. About 35 bytes are
# used for the log prefix. That leaves us with 477 bytes for logging data.
# The default log output is hex, which uses 3 characters per represented
# byte (2 hex chars + 1 separator). That means that 477 / 3 = 159 bytes
# can be represented in a single log line. Using 150, because people love
# round numbers.
AFTER_DEFAULTS = {CONF_BYTES: 150, CONF_TIMEOUT: "100ms"}

# By default, log in hex format when no specific sequence is provided.
DEFAULT_DEBUG_OUTPUT = "UARTDebug::log_hex(direction, bytes, ':');"
DEFAULT_SEQUENCE = [{CONF_LAMBDA: make_data_base(DEFAULT_DEBUG_OUTPUT)}]


def maybe_empty_debug(value):
    if value is None:
        value = {}
    return DEBUG_SCHEMA(value)


DEBUG_SCHEMA = cv.Schema({
    cv.GenerateID(CONF_TRIGGER_ID):
    cv.declare_id(UARTDebugger),
    cv.Optional(CONF_DIRECTION, default="BOTH"):
    cv.enum(UART_DIRECTIONS, upper=True),
    cv.Optional(CONF_AFTER, default=AFTER_DEFAULTS):
    cv.Schema({