def to_code(config):
    rhs = App.make_esp32_ble_tracker()
    ble = Pvariable(config[CONF_ID], rhs)
    if CONF_SCAN_INTERVAL in config:
        add(ble.set_scan_interval(config[CONF_SCAN_INTERVAL]))

    setup_component(ble, config)
Example #2
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 #3
0
def write_cpp(config):
    _LOGGER.info("Generating C++ source...")

    CORE.add_job(core_config.to_code,
                 config[CONF_ESPHOMEYAML],
                 domain='esphomeyaml')
    for domain in PRE_INITIALIZE:
        if domain == CONF_ESPHOMEYAML or domain not in config:
            continue
        CORE.add_job(get_component(domain).to_code,
                     config[domain],
                     domain=domain)

    for domain, component, conf in iter_components(config):
        if domain in PRE_INITIALIZE or not hasattr(component, 'to_code'):
            continue
        CORE.add_job(component.to_code, conf, domain=domain)

    CORE.flush_tasks()
    add(RawStatement(''))
    add(RawStatement(''))
    all_code = []
    for exp in CORE.expressions:
        if not config[CONF_ESPHOMEYAML][CONF_USE_CUSTOM_CODE]:
            if isinstance(exp, Expression) and not exp.required:
                continue
        all_code.append(text_type(statement(exp)))

    writer.write_platformio_project()

    code_s = indent('\n'.join(line.rstrip() for line in all_code))
    writer.write_cpp(code_s)
    return 0
Example #4
0
def setup_binary_sensor_core_(binary_sensor_var, mqtt_var, config):
    if CONF_INTERNAL in config:
        add(binary_sensor_var.set_internal(CONF_INTERNAL))
    if CONF_DEVICE_CLASS in config:
        add(binary_sensor_var.set_device_class(config[CONF_DEVICE_CLASS]))
    if CONF_INVERTED in config:
        add(binary_sensor_var.set_inverted(config[CONF_INVERTED]))
    if CONF_FILTERS in config:
        filters = None
        for filters in setup_filters(config[CONF_FILTERS]):
            yield
        add(binary_sensor_var.add_filters(filters))

    for conf in config.get(CONF_ON_PRESS, []):
        rhs = binary_sensor_var.make_press_trigger()
        trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs)
        automation.build_automation(trigger, NoArg, conf)

    for conf in config.get(CONF_ON_RELEASE, []):
        rhs = binary_sensor_var.make_release_trigger()
        trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs)
        automation.build_automation(trigger, NoArg, conf)

    for conf in config.get(CONF_ON_CLICK, []):
        rhs = binary_sensor_var.make_click_trigger(conf[CONF_MIN_LENGTH],
                                                   conf[CONF_MAX_LENGTH])
        trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs)
        automation.build_automation(trigger, NoArg, conf)

    for conf in config.get(CONF_ON_DOUBLE_CLICK, []):
        rhs = binary_sensor_var.make_double_click_trigger(
            conf[CONF_MIN_LENGTH], conf[CONF_MAX_LENGTH])
        trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs)
        automation.build_automation(trigger, NoArg, conf)

    for conf in config.get(CONF_ON_MULTI_CLICK, []):
        timings = []
        for tim in conf[CONF_TIMING]:
            timings.append(
                StructInitializer(
                    MultiClickTriggerEvent,
                    ('state', tim[CONF_STATE]),
                    ('min_length', tim[CONF_MIN_LENGTH]),
                    ('max_length', tim.get(CONF_MAX_LENGTH, 4294967294)),
                ))
        timings = ArrayInitializer(*timings, multiline=False)
        rhs = App.register_component(
            binary_sensor_var.make_multi_click_trigger(timings))
        trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs)
        if CONF_INVALID_COOLDOWN in conf:
            add(trigger.set_invalid_cooldown(conf[CONF_INVALID_COOLDOWN]))
        automation.build_automation(trigger, NoArg, conf)

    for conf in config.get(CONF_ON_STATE, []):
        rhs = binary_sensor_var.make_state_trigger()
        trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs)
        automation.build_automation(trigger, bool_, conf)

    setup_mqtt_component(mqtt_var, config)
Example #5
0
def delay_action_to_code(config, action_id, arg_type, template_arg):
    rhs = App.register_component(DelayAction.new(template_arg))
    type = DelayAction.template(template_arg)
    action = Pvariable(action_id, rhs, type=type)
    for template_ in templatable(config, arg_type, uint32):
        yield
    add(action.set_delay(template_))
    yield action
