Exemplo n.º 1
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_CYCLES in config:
        add(deep_sleep.set_run_cycles(config[CONF_RUN_CYCLES]))
    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.º 2
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)
Exemplo n.º 3
0
def exp_mqtt_message(config):
    if config is None:
        return optional(TemplateArguments(MQTTMessage))
    exp = StructInitializer(MQTTMessage, ('topic', config[CONF_TOPIC]),
                            ('payload', config.get(CONF_PAYLOAD, "")),
                            ('qos', config[CONF_QOS]),
                            ('retain', config[CONF_RETAIN]))
    return exp
Exemplo n.º 4
0
def manual_ip(config):
    if config is None:
        return None
    return StructInitializer(
        ManualIP,
        ('static_ip', safe_ip(config[CONF_STATIC_IP])),
        ('gateway', safe_ip(config[CONF_GATEWAY])),
        ('subnet', safe_ip(config[CONF_SUBNET])),
        ('dns1', safe_ip(config.get(CONF_DNS1))),
        ('dns2', safe_ip(config.get(CONF_DNS2))),
    )
Exemplo n.º 5
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())))