Example #1
0
def to_code(config):
    for spi_ in get_variable(config[CONF_SPI_ID]):
        yield
    for cs in gpio_output_pin_expression(config[CONF_CS_PIN]):
        yield
    for dc in gpio_output_pin_expression(config[CONF_DC_PIN]):
        yield

    rhs = App.make_spi_ssd1306(spi_, cs, dc)
    ssd = Pvariable(config[CONF_ID], rhs)
    add(ssd.set_model(MODELS[config[CONF_MODEL]]))

    if CONF_RESET_PIN in config:
        for reset in gpio_output_pin_expression(config[CONF_RESET_PIN]):
            yield
        add(ssd.set_reset_pin(reset))
    if CONF_EXTERNAL_VCC in config:
        add(ssd.set_external_vcc(config[CONF_EXTERNAL_VCC]))
    if CONF_LAMBDA in config:
        for lambda_ in process_lambda(config[CONF_LAMBDA],
                                      [(display.DisplayBufferRef, 'it')], return_type=void):
            yield
        add(ssd.set_writer(lambda_))

    display.setup_display(ssd, config)
    setup_component(ssd, config)
Example #2
0
def to_code(config):
    rhs = App.make_template_switch(config[CONF_NAME])
    make = variable(config[CONF_MAKE_ID], rhs)
    template = make.Ptemplate_

    switch.setup_switch(template, make.Pmqtt, config)

    if CONF_LAMBDA in config:
        for template_ in process_lambda(config[CONF_LAMBDA], [],
                                        return_type=optional.template(bool_)):
            yield
        add(template.set_state_lambda(template_))
    if CONF_TURN_OFF_ACTION in config:
        automation.build_automation(template.get_turn_off_trigger(), NoArg,
                                    config[CONF_TURN_OFF_ACTION])
    if CONF_TURN_ON_ACTION in config:
        automation.build_automation(template.get_turn_on_trigger(), NoArg,
                                    config[CONF_TURN_ON_ACTION])
    if CONF_OPTIMISTIC in config:
        add(template.set_optimistic(config[CONF_OPTIMISTIC]))

    if CONF_RESTORE_STATE in config:
        add(template.set_restore_state(config[CONF_RESTORE_STATE]))

    setup_component(template, config)
Example #3
0
def to_code(config):
    rhs = App.make_gpio_lcd_display(config[CONF_DIMENSIONS][0],
                                    config[CONF_DIMENSIONS][1])
    lcd = Pvariable(config[CONF_ID], rhs)
    pins_ = []
    for conf in config[CONF_DATA_PINS]:
        for pin in gpio_output_pin_expression(conf):
            yield
        pins_.append(pin)
    add(lcd.set_data_pins(*pins_))
    for enable in gpio_output_pin_expression(config[CONF_ENABLE_PIN]):
        yield
    add(lcd.set_enable_pin(enable))

    for rs in gpio_output_pin_expression(config[CONF_RS_PIN]):
        yield
    add(lcd.set_rs_pin(rs))

    if CONF_RW_PIN in config:
        for rw in gpio_output_pin_expression(config[CONF_RW_PIN]):
            yield
        add(lcd.set_rw_pin(rw))

    if CONF_LAMBDA in config:
        for lambda_ in process_lambda(config[CONF_LAMBDA],
                                      [(LCDDisplayRef, 'it')],
                                      return_type=void):
            yield
        add(lcd.set_writer(lambda_))

    display.setup_display(lcd, config)
    setup_component(lcd, config)
Example #4
0
def lambda_condition_to_code(config, condition_id, arg_type, template_arg):
    for lambda_ in process_lambda(config, [(arg_type, 'x')],
                                  return_type=bool_):
        yield
    rhs = LambdaCondition.new(template_arg, lambda_)
    type = LambdaCondition.template(template_arg)
    yield Pvariable(condition_id, rhs, type=type)
Example #5
0
def to_code(config):
    for template_ in process_lambda(config[CONF_LAMBDA], [],
                                    return_type=std_vector.template(sensor.SensorPtr)):
        yield

    rhs = CustomSensorConstructor(template_)
    custom = variable(config[CONF_ID], rhs)
    for i, sens in enumerate(config[CONF_SENSORS]):
        sensor.register_sensor(custom.get_sensor(i), sens)