Example #6
0
def to_code(config):
    rhs = App.make_apds9960(config.get(CONF_UPDATE_INTERVAL))
    var = Pvariable(config[CONF_ID], rhs)

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

    setup_component(var, config)
Example #7
0
def to_code(config):
    rhs = App.init_i2c(config[CONF_SDA], config[CONF_SCL],
                       config.get(CONF_SCAN))
    i2c = Pvariable(config[CONF_ID], rhs)
    if CONF_FREQUENCY in config:
        add(i2c.set_frequency(config[CONF_FREQUENCY]))

    setup_component(i2c, config)
Example #8
0
def to_code(config):
    rhs = App.make_bh1750_sensor(config[CONF_NAME], config[CONF_ADDRESS],
                                 config.get(CONF_UPDATE_INTERVAL))
    make_bh1750 = variable(config[CONF_MAKE_ID], rhs)
    bh1750 = make_bh1750.Pbh1750
    if CONF_RESOLUTION in config:
        add(bh1750.set_resolution(BH1750_RESOLUTIONS[config[CONF_RESOLUTION]]))
    sensor.setup_sensor(bh1750, make_bh1750.Pmqtt, config)
    setup_component(bh1750, config)
Example #9
0
def to_code(config):
    rhs = App.init_web_server(config.get(CONF_PORT))
    web_server = Pvariable(config[CONF_ID], rhs)
    if CONF_CSS_URL in config:
        add(web_server.set_css_url(config[CONF_CSS_URL]))
    if CONF_JS_URL in config:
        add(web_server.set_js_url(config[CONF_JS_URL]))

    setup_component(web_server, config)
Example #10
0
def stepper_set_target_to_code(config, action_id, arg_type, template_arg):
    for var in get_variable(config[CONF_ID]):
        yield None
    rhs = var.make_set_target_action(template_arg)
    type = SetTargetAction.template(arg_type)
    action = Pvariable(action_id, rhs, type=type)
    for template_ in templatable(config[CONF_TARGET], arg_type, int32):
        yield None
    add(action.set_target(template_))
    yield action
Example #11
0
def stepper_report_position_to_code(config, action_id, arg_type, template_arg):
    for var in get_variable(config[CONF_ID]):
        yield None
    rhs = var.make_report_position_action(template_arg)
    type = ReportPositionAction.template(arg_type)
    action = Pvariable(action_id, rhs, type=type)
    for template_ in templatable(config[CONF_POSITION], arg_type, int32):
        yield None
    add(action.set_target(template_))
    yield action
Example #12
0
def to_code(config):
    for pin in gpio_output_pin_expression(config[CONF_PIN]):
        yield
    rhs = App.make_remote_transmitter_component(pin)
    transmitter = Pvariable(config[CONF_ID], rhs)

    if CONF_CARRIER_DUTY_PERCENT in config:
        add(transmitter.set_carrier_duty_percent(config[CONF_CARRIER_DUTY_PERCENT]))

    setup_component(transmitter, config)
Example #13
0
def to_code(config):
    rhs = App.make_mqtt_subscribe_sensor(config[CONF_NAME], config[CONF_TOPIC])
    make = variable(config[CONF_MAKE_ID], rhs)
    subs = make.Psensor

    if CONF_QOS in config:
        add(subs.set_qos(config[CONF_QOS]))

    sensor.setup_sensor(subs, make.Pmqtt, config)
    setup_component(subs, config)
Example #14
0
def while_action_to_code(config, action_id, arg_type, template_arg):
    for conditions in build_conditions(config[CONF_CONDITION], arg_type):
        yield None
    rhs = WhileAction.new(template_arg, conditions)
    type = WhileAction.template(template_arg)
    action = Pvariable(action_id, rhs, type=type)
    for actions in build_actions(config[CONF_THEN], arg_type):
        yield None
    add(action.add_then(actions))
    yield action
Example #15
0
def to_code(config):
    rhs = App.make_sntp_component()
    sntp = Pvariable(config[CONF_ID], rhs)
    if CONF_SERVERS in config:
        servers = config[CONF_SERVERS]
        servers += [''] * (3 - len(servers))
        add(sntp.set_servers(*servers))

    time_.setup_time(sntp, config)
    setup_component(sntp, config)
