How to use the esphome.cpp_generator.add function in esphome

To help you get started, we’ve selected a few esphome examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github esphome / esphome / esphome / components / remote_transmitter.py View on Github external
def to_code(config):
    pin = yield gpio_output_pin_expression(config[CONF_PIN])
    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)
github esphome / esphome / esphome / components / display / ssd1306_spi.py View on Github external
def to_code(config):
    spi_ = yield get_variable(config[CONF_SPI_ID])
    cs = yield gpio_output_pin_expression(config[CONF_CS_PIN])
    dc = yield gpio_output_pin_expression(config[CONF_DC_PIN])

    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:
        reset = yield gpio_output_pin_expression(config[CONF_RESET_PIN])
        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:
        lambda_ = yield process_lambda(config[CONF_LAMBDA],
                                       [(display.DisplayBufferRef, 'it')], return_type=void)
        add(ssd.set_writer(lambda_))

    display.setup_display(ssd, config)
    setup_component(ssd, config)
github esphome / esphome / esphome / components / cover / endstop.py View on Github external
def to_code(config):
    rhs = App.register_component(EndstopCover.new(config[CONF_NAME]))
    var = Pvariable(config[CONF_ID], rhs)
    cover.register_cover(var, config)
    setup_component(var, config)

    automation.build_automations(var.get_stop_trigger(), [],
                                 config[CONF_STOP_ACTION])

    bin = yield get_variable(config[CONF_OPEN_ENDSTOP])
    add(var.set_open_endstop(bin))
    add(var.set_open_duration(config[CONF_OPEN_DURATION]))
    automation.build_automations(var.get_open_trigger(), [],
                                 config[CONF_OPEN_ACTION])

    bin = yield get_variable(config[CONF_CLOSE_ENDSTOP])
    add(var.set_close_endstop(bin))
    add(var.set_close_duration(config[CONF_CLOSE_DURATION]))
    automation.build_automations(var.get_close_trigger(), [],
                                 config[CONF_CLOSE_ACTION])

    if CONF_MAX_DURATION in config:
        add(var.set_max_duration(config[CONF_MAX_DURATION]))
github esphome / esphome / esphome / components / spi.py View on Github external
def to_code(config):
    clk = yield gpio_output_pin_expression(config[CONF_CLK_PIN])
    rhs = App.init_spi(clk)
    spi = Pvariable(config[CONF_ID], rhs)
    if CONF_MISO_PIN in config:
        miso = yield gpio_input_pin_expression(config[CONF_MISO_PIN])
        add(spi.set_miso(miso))
    if CONF_MOSI_PIN in config:
        mosi = yield gpio_input_pin_expression(config[CONF_MOSI_PIN])
        add(spi.set_mosi(mosi))

    setup_component(spi, config)
github esphome / esphome / esphome / components / mqtt.py View on Github external
add(mqtt.set_client_id(config[CONF_CLIENT_ID]))

    if CONF_LOG_TOPIC in config:
        log_topic = config[CONF_LOG_TOPIC]
        if not log_topic:
            add(mqtt.disable_log_message())
        else:
            add(mqtt.set_log_message_template(exp_mqtt_message(log_topic)))

            if CONF_LEVEL in log_topic:
                add(mqtt.set_log_level(logger.LOG_LEVELS[log_topic[CONF_LEVEL]]))

    if CONF_SSL_FINGERPRINTS in config:
        for fingerprint in config[CONF_SSL_FINGERPRINTS]:
            arr = [RawExpression("0x{}".format(fingerprint[i:i + 2])) for i in range(0, 40, 2)]
            add(mqtt.add_ssl_fingerprint(arr))

    if CONF_KEEPALIVE in config:
        add(mqtt.set_keep_alive(config[CONF_KEEPALIVE]))

    if CONF_REBOOT_TIMEOUT in config:
        add(mqtt.set_reboot_timeout(config[CONF_REBOOT_TIMEOUT]))

    for conf in config.get(CONF_ON_MESSAGE, []):
        rhs = App.register_component(mqtt.make_message_trigger(conf[CONF_TOPIC]))
        trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs)
        if CONF_QOS in conf:
            add(trigger.set_qos(conf[CONF_QOS]))
        if CONF_PAYLOAD in conf:
            add(trigger.set_payload(conf[CONF_PAYLOAD]))
        automation.build_automations(trigger, [(std_string, 'x')], conf)
github esphome / esphome / esphome / components / mqtt.py View on Github external
if CONF_SSL_FINGERPRINTS in config:
        for fingerprint in config[CONF_SSL_FINGERPRINTS]:
            arr = [RawExpression("0x{}".format(fingerprint[i:i + 2])) for i in range(0, 40, 2)]
            add(mqtt.add_ssl_fingerprint(arr))

    if CONF_KEEPALIVE in config:
        add(mqtt.set_keep_alive(config[CONF_KEEPALIVE]))

    if CONF_REBOOT_TIMEOUT in config:
        add(mqtt.set_reboot_timeout(config[CONF_REBOOT_TIMEOUT]))

    for conf in config.get(CONF_ON_MESSAGE, []):
        rhs = App.register_component(mqtt.make_message_trigger(conf[CONF_TOPIC]))
        trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs)
        if CONF_QOS in conf:
            add(trigger.set_qos(conf[CONF_QOS]))
        if CONF_PAYLOAD in conf:
            add(trigger.set_payload(conf[CONF_PAYLOAD]))
        automation.build_automations(trigger, [(std_string, 'x')], conf)

    for conf in config.get(CONF_ON_JSON_MESSAGE, []):
        rhs = mqtt.make_json_message_trigger(conf[CONF_TOPIC], conf[CONF_QOS])
        trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs)
        automation.build_automations(trigger, [(JsonObjectConstRef, 'x')], conf)
