Ejemplo n.º 1
0
def write_cpp(config):
    _LOGGER.info("Generating C++ source...")

    CORE.add_job(core_config.to_code, config[CONF_PI4HOME], domain='pi4home')
    for domain in PRE_INITIALIZE:
        if domain == CONF_PI4HOME 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_PI4HOME][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
Ejemplo n.º 2
0
def to_code(config):
    rhs = App.make_deep_sleep_component()
    deep_sleep = Pvariable(config[CONF_ID], rhs)
    if CONF_SLEEP_DURATION in config:
        add(deep_sleep.set_sleep_duration(config[CONF_SLEEP_DURATION]))
    if CONF_WAKEUP_PIN in config:
        for pin in gpio_input_pin_expression(config[CONF_WAKEUP_PIN]):
            yield
        add(deep_sleep.set_wakeup_pin(pin))
    if CONF_WAKEUP_PIN_MODE in config:
        add(
            deep_sleep.set_wakeup_pin_mode(
                WAKEUP_PIN_MODES[config[CONF_WAKEUP_PIN_MODE]]))
    if CONF_RUN_DURATION in config:
        add(deep_sleep.set_run_duration(config[CONF_RUN_DURATION]))

    if CONF_ESP32_EXT1_WAKEUP in config:
        conf = config[CONF_ESP32_EXT1_WAKEUP]
        mask = 0
        for pin in conf[CONF_PINS]:
            mask |= 1 << pin[CONF_NUMBER]
        struct = StructInitializer(
            Ext1Wakeup, ('mask', mask),
            ('wakeup_mode', EXT1_WAKEUP_MODES[conf[CONF_MODE]]))
        add(deep_sleep.set_ext1_wakeup(struct))

    setup_component(deep_sleep, config)
Ejemplo n.º 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)
Ejemplo n.º 4
0
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)
Ejemplo n.º 5
0
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)
Ejemplo n.º 6
0
def delay_action_to_code(config, action_id, template_arg, args):
    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, args, uint32):
        yield
    add(action.set_delay(template_))
    yield action
Ejemplo n.º 7
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)
Ejemplo n.º 8
0
def to_code(config):
    rhs = App.make_bh1750_sensor(config[CONF_NAME], config[CONF_ADDRESS],
                                 config.get(CONF_UPDATE_INTERVAL))
    bh1750 = Pvariable(config[CONF_ID], rhs)
    if CONF_RESOLUTION in config:
        add(bh1750.set_resolution(BH1750_RESOLUTIONS[config[CONF_RESOLUTION]]))
    sensor.setup_sensor(bh1750, config)
    setup_component(bh1750, config)
Ejemplo n.º 9
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)
Ejemplo n.º 10
0
def wait_until_action_to_code(config, action_id, template_arg, args):
    for conditions in build_conditions(config[CONF_CONDITION], template_arg, args):
        yield None
    rhs = WaitUntilAction.new(template_arg, conditions)
    type = WaitUntilAction.template(template_arg)
    action = Pvariable(action_id, rhs, type=type)
    add(App.register_component(action))
    yield action
Ejemplo n.º 11
0
def setup_binary_sensor_core_(binary_sensor_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:
        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_automations(trigger, [], 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_automations(trigger, [], 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_automations(trigger, [], 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_automations(trigger, [], 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)),
                ))
        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_automations(trigger, [], 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_automations(trigger, [(bool_, 'x')], conf)

    setup_mqtt_component(binary_sensor_var.Pget_mqtt(), config)
Ejemplo n.º 12
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)
Ejemplo n.º 13
0
def to_code(config):
    rhs = App.init_log(
        config.get(CONF_BAUD_RATE), config.get(CONF_TX_BUFFER_SIZE),
        HARDWARE_UART_TO_UART_SELECTION[config.get(CONF_HARDWARE_UART)])
    log = Pvariable(config[CONF_ID], rhs)
    if CONF_LEVEL in config:
        add(log.set_global_log_level(LOG_LEVELS[config[CONF_LEVEL]]))
    for tag, level in config.get(CONF_LOGS, {}).items():
        add(log.set_log_level(tag, LOG_LEVELS[level]))
Ejemplo n.º 14
0
def to_code(config):
    rhs = App.make_mqtt_subscribe_sensor(config[CONF_NAME], config[CONF_TOPIC])
    subs = Pvariable(config[CONF_ID], rhs)

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

    sensor.setup_sensor(subs, config)
    setup_component(subs, config)
Ejemplo n.º 15
0
def to_code(config):
    rhs = App.init_uart(config[CONF_BAUD_RATE])
    var = Pvariable(config[CONF_ID], rhs)

    if CONF_TX_PIN in config:
        add(var.set_tx_pin(config[CONF_TX_PIN]))
    if CONF_RX_PIN in config:
        add(var.set_rx_pin(config[CONF_RX_PIN]))

    setup_component(var, config)
Ejemplo n.º 16
0
def output_set_level_to_code(config, action_id, template_arg, args):
    for var in get_variable(config[CONF_ID]):
        yield None
    rhs = var.make_set_level_action(template_arg)
    type = SetLevelAction.template(template_arg)
    action = Pvariable(action_id, rhs, type=type)
    for template_ in templatable(config[CONF_LEVEL], args, float_):
        yield None
    add(action.set_level(template_))
    yield action
Ejemplo n.º 17
0
def stepper_set_target_to_code(config, action_id, template_arg, args):
    for var in get_variable(config[CONF_ID]):
        yield None
    rhs = var.make_set_target_action(template_arg)
    type = SetTargetAction.template(template_arg)
    action = Pvariable(action_id, rhs, type=type)
    for template_ in templatable(config[CONF_TARGET], args, int32):
        yield None
    add(action.set_target(template_))
    yield action
Ejemplo n.º 18
0
Archivo: sntp.py Proyecto: khzd/pi4home
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)
Ejemplo n.º 19
0
def build_automation_(trigger, args, config):
    arg_types = [arg[0] for arg in args]
    templ = TemplateArguments(*arg_types)
    rhs = App.make_automation(templ, trigger)
    type = Automation.template(templ)
    obj = Pvariable(config[CONF_AUTOMATION_ID], rhs, type=type)
    for actions in build_actions(config[CONF_THEN], templ, args):
        yield None
    add(obj.add_actions(actions))
    yield obj