Example #16
0
def light_turn_off_to_code(config, action_id, arg_type, template_arg):
    for var in get_variable(config[CONF_ID]):
        yield None
    rhs = var.make_turn_off_action(template_arg)
    type = TurnOffAction.template(template_arg)
    action = Pvariable(action_id, rhs, type=type)
    if CONF_TRANSITION_LENGTH in config:
        for template_ in templatable(config[CONF_TRANSITION_LENGTH], arg_type, uint32):
            yield None
        add(action.set_transition_length(template_))
    yield action
Example #17
0
def to_code(config):
    for pin in gpio_output_pin_expression(config[CONF_PIN]):
        yield
    rhs = App.make_esp8266_pwm_output(pin)
    gpio = Pvariable(config[CONF_ID], rhs)

    if CONF_FREQUENCY in config:
        add(gpio.set_frequency(config[CONF_FREQUENCY]))

    output.setup_output_platform(gpio, config)
    setup_component(gpio, config)
Example #18
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 #19
0
def to_code(config):
    frequency = config.get(CONF_FREQUENCY)
    if frequency is None and CONF_BIT_DEPTH in config:
        frequency = 1000
    rhs = App.make_ledc_output(config[CONF_PIN], frequency,
                               config.get(CONF_BIT_DEPTH))
    ledc = Pvariable(config[CONF_ID], rhs)
    if CONF_CHANNEL in config:
        add(ledc.set_channel(config[CONF_CHANNEL]))
    output.setup_output_platform(ledc, config)
    setup_component(ledc, config)
Example #20
0
def to_code(config):
    uuid = config[CONF_UUID].hex
    uuid_arr = [RawExpression('0x{}'.format(uuid[i:i + 2])) for i in range(0, len(uuid), 2)]
    rhs = App.make_esp32_ble_beacon(ArrayInitializer(*uuid_arr, multiline=False))
    ble = Pvariable(config[CONF_ID], rhs)
    if CONF_MAJOR in config:
        add(ble.set_major(config[CONF_MAJOR]))
    if CONF_MINOR in config:
        add(ble.set_minor(config[CONF_MINOR]))

    setup_component(ble, config)
Example #21
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 #22
0
def to_code(config):
    for pin in gpio_output_pin_expression(config[CONF_PIN]):
        yield
    rhs = App.make_gpio_switch(config[CONF_NAME], pin)
    make = variable(config[CONF_MAKE_ID], rhs)
    gpio = make.Pswitch_

    if CONF_RESTORE_MODE in config:
        add(gpio.set_restore_mode(RESTORE_MODES[config[CONF_RESTORE_MODE]]))

    switch.setup_switch(gpio, make.Pmqtt, config)
    setup_component(gpio, config)
Example #23
0
def to_code(config):
    for pin in gpio_output_pin_expression(config[CONF_PIN]):
        yield

    rhs = App.make_power_supply(pin)
    psu = Pvariable(config[CONF_ID], rhs)
    if CONF_ENABLE_TIME in config:
        add(psu.set_enable_time(config[CONF_ENABLE_TIME]))
    if CONF_KEEP_ON_TIME in config:
        add(psu.set_keep_on_time(config[CONF_KEEP_ON_TIME]))

    setup_component(psu, config)
Example #24
0
def to_code(config):
    pin = config[CONF_PIN]
    if pin == 'VCC':
        pin = 0
    rhs = App.make_adc_sensor(config[CONF_NAME], pin,
                              config.get(CONF_UPDATE_INTERVAL))
    make = variable(config[CONF_MAKE_ID], rhs)
    adc = make.Padc
    if CONF_ATTENUATION in config:
        add(adc.set_attenuation(ATTENUATION_MODES[config[CONF_ATTENUATION]]))
    sensor.setup_sensor(adc, make.Pmqtt, config)
    setup_component(adc, config)
Example #25
0
def setup_text_sensor_core_(text_sensor_var, mqtt_var, config):
    if CONF_INTERNAL in config:
        add(text_sensor_var.set_internal(config[CONF_INTERNAL]))
    if CONF_ICON in config:
        add(text_sensor_var.set_icon(config[CONF_ICON]))

    for conf in config.get(CONF_ON_VALUE, []):
        rhs = text_sensor_var.make_state_trigger()
        trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs)
        automation.build_automation(trigger, std_string, conf)

    setup_mqtt_component(mqtt_var, config)
