Esempio n. 1
0
def process_lambda(
        value: Lambda, parameters: List[Tuple[SafeExpType, str]],
        capture: str = '=', return_type: SafeExpType = None
) -> Generator[LambdaExpression, None, None]:
    """Process the given lambda value into a LambdaExpression.

    This is a coroutine because lambdas can depend on other IDs,
    you need to await it with 'yield'!

    :param value: The lambda to process.
    :param parameters: The parameters to pass to the Lambda, list of tuples
    :param capture: The capture expression for the lambda, usually ''.
    :param return_type: The return type of the lambda.
    :return: The generated lambda expression.
    """
    from esphome.components.globals import GlobalsComponent

    if value is None:
        yield
        return
    parts = value.parts[:]
    for i, id in enumerate(value.requires_ids):
        full_id, var = yield CORE.get_variable_with_full_id(id)
        if full_id is not None and isinstance(full_id.type, MockObjClass) and \
                full_id.type.inherits_from(GlobalsComponent):
            parts[i * 3 + 1] = var.value()
            continue

        if parts[i * 3 + 2] == '.':
            parts[i * 3 + 1] = var._
        else:
            parts[i * 3 + 1] = var
        parts[i * 3 + 2] = ''
    yield LambdaExpression(parts, parameters, capture, return_type, value.source_location)
Esempio n. 2
0
def process_lambda(
        value,  # type: Lambda
        parameters,  # type: List[Tuple[Expression, str]]
        capture='=',  # type: str
        return_type=None  # type: Optional[Expression]
):
    # type: (...) -> Generator[LambdaExpression]
    from esphome.components.globals import GlobalVariableComponent

    if value is None:
        yield
        return
    parts = value.parts[:]
    for i, id in enumerate(value.requires_ids):
        for full_id, var in CORE.get_variable_with_full_id(id):
            yield
        if full_id is not None and isinstance(full_id.type, MockObjClass) and \
                full_id.type.inherits_from(GlobalVariableComponent):
            parts[i * 3 + 1] = var.value()
            continue

        if parts[i * 3 + 2] == '.':
            parts[i * 3 + 1] = var._
        else:
            parts[i * 3 + 1] = var
        parts[i * 3 + 2] = ''
    yield LambdaExpression(parts, parameters, capture, return_type)
Esempio n. 3
0
def get_variable_with_full_id(id_: ID) -> Generator[Tuple[ID, "MockObj"], None, None]:
    """
    Wait for the given ID to be defined in the code generation and
    return it as a MockObj.

    This is a coroutine, you need to await it with a 'yield' expression!

    :param id_: The ID to retrieve
    :return: The variable as a MockObj.
    """
    full_id, var = yield CORE.get_variable_with_full_id(id_)
    yield full_id, var