Exemplo n.º 1
0
def to_code(config):
    for pin in gpio_input_pin_expression(config[CONF_PIN]):
        yield
    rhs = App.make_duty_cycle_sensor(config[CONF_NAME], pin,
                                     config.get(CONF_UPDATE_INTERVAL))
    duty = Pvariable(config[CONF_ID], rhs)
    sensor.setup_sensor(duty, config)
    setup_component(duty, config)
Exemplo n.º 2
0
def setup_switch_core_(switch_var, config):
    if CONF_INTERNAL in config:
        add(switch_var.set_internal(config[CONF_INTERNAL]))
    if CONF_ICON in config:
        add(switch_var.set_icon(config[CONF_ICON]))
    if CONF_INVERTED in config:
        add(switch_var.set_inverted(config[CONF_INVERTED]))
    for conf in config.get(CONF_ON_TURN_ON, []):
        rhs = switch_var.make_switch_turn_on_trigger()
        trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs)
        automation.build_automations(trigger, [], conf)
    for conf in config.get(CONF_ON_TURN_OFF, []):
        rhs = switch_var.make_switch_turn_off_trigger()
        trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs)
        automation.build_automations(trigger, [], conf)

    setup_mqtt_component(switch_var.Pget_mqtt(), config)
Exemplo n.º 3
0
def fan_turn_on_to_code(config, action_id, template_arg, args):
    for var in get_variable(config[CONF_ID]):
        yield None
    rhs = var.make_turn_on_action(template_arg)
    type = TurnOnAction.template(template_arg)
    action = Pvariable(action_id, rhs, type=type)
    if CONF_OSCILLATING in config:
        for template_ in templatable(config[CONF_OSCILLATING], args, bool_):
            yield None
        add(action.set_oscillating(template_))
    if CONF_SPEED in config:
        for template_ in templatable(config[CONF_SPEED], args, FanSpeed):
            yield None
        if isinstance(template_, string_types):
            template_ = FAN_SPEEDS[template_]
        add(action.set_speed(template_))
    yield action
Exemplo n.º 4
0
def to_code(config):
    for uart_ in get_variable(config[CONF_UART_ID]):
        yield

    rhs = App.make_cse7766(uart_, config.get(CONF_UPDATE_INTERVAL))
    cse = Pvariable(config[CONF_ID], rhs)

    if CONF_VOLTAGE in config:
        conf = config[CONF_VOLTAGE]
        sensor.register_sensor(cse.make_voltage_sensor(conf[CONF_NAME]), conf)
    if CONF_CURRENT in config:
        conf = config[CONF_CURRENT]
        sensor.register_sensor(cse.make_current_sensor(conf[CONF_NAME]), conf)
    if CONF_POWER in config:
        conf = config[CONF_POWER]
        sensor.register_sensor(cse.make_power_sensor(conf[CONF_NAME]), conf)
    setup_component(cse, config)
Exemplo n.º 5
0
def to_code(config):
    for hub in get_variable(config[CONF_ESP32_BLE_ID]):
        yield
    rhs = hub.make_xiaomi_device(make_address_array(config[CONF_MAC_ADDRESS]))
    dev = Pvariable(config[CONF_MAKE_ID], rhs)
    if CONF_TEMPERATURE in config:
        conf = config[CONF_TEMPERATURE]
        sensor.register_sensor(dev.Pmake_temperature_sensor(conf[CONF_NAME]),
                               conf)
    if CONF_HUMIDITY in config:
        conf = config[CONF_HUMIDITY]
        sensor.register_sensor(dev.Pmake_humidity_sensor(conf[CONF_NAME]),
                               conf)
    if CONF_BATTERY_LEVEL in config:
        conf = config[CONF_BATTERY_LEVEL]
        sensor.register_sensor(dev.Pmake_battery_level_sensor(conf[CONF_NAME]),
                               conf)
Exemplo n.º 6
0
def to_code(config):
    pin = None
    for pin in gpio_input_pin_expression(config[CONF_PIN]):
        yield
    rhs = App.make_gpio_binary_sensor(config[CONF_NAME], pin)
    gpio = Pvariable(config[CONF_ID], rhs)
    binary_sensor.setup_binary_sensor(gpio, config)
    setup_component(gpio, config)