Example #6
0
def to_code(config):
    for template_ in process_lambda(config[CONF_LAMBDA], [],
                                    return_type=std_vector.template(
                                        switch.SwitchPtr)):
        yield

    rhs = CustomSwitchConstructor(template_)
    custom = variable(config[CONF_ID], rhs)
    for i, sens in enumerate(config[CONF_SWITCHES]):
        switch.register_switch(custom.get_switch(i), sens)
Example #7
0
def to_code(config):
    for template_ in process_lambda(
            config[CONF_LAMBDA], [],
            return_type=std_vector.template(ComponentPtr)):
        yield

    rhs = CustomComponentConstructor(template_)
    custom = variable(config[CONF_ID], rhs)
    for i, comp in enumerate(config.get(CONF_COMPONENTS, [])):
        setup_component(custom.get_component(i), comp)
Example #8
0
def logger_log_action_to_code(config, action_id, arg_type, template_arg):
    esp_log = LOG_LEVEL_TO_ESP_LOG[config[CONF_LEVEL]]
    args = [RawExpression(text_type(x)) for x in config[CONF_ARGS]]

    text = text_type(statement(esp_log(config[CONF_TAG], config[CONF_FORMAT], *args)))

    for lambda_ in process_lambda(Lambda(text), [(arg_type, 'x')], return_type=void):
        yield None
    rhs = LambdaAction.new(template_arg, lambda_)
    type = LambdaAction.template(template_arg)
    yield Pvariable(action_id, rhs, type=type)
Example #9
0
def to_code(config):
    rhs = App.make_template_binary_sensor(config[CONF_NAME])
    make = variable(config[CONF_MAKE_ID], rhs)
    binary_sensor.setup_binary_sensor(make.Ptemplate_, make.Pmqtt, config)
    setup_component(make.Ptemplate_, config)

    template_ = None
    for template_ in process_lambda(config[CONF_LAMBDA], [],
                                    return_type=optional.template(bool_)):
        yield
    add(make.Ptemplate_.set_template(template_))
Example #10
0
def to_code(config):
    rhs = App.make_template_text_sensor(config[CONF_NAME],
                                        config.get(CONF_UPDATE_INTERVAL))
    make = variable(config[CONF_MAKE_ID], rhs)
    template = make.Ptemplate_
    text_sensor.setup_text_sensor(template, make.Pmqtt, config)
    setup_component(template, config)

    for template_ in process_lambda(config[CONF_LAMBDA], [],
                                    return_type=optional.template(std_string)):
        yield
    add(template.set_template(template_))
Example #11
0
def to_code(config):
    for uart_ in get_variable(config[CONF_UART_ID]):
        yield
    rhs = App.make_nextion(uart_)
    nextion = Pvariable(config[CONF_ID], rhs)

    if CONF_LAMBDA in config:
        for lambda_ in process_lambda(config[CONF_LAMBDA],
                                      [(NextionRef, 'it')]):
            yield
        add(nextion.set_writer(lambda_))

    display.setup_display(nextion, config)
    setup_component(nextion, config)
Example #12
0
def to_code(config):
    type = config[CONF_TYPE]
    if type == 'binary':
        ret_type = output.BinaryOutputPtr
        klass = CustomBinaryOutputConstructor
    else:
        ret_type = output.FloatOutputPtr
        klass = CustomFloatOutputConstructor
    for template_ in process_lambda(config[CONF_LAMBDA], [],
                                    return_type=std_vector.template(ret_type)):
        yield

    rhs = klass(template_)
    custom = variable(config[CONF_ID], rhs)
    for i, sens in enumerate(config[CONF_OUTPUTS]):
        output.register_output(custom.get_output(i), sens)
Example #13
0
def to_code(config):
    rhs = App.make_pcf8574_lcd_display(config[CONF_DIMENSIONS][0],
                                       config[CONF_DIMENSIONS][1])
    lcd = Pvariable(config[CONF_ID], rhs)

    if CONF_ADDRESS in config:
        add(lcd.set_address(config[CONF_ADDRESS]))

    if CONF_LAMBDA in config:
        for lambda_ in process_lambda(config[CONF_LAMBDA],
                                      [(LCDDisplayRef, 'it')]):
            yield
        add(lcd.set_writer(lambda_))

    display.setup_display(lcd, config)
    setup_component(lcd, config)