github esphome / esphome / esphome / components / sensor / tsl2561.py View on Github external
def to_code(config):
    rhs = App.make_tsl2561_sensor(config[CONF_NAME], config[CONF_ADDRESS],
                                  config.get(CONF_UPDATE_INTERVAL))
    tsl2561 = Pvariable(config[CONF_ID], rhs)

    if CONF_INTEGRATION_TIME in config:
        add(tsl2561.set_integration_time(INTEGRATION_TIMES[config[CONF_INTEGRATION_TIME]]))
    if CONF_GAIN in config:
        add(tsl2561.set_gain(GAINS[config[CONF_GAIN]]))
    if CONF_IS_CS_PACKAGE in config:
        add(tsl2561.set_is_cs_package(config[CONF_IS_CS_PACKAGE]))

    sensor.setup_sensor(tsl2561, config)
    setup_component(tsl2561, config)
github esphome / esphome / esphome / components / sensor / ina3221.py View on Github external
def to_code(config):
    rhs = App.make_ina3221(config[CONF_ADDRESS], config.get(CONF_UPDATE_INTERVAL))
    ina = Pvariable(config[CONF_ID], rhs)
    for i, channel in enumerate([CONF_CHANNEL_1, CONF_CHANNEL_2, CONF_CHANNEL_3]):
        if channel not in config:
            continue
        conf = config[channel]
        if CONF_SHUNT_RESISTANCE in conf:
            add(ina.set_shunt_resistance(i, conf[CONF_SHUNT_RESISTANCE]))
        if CONF_BUS_VOLTAGE in conf:
            c = conf[CONF_BUS_VOLTAGE]
            sensor.register_sensor(ina.Pmake_bus_voltage_sensor(i, c[CONF_NAME]), c)
        if CONF_SHUNT_VOLTAGE in conf:
            c = conf[CONF_SHUNT_VOLTAGE]
            sensor.register_sensor(ina.Pmake_shunt_voltage_sensor(i, c[CONF_NAME]), c)
        if CONF_CURRENT in conf:
            c = conf[CONF_CURRENT]
            sensor.register_sensor(ina.Pmake_current_sensor(i, c[CONF_NAME]), c)
        if CONF_POWER in conf:
            c = conf[CONF_POWER]
            sensor.register_sensor(ina.Pmake_power_sensor(i, c[CONF_NAME]), c)

    setup_component(ina, config)
github esphome / esphome / esphome / components / sensor / hlw8012.py View on Github external
config.get(CONF_UPDATE_INTERVAL))
    hlw = Pvariable(config[CONF_ID], rhs)

    if CONF_VOLTAGE in config:
        conf = config[CONF_VOLTAGE]
        sensor.register_sensor(hlw.make_voltage_sensor(conf[CONF_NAME]), conf)
    if CONF_CURRENT in config:
        conf = config[CONF_CURRENT]
        sensor.register_sensor(hlw.make_current_sensor(conf[CONF_NAME]), conf)
    if CONF_POWER in config:
        conf = config[CONF_POWER]
        sensor.register_sensor(hlw.make_power_sensor(conf[CONF_NAME]), conf)
    if CONF_CURRENT_RESISTOR in config:
        add(hlw.set_current_resistor(config[CONF_CURRENT_RESISTOR]))
    if CONF_VOLTAGE_DIVIDER in config:
        add(hlw.set_voltage_divider(config[CONF_VOLTAGE_DIVIDER]))
    if CONF_CHANGE_MODE_EVERY in config:
        add(hlw.set_change_mode_every(config[CONF_CHANGE_MODE_EVERY]))
    setup_component(hlw, config)
github esphome / esphome / esphome / components / sensor / bme280.py View on Github external
def to_code(config):
    rhs = App.make_bme280_sensor(config[CONF_TEMPERATURE][CONF_NAME],
                                 config[CONF_PRESSURE][CONF_NAME],
                                 config[CONF_HUMIDITY][CONF_NAME],
                                 config[CONF_ADDRESS],
                                 config.get(CONF_UPDATE_INTERVAL))
    bme280 = Pvariable(config[CONF_ID], rhs)
    if CONF_OVERSAMPLING in config[CONF_TEMPERATURE]:
        constant = OVERSAMPLING_OPTIONS[config[CONF_TEMPERATURE][CONF_OVERSAMPLING]]
        add(bme280.set_temperature_oversampling(constant))
    if CONF_OVERSAMPLING in config[CONF_PRESSURE]:
        constant = OVERSAMPLING_OPTIONS[config[CONF_PRESSURE][CONF_OVERSAMPLING]]
        add(bme280.set_pressure_oversampling(constant))
    if CONF_OVERSAMPLING in config[CONF_HUMIDITY]:
        constant = OVERSAMPLING_OPTIONS[config[CONF_HUMIDITY][CONF_OVERSAMPLING]]
        add(bme280.set_humidity_oversampling(constant))
    if CONF_IIR_FILTER in config:
        constant = IIR_FILTER_OPTIONS[config[CONF_IIR_FILTER]]
        add(bme280.set_iir_filter(constant))

    sensor.setup_sensor(bme280.Pget_temperature_sensor(), config[CONF_TEMPERATURE])
    sensor.setup_sensor(bme280.Pget_pressure_sensor(), config[CONF_PRESSURE])
    sensor.setup_sensor(bme280.Pget_humidity_sensor(), config[CONF_HUMIDITY])
    setup_component(bme280, config)