Exemplo n.º 7
0
def to_code(config):
    for trigger in gpio_output_pin_expression(config[CONF_TRIGGER_PIN]):
        yield
    for echo in gpio_input_pin_expression(config[CONF_ECHO_PIN]):
        yield
    rhs = App.make_ultrasonic_sensor(config[CONF_NAME], trigger, echo,
                                     config.get(CONF_UPDATE_INTERVAL))
    ultrasonic = Pvariable(config[CONF_ID], rhs)

    if CONF_TIMEOUT in config:
        add(ultrasonic.set_timeout_us(config[CONF_TIMEOUT] / (0.000343 / 2)))

    if CONF_PULSE_TIME in config:
        add(ultrasonic.set_pulse_time_us(config[CONF_PULSE_TIME]))

    sensor.setup_sensor(ultrasonic, config)
    setup_component(ultrasonic, config)
Exemplo n.º 8
0
def wait_until_action_to_code(config, action_id, arg_type, template_arg):
    for conditions in build_conditions(config[CONF_CONDITION], arg_type):
        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
Exemplo n.º 9
0
def to_code(config):
    for output_ in get_variable(config[CONF_OUTPUT]):
        yield
    rhs = App.make_output_switch(config[CONF_NAME], output_)
    switch_ = Pvariable(config[CONF_ID], rhs)

    switch.setup_switch(switch_, config)
    setup_component(switch, config)
Exemplo n.º 10
0
def to_code(config):
    for hub in get_variable(config[CONF_ESP32_BLE_ID]):
        yield
    rhs = hub.make_xiaomi_device(make_address_array(config[CONF_MAC_ADDRESS]))
    dev = Pvariable(config[CONF_ID], rhs)
    if CONF_TEMPERATURE in config:
        conf = config[CONF_TEMPERATURE]
        sensor.register_sensor(dev.Pmake_temperature_sensor(conf[CONF_NAME]),
                               conf)
    if CONF_MOISTURE in config:
        conf = config[CONF_MOISTURE]
        sensor.register_sensor(dev.Pmake_moisture_sensor(conf[CONF_NAME]),
                               conf)
    if CONF_ILLUMINANCE in config:
        conf = config[CONF_ILLUMINANCE]
        sensor.register_sensor(dev.Pmake_illuminance_sensor(conf[CONF_NAME]),
                               conf)
    if CONF_CONDUCTIVITY in config:
        conf = config[CONF_CONDUCTIVITY]
        sensor.register_sensor(dev.Pmake_conductivity_sensor(conf[CONF_NAME]),
                               conf)
    if CONF_BATTERY_LEVEL in config:
        conf = config[CONF_BATTERY_LEVEL]
        sensor.register_sensor(dev.Pmake_battery_level_sensor(conf[CONF_NAME]),
                               conf)
Exemplo n.º 11
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)
Exemplo n.º 12
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)
Exemplo n.º 13
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)
Exemplo n.º 14
0
def to_code(config):
    rhs = App.init_ethernet()
    eth = Pvariable(config[CONF_ID], rhs)

    add(eth.set_phy_addr(config[CONF_PHY_ADDR]))
    add(eth.set_mdc_pin(config[CONF_MDC_PIN]))
    add(eth.set_mdio_pin(config[CONF_MDIO_PIN]))
    add(eth.set_type(ETHERNET_TYPES[config[CONF_TYPE]]))
    add(eth.set_clk_mode(CLK_MODES[config[CONF_CLK_MODE]]))
    add(eth.set_use_address(config[CONF_USE_ADDRESS]))

    if CONF_POWER_PIN in config:
        for pin in gpio_output_pin_expression(config[CONF_POWER_PIN]):
            yield
        add(eth.set_power_pin(pin))

    if CONF_MANUAL_IP in config:
        add(eth.set_manual_ip(wifi.manual_ip(config[CONF_MANUAL_IP])))