Example #14
0
def setup_filter(config):
    if CONF_INVERT in config:
        yield InvertFilter.new()
    elif CONF_DELAYED_OFF in config:
        yield App.register_component(
            DelayedOffFilter.new(config[CONF_DELAYED_OFF]))
    elif CONF_DELAYED_ON in config:
        yield App.register_component(
            DelayedOnFilter.new(config[CONF_DELAYED_ON]))
    elif CONF_HEARTBEAT in config:
        yield App.register_component(
            HeartbeatFilter.new(config[CONF_HEARTBEAT]))
    elif CONF_LAMBDA in config:
        lambda_ = None
        for lambda_ in process_lambda(config[CONF_LAMBDA], [(bool_, 'x')]):
            yield None
        yield LambdaFilter.new(lambda_)
Example #15
0
def mqtt_publish_json_action_to_code(config, action_id, arg_type,
                                     template_arg):
    rhs = App.Pget_mqtt_client().Pmake_publish_json_action(template_arg)
    type = MQTTPublishJsonAction.template(template_arg)
    action = Pvariable(action_id, rhs, type=type)
    for template_ in templatable(config[CONF_TOPIC], arg_type, std_string):
        yield None
    add(action.set_topic(template_))

    for lambda_ in process_lambda(config[CONF_PAYLOAD],
                                  [(arg_type, 'x'), (JsonObjectRef, 'root')]):
        yield None
    add(action.set_payload(lambda_))
    if CONF_QOS in config:
        add(action.set_qos(config[CONF_QOS]))
    if CONF_RETAIN in config:
        add(action.set_retain(config[CONF_RETAIN]))
    yield action
Example #16
0
def to_code(config):
    ssd = Pvariable(config[CONF_ID], App.make_i2c_ssd1306())
    add(ssd.set_model(ssd1306_spi.MODELS[config[CONF_MODEL]]))

    if CONF_RESET_PIN in config:
        for reset in gpio_output_pin_expression(config[CONF_RESET_PIN]):
            yield
        add(ssd.set_reset_pin(reset))
    if CONF_EXTERNAL_VCC in config:
        add(ssd.set_external_vcc(config[CONF_EXTERNAL_VCC]))
    if CONF_ADDRESS in config:
        add(ssd.set_address(config[CONF_ADDRESS]))
    if CONF_LAMBDA in config:
        for lambda_ in process_lambda(config[CONF_LAMBDA],
                                      [(display.DisplayBufferRef, 'it')], return_type=void):
            yield
        add(ssd.set_writer(lambda_))

    display.setup_display(ssd, config)
    setup_component(ssd, config)
Example #17
0
def homeassistant_service_to_code(config, action_id, arg_type, template_arg):
    for var in get_variable(config[CONF_ID]):
        yield None
    rhs = var.make_home_assistant_service_call_action(template_arg)
    type = HomeAssistantServiceCallAction.template(arg_type)
    act = Pvariable(action_id, rhs, type=type)
    add(act.set_service(config[CONF_SERVICE]))
    if CONF_DATA in config:
        datas = [KeyValuePair(k, v) for k, v in config[CONF_DATA].items()]
        add(act.set_data(ArrayInitializer(*datas)))
    if CONF_DATA_TEMPLATE in config:
        datas = [KeyValuePair(k, v) for k, v in config[CONF_DATA_TEMPLATE].items()]
        add(act.set_data_template(ArrayInitializer(*datas)))
    if CONF_VARIABLES in config:
        datas = []
        for key, value in config[CONF_VARIABLES].items():
            for value_ in process_lambda(value, []):
                yield None
            datas.append(TemplatableKeyValuePair(key, value_))
        add(act.set_variables(ArrayInitializer(*datas)))
    yield act