Ejemplo n.º 20
0
def while_action_to_code(config, action_id, template_arg, args):
    for conditions in build_conditions(config[CONF_CONDITION], template_arg, args):
        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], template_arg, args):
        yield None
    add(action.add_then(actions))
    yield action
Ejemplo n.º 21
0
def sensor_template_publish_to_code(config, action_id, template_arg, args):
    for var in get_variable(config[CONF_ID]):
        yield None
    rhs = var.make_sensor_publish_action(template_arg)
    type = SensorPublishAction.template(template_arg)
    action = Pvariable(action_id, rhs, type=type)
    for template_ in templatable(config[CONF_STATE], args, float_):
        yield None
    add(action.set_state(template_))
    yield action
Ejemplo n.º 22
0
def servo_write_to_code(config, action_id, template_arg, args):
    for var in get_variable(config[CONF_ID]):
        yield None
    rhs = ServoWriteAction.new(template_arg, var)
    type = ServoWriteAction.template(template_arg)
    action = Pvariable(action_id, rhs, type=type)
    for template_ in templatable(config[CONF_LEVEL], args, float_):
        yield None
    add(action.set_value(template_))
    yield action
Ejemplo n.º 23
0
def stepper_report_position_to_code(config, action_id, template_arg, args):
    for var in get_variable(config[CONF_ID]):
        yield None
    rhs = var.make_report_position_action(template_arg)
    type = ReportPositionAction.template(template_arg)
    action = Pvariable(action_id, rhs, type=type)
    for template_ in templatable(config[CONF_POSITION], args, int32):
        yield None
    add(action.set_position(template_))
    yield action
Ejemplo n.º 24
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, conf in enumerate(config[CONF_SWITCHES]):
        rhs = custom.Pget_switch(i)
        add(rhs.set_name(conf[CONF_NAME]))
        switch.register_switch(rhs, conf)
Ejemplo n.º 25
0
Archivo: adc.py Proyecto: khzd/pi4home
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))
    adc = Pvariable(config[CONF_ID], rhs)
    if CONF_ATTENUATION in config:
        add(adc.set_attenuation(ATTENUATION_MODES[config[CONF_ATTENUATION]]))
    sensor.setup_sensor(adc, config)
    setup_component(adc, config)
Ejemplo n.º 26
0
def to_code(config):
    for template_ in process_lambda(config[CONF_LAMBDA], [],
                                    return_type=std_vector.template(binary_sensor.BinarySensorPtr)):
        yield

    rhs = CustomBinarySensorConstructor(template_)
    custom = variable(config[CONF_ID], rhs)
    for i, conf in enumerate(config[CONF_BINARY_SENSORS]):
        rhs = custom.Pget_binary_sensor(i)
        add(rhs.set_name(conf[CONF_NAME]))
        binary_sensor.register_binary_sensor(rhs, conf)
Ejemplo n.º 27
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(uuid_arr)
    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)
Ejemplo n.º 28
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)
Ejemplo n.º 29
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)
Ejemplo n.º 30
0
def light_turn_off_to_code(config, action_id, template_arg, args):
    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], args,
                                     uint32):
            yield None
        add(action.set_transition_length(template_))
    yield action