Exemplo n.º 15
0
def to_code(config):
    for pin_a in gpio_input_pin_expression(config[CONF_PIN_A]):
        yield
    for pin_b in gpio_input_pin_expression(config[CONF_PIN_B]):
        yield
    rhs = App.make_rotary_encoder_sensor(config[CONF_NAME], pin_a, pin_b)
    encoder = Pvariable(config[CONF_ID], rhs)

    if CONF_PIN_RESET in config:
        for pin_i in gpio_input_pin_expression(config[CONF_PIN_RESET]):
            yield
        add(encoder.set_reset_pin(pin_i))
    if CONF_RESOLUTION in config:
        resolution = RESOLUTIONS[config[CONF_RESOLUTION]]
        add(encoder.set_resolution(resolution))

    sensor.setup_sensor(encoder, config)
    setup_component(encoder, config)
Exemplo n.º 16
0
def to_code(config):
    for uart_ in get_variable(config[CONF_UART_ID]):
        yield
    data = config[CONF_DATA]
    if isinstance(data, str):
        data = [HexInt(ord(x)) for x in data]
    rhs = App.make_uart_switch(uart_, config[CONF_NAME], data)
    var = Pvariable(config[CONF_ID], rhs)
    switch.setup_switch(var, config)
Exemplo n.º 17
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)
    gpio = Pvariable(config[CONF_ID], rhs)

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

    if CONF_INTERLOCK in config:
        interlock = []
        for it in config[CONF_INTERLOCK]:
            for lock in get_variable(it):
                yield
            interlock.append(lock)
        add(gpio.set_interlock(interlock))

    switch.setup_switch(gpio, config)
    setup_component(gpio, config)
Exemplo n.º 18
0
def to_code(config):
    power_supply = None
    if CONF_POWER_SUPPLY in config:
        for power_supply in get_variable(config[CONF_POWER_SUPPLY]):
            yield
    for pca9685 in get_variable(config[CONF_PCA9685_ID]):
        yield
    rhs = pca9685.create_channel(config[CONF_CHANNEL], power_supply)
    out = Pvariable(config[CONF_ID], rhs)
    output.setup_output_platform(out, config, skip_power_supply=True)
Exemplo n.º 19
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_max6675_sensor(config[CONF_NAME], spi_, cs,
                                  config.get(CONF_UPDATE_INTERVAL))
    max6675 = Pvariable(config[CONF_ID], rhs)
    sensor.setup_sensor(max6675, config)
    setup_component(max6675, config)
Exemplo n.º 20
0
def to_code(config):
    add(App.set_name(config[CONF_NAME]))

    for conf in config.get(CONF_ON_BOOT, []):
        rhs = App.register_component(
            StartupTrigger.new(conf.get(CONF_PRIORITY)))
        trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs)
        automation.build_automations(trigger, [], conf)

    for conf in config.get(CONF_ON_SHUTDOWN, []):
        trigger = Pvariable(conf[CONF_TRIGGER_ID], ShutdownTrigger.new())
        automation.build_automations(trigger, [(const_char_ptr, 'x')], conf)

    for conf in config.get(CONF_ON_LOOP, []):
        rhs = App.register_component(LoopTrigger.new())
        trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs)
        automation.build_automations(trigger, [], conf)

    add(App.set_compilation_datetime(RawExpression('__DATE__ ", " __TIME__')))
Exemplo n.º 21
0
def to_code(config):
    for time_ in get_variable(config[CONF_TIME_ID]):
        yield
    for sens in get_variable(config[CONF_POWER_ID]):
        yield
    rhs = App.make_total_daily_energy_sensor(config[CONF_NAME], time_, sens)
    total_energy = Pvariable(config[CONF_ID], rhs)

    sensor.setup_sensor(total_energy, config)
    setup_component(total_energy, config)
Exemplo n.º 22
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_config in enumerate(config.get(CONF_COMPONENTS, [])):
        comp = Pvariable(comp_config[CONF_ID], custom.get_component(i))
        setup_component(comp, comp_config)