Example #18
0
def to_code(config):
    for spi_ in get_variable(config[CONF_SPI_ID]):
        yield
    for cs in gpio_output_pin_expression(config[CONF_CS_PIN]):
        yield
    rhs = App.make_max7219(spi_, cs)
    max7219 = Pvariable(config[CONF_ID], rhs)

    if CONF_NUM_CHIPS in config:
        add(max7219.set_num_chips(config[CONF_NUM_CHIPS]))
    if CONF_INTENSITY in config:
        add(max7219.set_intensity(config[CONF_INTENSITY]))

    if CONF_LAMBDA in config:
        for lambda_ in process_lambda(config[CONF_LAMBDA],
                                      [(MAX7219ComponentRef, 'it')]):
            yield
        add(max7219.set_writer(lambda_))

    display.setup_display(max7219, config)
    setup_component(max7219, config)
Example #19
0
def setup_filter(config):
    if CONF_OFFSET in config:
        yield OffsetFilter.new(config[CONF_OFFSET])
    elif CONF_MULTIPLY in config:
        yield MultiplyFilter.new(config[CONF_MULTIPLY])
    elif CONF_FILTER_OUT in config:
        yield FilterOutValueFilter.new(config[CONF_FILTER_OUT])
    elif CONF_FILTER_NAN in config:
        yield FilterOutNANFilter.new()
    elif CONF_SLIDING_WINDOW_MOVING_AVERAGE in config:
        conf = config[CONF_SLIDING_WINDOW_MOVING_AVERAGE]
        yield SlidingWindowMovingAverageFilter.new(
            conf[CONF_WINDOW_SIZE], conf[CONF_SEND_EVERY],
            conf.get(CONF_SEND_FIRST_AT))
    elif CONF_EXPONENTIAL_MOVING_AVERAGE in config:
        conf = config[CONF_EXPONENTIAL_MOVING_AVERAGE]
        yield ExponentialMovingAverageFilter.new(conf[CONF_ALPHA],
                                                 conf[CONF_SEND_EVERY])
    elif CONF_LAMBDA in config:
        lambda_ = None
        for lambda_ in process_lambda(config[CONF_LAMBDA], [(float_, 'x')],
                                      return_type=optional.template(float_)):
            yield None
        yield LambdaFilter.new(lambda_)
    elif CONF_THROTTLE in config:
        yield ThrottleFilter.new(config[CONF_THROTTLE])
    elif CONF_DELTA in config:
        yield DeltaFilter.new(config[CONF_DELTA])
    elif CONF_OR in config:
        filters = None
        for filters in setup_filters(config[CONF_OR]):
            yield None
        yield OrFilter.new(filters)
    elif CONF_HEARTBEAT in config:
        yield App.register_component(
            HeartbeatFilter.new(config[CONF_HEARTBEAT]))
    elif CONF_DEBOUNCE in config:
        yield App.register_component(DebounceFilter.new(config[CONF_DEBOUNCE]))
    elif CONF_UNIQUE in config:
        yield UniqueFilter.new()
Example #20
0
def to_code(config):
    rhs = App.make_template_cover(config[CONF_NAME])
    make = variable(config[CONF_MAKE_ID], rhs)

    cover.setup_cover(make.Ptemplate_, make.Pmqtt, config)
    setup_component(make.Ptemplate_, config)

    if CONF_LAMBDA in config:
        for template_ in process_lambda(config[CONF_LAMBDA], [],
                                        return_type=optional.template(cover.CoverState)):
            yield
        add(make.Ptemplate_.set_state_lambda(template_))
    if CONF_OPEN_ACTION in config:
        automation.build_automation(make.Ptemplate_.get_open_trigger(), NoArg,
                                    config[CONF_OPEN_ACTION])
    if CONF_CLOSE_ACTION in config:
        automation.build_automation(make.Ptemplate_.get_close_trigger(), NoArg,
                                    config[CONF_CLOSE_ACTION])
    if CONF_STOP_ACTION in config:
        automation.build_automation(make.Ptemplate_.get_stop_trigger(), NoArg,
                                    config[CONF_STOP_ACTION])
    if CONF_OPTIMISTIC in config:
        add(make.Ptemplate_.set_optimistic(config[CONF_OPTIMISTIC]))