Example #26
0
def setup_fan_core_(fan_var, mqtt_var, config):
    if CONF_INTERNAL in config:
        add(fan_var.set_internal(config[CONF_INTERNAL]))

    if CONF_OSCILLATION_STATE_TOPIC in config:
        add(mqtt_var.set_custom_oscillation_state_topic(config[CONF_OSCILLATION_STATE_TOPIC]))
    if CONF_OSCILLATION_COMMAND_TOPIC in config:
        add(mqtt_var.set_custom_oscillation_command_topic(config[CONF_OSCILLATION_COMMAND_TOPIC]))
    if CONF_SPEED_STATE_TOPIC in config:
        add(mqtt_var.set_custom_speed_state_topic(config[CONF_SPEED_STATE_TOPIC]))
    if CONF_SPEED_COMMAND_TOPIC in config:
        add(mqtt_var.set_custom_speed_command_topic(config[CONF_SPEED_COMMAND_TOPIC]))
    setup_mqtt_component(mqtt_var, config)
Example #27
0
def to_code(config):
    rhs = App.make_bmp085_sensor(config[CONF_TEMPERATURE][CONF_NAME],
                                 config[CONF_PRESSURE][CONF_NAME],
                                 config.get(CONF_UPDATE_INTERVAL))
    bmp = variable(config[CONF_MAKE_ID], rhs)
    if CONF_ADDRESS in config:
        add(bmp.Pbmp.set_address(HexIntLiteral(config[CONF_ADDRESS])))

    sensor.setup_sensor(bmp.Pbmp.Pget_temperature_sensor(),
                        bmp.Pmqtt_temperature, config[CONF_TEMPERATURE])
    sensor.setup_sensor(bmp.Pbmp.Pget_pressure_sensor(), bmp.Pmqtt_pressure,
                        config[CONF_PRESSURE])
    setup_component(bmp.Pbmp, config)
Example #28
0
def sensor_in_range_to_code(config, condition_id, arg_type, template_arg):
    for var in get_variable(config[CONF_ID]):
        yield None
    rhs = var.make_sensor_in_range_condition(template_arg)
    type = SensorInRangeCondition.template(arg_type)
    cond = Pvariable(condition_id, rhs, type=type)

    if CONF_ABOVE in config:
        add(cond.set_min(config[CONF_ABOVE]))
    if CONF_BELOW in config:
        add(cond.set_max(config[CONF_BELOW]))

    yield cond
Example #29
0
def to_code(config):
    rhs = App.make_fast_led_light(config[CONF_NAME])
    make = variable(config[CONF_MAKE_ID], rhs)
    fast_led = make.Pfast_led

    rgb_order = None
    if CONF_RGB_ORDER in config:
        rgb_order = RawExpression(config[CONF_RGB_ORDER])
    template_args = TemplateArguments(RawExpression(config[CONF_CHIPSET]),
                                      config[CONF_DATA_PIN],
                                      config[CONF_CLOCK_PIN], rgb_order)
    add(fast_led.add_leds(template_args, config[CONF_NUM_LEDS]))

    if CONF_MAX_REFRESH_RATE in config:
        add(fast_led.set_max_refresh_rate(config[CONF_MAX_REFRESH_RATE]))

    if CONF_POWER_SUPPLY in config:
        for power_supply in get_variable(config[CONF_POWER_SUPPLY]):
            yield
        add(fast_led.set_power_supply(power_supply))

    if CONF_COLOR_CORRECT in config:
        r, g, b = config[CONF_COLOR_CORRECT]
        add(fast_led.set_correction(r, g, b))

    light.setup_light(make.Pstate, make.Pmqtt, config)
    setup_component(fast_led, config)
Example #30
0
def build_automation_(trigger, arg_type, config):
    rhs = App.make_automation(TemplateArguments(arg_type), trigger)
    type = Automation.template(arg_type)
    obj = Pvariable(config[CONF_AUTOMATION_ID], rhs, type=type)
    if CONF_IF in config:
        conditions = None
        for conditions in build_conditions(config[CONF_IF], arg_type):
            yield None
        add(obj.add_conditions(conditions))
    actions = None
    for actions in build_actions(config[CONF_THEN], arg_type):
        yield None
    add(obj.add_actions(actions))
    yield obj