Exemplo n.º 23
0
def to_code(config):
    for pin_a in gpio_output_pin_expression(config[CONF_PIN_A]):
        yield
    for pin_b in gpio_output_pin_expression(config[CONF_PIN_B]):
        yield
    for pin_c in gpio_output_pin_expression(config[CONF_PIN_C]):
        yield
    for pin_d in gpio_output_pin_expression(config[CONF_PIN_D]):
        yield
    rhs = App.make_uln2003(pin_a, pin_b, pin_c, pin_d)
    uln = Pvariable(config[CONF_ID], rhs)

    if CONF_SLEEP_WHEN_DONE in config:
        add(uln.set_sleep_when_done(config[CONF_SLEEP_WHEN_DONE]))

    if CONF_STEP_MODE in config:
        add(uln.set_step_mode(STEP_MODES[config[CONF_STEP_MODE]]))

    stepper.setup_stepper(uln, config)
    setup_component(uln, config)
Exemplo n.º 24
0
def to_code(config):
    power_supply = None
    if CONF_POWER_SUPPLY in config:
        for power_supply in get_variable(config[CONF_POWER_SUPPLY]):
            yield
    my9231 = None
    for my9231 in get_variable(config[CONF_MY9231_ID]):
        yield
    rhs = my9231.create_channel(config[CONF_CHANNEL], power_supply)
    out = Pvariable(config[CONF_ID], rhs)
    output.setup_output_platform(out, config, skip_power_supply=True)
    setup_component(out, config)
Exemplo n.º 25
0
def setup_text_sensor_core_(text_sensor_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_automations(trigger, [(std_string, 'x')], conf)

    setup_mqtt_component(text_sensor_var.Pget_mqtt(), config)
Exemplo n.º 26
0
def logger_log_action_to_code(config, action_id, template_arg, args):
    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), args, return_type=void):
        yield None
    rhs = LambdaAction.new(template_arg, lambda_)
    type = LambdaAction.template(template_arg)
    yield Pvariable(action_id, rhs, type=type)
Exemplo n.º 27
0
def to_code(config):
    rhs = App.init_api_server()
    api = Pvariable(config[CONF_ID], rhs)

    if config[CONF_PORT] != 6053:
        add(api.set_port(config[CONF_PORT]))
    if config.get(CONF_PASSWORD):
        add(api.set_password(config[CONF_PASSWORD]))
    if CONF_REBOOT_TIMEOUT in config:
        add(api.set_reboot_timeout(config[CONF_REBOOT_TIMEOUT]))

    for conf in config.get(CONF_SERVICES, []):
        template_args = []
        func_args = []
        service_type_args = []
        for name, var_ in conf[CONF_VARIABLES].items():
            native = SERVICE_ARG_NATIVE_TYPES[var_]
            template_args.append(native)
            func_args.append((native, name))
            service_type_args.append(ServiceTypeArgument(name, SERVICE_ARG_TYPES[var_]))
        func = api.make_user_service_trigger.template(*template_args)
        rhs = func(conf[CONF_SERVICE], service_type_args)
        type_ = UserService.template(*template_args)
        trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs, type=type_)
        automation.build_automations(trigger, func_args, conf)

    setup_component(api, config)
Exemplo n.º 28
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)
Exemplo n.º 29
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')],
                                      return_type=void):
            yield
        add(max7219.set_writer(lambda_))

    display.setup_display(max7219, config)
    setup_component(max7219, config)
Exemplo n.º 30
0
def setup_display_core_(display_var, config):
    if CONF_UPDATE_INTERVAL in config:
        add(display_var.set_update_interval(config[CONF_UPDATE_INTERVAL]))
    if CONF_ROTATION in config:
        add(display_var.set_rotation(DISPLAY_ROTATIONS[config[CONF_ROTATION]]))
    if CONF_PAGES in config:
        pages = []
        for conf in config[CONF_PAGES]:
            for lambda_ in process_lambda(conf[CONF_LAMBDA],
                                          [(DisplayBufferRef, 'it')],
                                          return_type=void):
                yield
            var = Pvariable(conf[CONF_ID], DisplayPage.new(lambda_))
            pages.append(var)
        add(display_var.set_pages(pages))