Example #21
0
def to_code(config):
    for spi_ in get_variable(config[CONF_SPI_ID]):
        yield
    for cs in gpio_output_pin_expression(config[CONF_CS_PIN]):
        yield
    for dc in gpio_output_pin_expression(config[CONF_DC_PIN]):
        yield

    model_type, model = MODELS[config[CONF_MODEL]]
    if model_type == 'a':
        rhs = App.make_waveshare_epaper_type_a(spi_, cs, dc, model)
        epaper = Pvariable(config[CONF_ID], rhs, type=WaveshareEPaperTypeA)
    elif model_type == 'b':
        rhs = App.make_waveshare_epaper_type_b(spi_, cs, dc, model)
        epaper = Pvariable(config[CONF_ID], rhs, type=WaveshareEPaper)
    else:
        raise NotImplementedError()

    if CONF_LAMBDA in config:
        for lambda_ in process_lambda(config[CONF_LAMBDA],
                                      [(display.DisplayBufferRef, 'it')],
                                      return_type=void):
            yield
        add(epaper.set_writer(lambda_))
    if CONF_RESET_PIN in config:
        for reset in gpio_output_pin_expression(config[CONF_RESET_PIN]):
            yield
        add(epaper.set_reset_pin(reset))
    if CONF_BUSY_PIN in config:
        for reset in gpio_input_pin_expression(config[CONF_BUSY_PIN]):
            yield
        add(epaper.set_busy_pin(reset))
    if CONF_FULL_UPDATE_EVERY in config:
        add(epaper.set_full_update_every(config[CONF_FULL_UPDATE_EVERY]))

    display.setup_display(epaper, config)
    setup_component(epaper, config)
Example #22
0
def lambda_action_to_code(config, action_id, arg_type, template_arg):
    for lambda_ in process_lambda(config, [(arg_type, 'x')], return_type=void):
        yield None
    rhs = LambdaAction.new(template_arg, lambda_)
    type = LambdaAction.template(template_arg)
    yield Pvariable(action_id, rhs, type=type)
Example #23
0
def build_effect(full_config):
    key, config = next(iter(full_config.items()))
    if key == CONF_LAMBDA:
        lambda_ = None
        for lambda_ in process_lambda(config[CONF_LAMBDA], []):
            yield None
        yield LambdaLightEffect.new(config[CONF_NAME], lambda_, config[CONF_UPDATE_INTERVAL])
    elif key == CONF_RANDOM:
        rhs = RandomLightEffect.new(config[CONF_NAME])
        effect = Pvariable(config[CONF_EFFECT_ID], rhs)
        if CONF_TRANSITION_LENGTH in config:
            add(effect.set_transition_length(config[CONF_TRANSITION_LENGTH]))
        if CONF_UPDATE_INTERVAL in config:
            add(effect.set_update_interval(config[CONF_UPDATE_INTERVAL]))
        yield effect
    elif key == CONF_STROBE:
        rhs = StrobeLightEffect.new(config[CONF_NAME])
        effect = Pvariable(config[CONF_EFFECT_ID], rhs)
        colors = []
        for color in config.get(CONF_COLORS, []):
            colors.append(StructInitializer(
                StrobeLightEffectColor,
                ('color', LightColorValues(color[CONF_STATE], color[CONF_BRIGHTNESS],
                                           color[CONF_RED], color[CONF_GREEN], color[CONF_BLUE],
                                           color[CONF_WHITE])),
                ('duration', color[CONF_DURATION]),
            ))
        if colors:
            add(effect.set_colors(ArrayInitializer(*colors)))
        yield effect
    elif key == CONF_FLICKER:
        rhs = FlickerLightEffect.new(config[CONF_NAME])
        effect = Pvariable(config[CONF_EFFECT_ID], rhs)
        if CONF_ALPHA in config:
            add(effect.set_alpha(config[CONF_ALPHA]))
        if CONF_INTENSITY in config:
            add(effect.set_intensity(config[CONF_INTENSITY]))
        yield effect
    elif key == CONF_ADDRESSABLE_LAMBDA:
        args = [(AddressableLightRef, 'it')]
        for lambda_ in process_lambda(config[CONF_LAMBDA], args):
            yield None
        yield AddressableLambdaLightEffect.new(config[CONF_NAME], lambda_,
                                               config[CONF_UPDATE_INTERVAL])
    elif key == CONF_ADDRESSABLE_RAINBOW:
        rhs = AddressableRainbowLightEffect.new(config[CONF_NAME])
        effect = Pvariable(config[CONF_EFFECT_ID], rhs)
        if CONF_SPEED in config:
            add(effect.set_speed(config[CONF_SPEED]))
        if CONF_WIDTH in config:
            add(effect.set_width(config[CONF_WIDTH]))
        yield effect
    elif key == CONF_ADDRESSABLE_COLOR_WIPE:
        rhs = AddressableColorWipeEffect.new(config[CONF_NAME])
        effect = Pvariable(config[CONF_EFFECT_ID], rhs)
        if CONF_ADD_LED_INTERVAL in config:
            add(effect.set_add_led_interval(config[CONF_ADD_LED_INTERVAL]))
        if CONF_REVERSE in config:
            add(effect.set_reverse(config[CONF_REVERSE]))
        colors = []
        for color in config.get(CONF_COLORS, []):
            colors.append(StructInitializer(
                AddressableColorWipeEffectColor,
                ('r', int(round(color[CONF_RED] * 255))),
                ('g', int(round(color[CONF_GREEN] * 255))),
                ('b', int(round(color[CONF_BLUE] * 255))),
                ('w', int(round(color[CONF_WHITE] * 255))),
                ('random', color[CONF_RANDOM]),
                ('num_leds', color[CONF_NUM_LEDS]),
            ))
        if colors:
            add(effect.set_colors(ArrayInitializer(*colors)))
        yield effect
    elif key == CONF_ADDRESSABLE_SCAN:
        rhs = AddressableScanEffect.new(config[CONF_NAME])
        effect = Pvariable(config[CONF_EFFECT_ID], rhs)
        if CONF_MOVE_INTERVAL in config:
            add(effect.set_move_interval(config[CONF_MOVE_INTERVAL]))
        yield effect
    elif key == CONF_ADDRESSABLE_TWINKLE:
        rhs = AddressableTwinkleEffect.new(config[CONF_NAME])
        effect = Pvariable(config[CONF_EFFECT_ID], rhs)
        if CONF_TWINKLE_PROBABILITY in config:
            add(effect.set_twinkle_probability(config[CONF_TWINKLE_PROBABILITY]))
        if CONF_PROGRESS_INTERVAL in config:
            add(effect.set_progress_interval(config[CONF_PROGRESS_INTERVAL]))
        yield effect
    elif key == CONF_ADDRESSABLE_RANDOM_TWINKLE:
        rhs = AddressableRandomTwinkleEffect.new(config[CONF_NAME])
        effect = Pvariable(config[CONF_EFFECT_ID], rhs)
        if CONF_TWINKLE_PROBABILITY in config:
            add(effect.set_twinkle_probability(config[CONF_TWINKLE_PROBABILITY]))
        if CONF_PROGRESS_INTERVAL in config:
            add(effect.set_progress_interval(config[CONF_PROGRESS_INTERVAL]))
        yield effect
    elif key == CONF_ADDRESSABLE_FIREWORKS:
        rhs = AddressableFireworksEffect.new(config[CONF_NAME])
        effect = Pvariable(config[CONF_EFFECT_ID], rhs)
        if CONF_UPDATE_INTERVAL in config:
            add(effect.set_update_interval(config[CONF_UPDATE_INTERVAL]))
        if CONF_SPARK_PROBABILITY in config:
            add(effect.set_spark_probability(config[CONF_SPARK_PROBABILITY]))
        if CONF_USE_RANDOM_COLOR in config:
            add(effect.set_spark_probability(config[CONF_USE_RANDOM_COLOR]))
        if CONF_FADE_OUT_RATE in config:
            add(effect.set_spark_probability(config[CONF_FADE_OUT_RATE]))
        yield effect
    elif key == CONF_ADDRESSABLE_FLICKER:
        rhs = AddressableFlickerEffect.new(config[CONF_NAME])
        effect = Pvariable(config[CONF_EFFECT_ID], rhs)
        if CONF_UPDATE_INTERVAL in config:
            add(effect.set_update_interval(config[CONF_UPDATE_INTERVAL]))
        if CONF_INTENSITY in config:
            add(effect.set_intensity(config[CONF_INTENSITY]))
        yield effect
    else:
        raise NotImplementedError("Effect {} not implemented".format(next(config.keys())))