def lambda_action_to_code(config, action_id, arg_type): template_arg = TemplateArguments(arg_type) for lambda_ in process_lambda(config, [(arg_type, 'x')]): yield None rhs = LambdaAction.new(template_arg, lambda_) type = LambdaAction.template(template_arg) yield Pvariable(action_id, rhs, type=type)
def build_condition(config, arg_type): template_arg = TemplateArguments(arg_type) if CONF_AND in config: yield AndCondition.new( template_arg, build_conditions(config[CONF_AND], template_arg)) elif CONF_OR in config: yield OrCondition.new(template_arg, build_conditions(config[CONF_OR], template_arg)) elif CONF_LAMBDA in config: lambda_ = None for lambda_ in process_lambda(config[CONF_LAMBDA], [(arg_type, 'x')]): yield yield LambdaCondition.new(template_arg, lambda_) elif CONF_RANGE in config: conf = config[CONF_RANGE] rhs = RangeCondition.new(template_arg) type = RangeCondition.template(template_arg) condition = Pvariable(config[CONF_CONDITION_ID], rhs, type=type) if CONF_ABOVE in conf: template_ = None for template_ in templatable(conf[CONF_ABOVE], arg_type, float_): yield condition.set_min(template_) if CONF_BELOW in conf: template_ = None for template_ in templatable(conf[CONF_BELOW], arg_type, float_): yield condition.set_max(template_) yield condition else: raise ESPHomeYAMLError(u"Unsupported condition {}".format(config))
def to_code(config): rhs = App.make_template_cover(config[CONF_NAME]) make = variable(config[CONF_MAKE_ID], rhs) if CONF_LAMBDA in config: template_ = None for template_ in process_lambda(config[CONF_LAMBDA], [], return_type=optional.template( cover.CoverState)): yield add(make.Ptemplate_.set_state_lambda(template_)) if CONF_OPEN_ACTION in config: actions = None for actions in automation.build_actions(config[CONF_OPEN_ACTION], NoArg): yield add(make.Ptemplate_.add_open_actions(actions)) if CONF_CLOSE_ACTION in config: actions = None for actions in automation.build_actions(config[CONF_CLOSE_ACTION], NoArg): yield add(make.Ptemplate_.add_close_actions(actions)) if CONF_STOP_ACTION in config: actions = None for actions in automation.build_actions(config[CONF_STOP_ACTION], NoArg): yield add(make.Ptemplate_.add_stop_actions(actions)) if CONF_OPTIMISTIC in config: add(make.Ptemplate_.set_optimistic(config[CONF_OPTIMISTIC])) cover.setup_cover(make.Ptemplate_, make.Pmqtt, config)
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')]): yield add(ssd.set_writer(lambda_)) display.setup_display(ssd, config) setup_component(ssd, config)
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 model_type, model = MODELS[config[CONF_MODEL]] if model_type == 'a': rhs = App.make_waveshare_epaper_type_a(spi, cs, dc, model) epaper = Pvariable(config[CONF_ID], rhs, type=WaveshareEPaperTypeA) elif model_type == 'b': rhs = App.make_waveshare_epaper_type_b(spi, cs, dc, model) epaper = Pvariable(config[CONF_ID], rhs, type=WaveshareEPaper) else: raise NotImplementedError() if CONF_LAMBDA in config: for lambda_ in process_lambda(config[CONF_LAMBDA], [(display.DisplayBufferRef, 'it')]): yield add(epaper.set_writer(lambda_)) if CONF_RESET_PIN in config: for reset in gpio_output_pin_expression(config[CONF_RESET_PIN]): yield add(epaper.set_reset_pin(reset)) if CONF_BUSY_PIN in config: for reset in gpio_input_pin_expression(config[CONF_BUSY_PIN]): yield add(epaper.set_busy_pin(reset)) if CONF_FULL_UPDATE_EVERY in config: add(epaper.set_full_update_every(config[CONF_FULL_UPDATE_EVERY])) display.setup_display(epaper, config)
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')]): yield add(lcd.set_writer(lambda_)) display.setup_display(lcd, config)
def setup_filter(config): if CONF_OFFSET in config: return OffsetFilter.new(config[CONF_OFFSET]) if CONF_MULTIPLY in config: return MultiplyFilter.new(config[CONF_MULTIPLY]) if CONF_FILTER_OUT in config: return FilterOutValueFilter.new(config[CONF_FILTER_OUT]) if CONF_FILTER_NAN in config: return FilterOutNANFilter() if CONF_SLIDING_WINDOW_MOVING_AVERAGE in config: conf = config[CONF_SLIDING_WINDOW_MOVING_AVERAGE] return SlidingWindowMovingAverageFilter.new(conf[CONF_WINDOW_SIZE], conf[CONF_SEND_EVERY]) if CONF_EXPONENTIAL_MOVING_AVERAGE in config: conf = config[CONF_EXPONENTIAL_MOVING_AVERAGE] return ExponentialMovingAverageFilter.new(conf[CONF_ALPHA], conf[CONF_SEND_EVERY]) if CONF_LAMBDA in config: return LambdaFilter.new( process_lambda(config[CONF_LAMBDA], [(float_, 'x')])) if CONF_THROTTLE in config: return ThrottleFilter.new(config[CONF_THROTTLE]) if CONF_DELTA in config: return DeltaFilter.new(config[CONF_DELTA]) if CONF_OR in config: return OrFilter.new(setup_filters(config[CONF_OR])) if CONF_HEARTBEAT in config: return App.register_component( HeartbeatFilter.new(config[CONF_HEARTBEAT])) if CONF_DEBOUNCE in config: return App.register_component(DebounceFilter.new( config[CONF_DEBOUNCE])) if CONF_UNIQUE in config: return UniqueFilter.new() raise ValueError(u"Filter unsupported: {}".format(config))
def setup_filter(config): if CONF_OFFSET in config: yield OffsetFilter.new(config[CONF_OFFSET]) elif CONF_MULTIPLY in config: yield MultiplyFilter.new(config[CONF_MULTIPLY]) elif CONF_FILTER_OUT in config: yield FilterOutValueFilter.new(config[CONF_FILTER_OUT]) elif CONF_FILTER_NAN in config: yield FilterOutNANFilter.new() elif CONF_SLIDING_WINDOW_MOVING_AVERAGE in config: conf = config[CONF_SLIDING_WINDOW_MOVING_AVERAGE] yield SlidingWindowMovingAverageFilter.new(conf[CONF_WINDOW_SIZE], conf[CONF_SEND_EVERY]) elif CONF_EXPONENTIAL_MOVING_AVERAGE in config: conf = config[CONF_EXPONENTIAL_MOVING_AVERAGE] yield ExponentialMovingAverageFilter.new(conf[CONF_ALPHA], conf[CONF_SEND_EVERY]) elif CONF_LAMBDA in config: lambda_ = None for lambda_ in process_lambda(config[CONF_LAMBDA], [(float_, 'x')]): yield None yield LambdaFilter.new(lambda_) elif CONF_THROTTLE in config: yield ThrottleFilter.new(config[CONF_THROTTLE]) elif CONF_DELTA in config: yield DeltaFilter.new(config[CONF_DELTA]) elif CONF_OR in config: filters = None for filters in setup_filters(config[CONF_OR]): yield None yield OrFilter.new(filters) elif CONF_HEARTBEAT in config: yield App.register_component(HeartbeatFilter.new(config[CONF_HEARTBEAT])) elif CONF_DEBOUNCE in config: yield App.register_component(DebounceFilter.new(config[CONF_DEBOUNCE])) elif CONF_UNIQUE in config: yield UniqueFilter.new()
def to_code(config): rhs = App.make_template_switch(config[CONF_NAME]) make = variable(config[CONF_MAKE_ID], rhs) if CONF_LAMBDA in config: template_ = None for template_ in process_lambda(config[CONF_LAMBDA], [], return_type=optional.template(bool_)): yield add(make.Ptemplate_.set_state_lambda(template_)) if CONF_TURN_OFF_ACTION in config: actions = None for actions in automation.build_actions(config[CONF_TURN_OFF_ACTION], NoArg): yield add(make.Ptemplate_.add_turn_off_actions(actions)) if CONF_TURN_ON_ACTION in config: actions = None for actions in automation.build_actions(config[CONF_TURN_ON_ACTION], NoArg): yield add(make.Ptemplate_.add_turn_on_actions(actions)) if CONF_OPTIMISTIC in config: add(make.Ptemplate_.set_optimistic(config[CONF_OPTIMISTIC])) switch.setup_switch(make.Ptemplate_, make.Pmqtt, config)
def to_code(config): rhs = App.make_template_switch(config[CONF_NAME]) make = variable(config[CONF_MAKE_ID], rhs) template = make.Ptemplate_ switch.setup_switch(template, make.Pmqtt, config) if CONF_LAMBDA in config: for template_ in process_lambda(config[CONF_LAMBDA], [], return_type=optional.template(bool_)): yield add(template.set_state_lambda(template_)) if CONF_TURN_OFF_ACTION in config: automation.build_automation(template.get_turn_off_trigger(), NoArg, config[CONF_TURN_OFF_ACTION]) if CONF_TURN_ON_ACTION in config: automation.build_automation(template.get_turn_on_trigger(), NoArg, config[CONF_TURN_ON_ACTION]) if CONF_OPTIMISTIC in config: add(template.set_optimistic(config[CONF_OPTIMISTIC])) if CONF_RESTORE_STATE in config: add(template.set_restore_state(config[CONF_RESTORE_STATE])) setup_component(template, config)
def to_code(config): template_ = None for template_ in process_lambda(config[CONF_LAMBDA], [], return_type=optional.template(bool_)): yield rhs = App.make_template_binary_sensor(config[CONF_NAME], template_) make = variable(config[CONF_MAKE_ID], rhs) binary_sensor.setup_binary_sensor(make.Ptemplate_, make.Pmqtt, config)
def to_code(config): template_ = None for template_ in process_lambda(config[CONF_LAMBDA], [], return_type=optional.template(float_)): yield rhs = App.make_template_sensor(config[CONF_NAME], template_, config.get(CONF_UPDATE_INTERVAL)) make = variable(config[CONF_MAKE_ID], rhs) sensor.setup_sensor(make.Ptemplate_, make.Pmqtt, config)
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_))
def setup_filter(config): if CONF_INVERT in config: yield InvertFilter.new() elif CONF_DELAYED_OFF in config: yield App.register_component(DelayedOffFilter.new(config[CONF_DELAYED_OFF])) elif CONF_DELAYED_ON in config: yield App.register_component(DelayedOnFilter.new(config[CONF_DELAYED_ON])) elif CONF_LAMBDA in config: lambda_ = None for lambda_ in process_lambda(config[CONF_LAMBDA], [(bool_, 'x')]): yield None yield LambdaFilter.new(lambda_)
def logger_log_action_to_code(config, action_id, arg_type): template_arg = TemplateArguments(arg_type) esp_log = LOG_LEVEL_TO_ESP_LOG[config[CONF_LEVEL]] args = [RawExpression(unicode(x)) for x in config[CONF_ARGS]] text = unicode( statement(esp_log(config[CONF_TAG], config[CONF_FORMAT], *args))) for lambda_ in process_lambda(Lambda(text), [(arg_type, 'x')]): yield None rhs = LambdaAction.new(template_arg, lambda_) type = LambdaAction.template(template_arg) yield Pvariable(action_id, rhs, type=type)
def to_code(config): for uart in get_variable(config[CONF_UART_ID]): yield rhs = App.make_nextion(uart) nextion = Pvariable(config[CONF_ID], rhs) if CONF_LAMBDA in config: for lambda_ in process_lambda(config[CONF_LAMBDA], [(NextionRef, 'it')]): yield add(nextion.set_writer(lambda_)) display.setup_display(nextion, config)
def to_code(config): rhs = App.make_pcf8574_lcd_display(config[CONF_DIMENSIONS][0], config[CONF_DIMENSIONS][1]) lcd = Pvariable(config[CONF_ID], rhs) if CONF_ADDRESS in config: add(lcd.set_address(config[CONF_ADDRESS])) if CONF_LAMBDA in config: for lambda_ in process_lambda(config[CONF_LAMBDA], [(LCDDisplayRef, 'it')]): yield add(lcd.set_writer(lambda_)) display.setup_display(lcd, config)
def to_code(config): rhs = App.make_template_switch(config[CONF_NAME]) make = variable(MakeTemplateSwitch, config[CONF_MAKE_ID], rhs) if CONF_LAMBDA in config: template_ = process_lambda(config[CONF_LAMBDA], []) add(make.Ptemplate.set_state_lambda(template_)) if CONF_TURN_OFF_ACTION in config: actions = automation.build_actions(config[CONF_TURN_OFF_ACTION], NoArg) add(make.Ptemplate_.add_turn_off_actions(actions)) if CONF_TURN_ON_ACTION in config: actions = automation.build_actions(config[CONF_TURN_ON_ACTION], NoArg) add(make.Ptemplate_.add_turn_on_actions(actions)) if CONF_OPTIMISTIC in config: add(make.Ptemplate_.set_optimistic(config[CONF_OPTIMISTIC])) switch.setup_switch(make.Ptemplate_, make.Pmqtt, config)
def mqtt_publish_json_action_to_code(config, action_id, arg_type): template_arg = TemplateArguments(arg_type) rhs = App.Pget_mqtt_client().Pmake_publish_json_action(template_arg) type = MQTTPublishJsonAction.template(template_arg) action = Pvariable(action_id, rhs, type=type) for template_ in templatable(config[CONF_TOPIC], arg_type, std_string): yield None add(action.set_topic(template_)) for lambda_ in process_lambda(config[CONF_PAYLOAD], [(arg_type, 'x'), (JsonObjectRef, 'root')]): yield None add(action.set_payload(lambda_)) if CONF_QOS in config: add(action.set_qos(config[CONF_QOS])) if CONF_RETAIN in config: add(action.set_retain(config[CONF_RETAIN])) yield action
def to_code(config): ssd = Pvariable(config[CONF_ID], App.make_i2c_ssd1306()) add(ssd.set_model(ssd1306_spi.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_ADDRESS in config: add(ssd.set_address(config[CONF_ADDRESS])) if CONF_LAMBDA in config: for lambda_ in process_lambda(config[CONF_LAMBDA], [(display.DisplayBufferRef, 'it')]): yield add(ssd.set_writer(lambda_)) display.setup_display(ssd, config)
def to_code(config): rhs = App.make_template_cover(config[CONF_NAME]) make = variable(MakeTemplateCover, config[CONF_MAKE_ID], rhs) if CONF_LAMBDA in config: template_ = process_lambda(config[CONF_LAMBDA], []) add(make.Ptemplate.set_state_lambda(template_)) if CONF_OPEN_ACTION in config: actions = automation.build_actions(config[CONF_OPEN_ACTION], NoArg) add(make.Ptemplate_.add_open_actions(actions)) if CONF_CLOSE_ACTION in config: actions = automation.build_actions(config[CONF_CLOSE_ACTION], NoArg) add(make.Ptemplate_.add_close_actions(actions)) if CONF_STOP_ACTION in config: actions = automation.build_actions(config[CONF_STOP_ACTION], NoArg) add(make.Ptemplate_.add_stop_actions(actions)) if CONF_OPTIMISTIC in config: add(make.Ptemplate_.set_optimistic(config[CONF_OPTIMISTIC])) cover.setup_cover(make.Ptemplate_, make.Pmqtt, config)
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')]): yield add(max7219.set_writer(lambda_)) display.setup_display(max7219, config)
def build_condition(config, arg_type): template_arg = TemplateArguments(arg_type) if CONF_AND in config: return AndCondition.new( template_arg, build_conditions(config[CONF_AND], template_arg)) if CONF_OR in config: return OrCondition.new(template_arg, build_conditions(config[CONF_OR], template_arg)) if CONF_LAMBDA in config: return LambdaCondition.new( template_arg, process_lambda(config[CONF_LAMBDA], [(arg_type, 'x')])) if CONF_RANGE in config: conf = config[CONF_RANGE] rhs = RangeCondition.new(template_arg) condition = Pvariable(RangeCondition.template(template_arg), config[CONF_CONDITION_ID], rhs) if CONF_ABOVE in conf: condition.set_min(templatable(conf[CONF_ABOVE], arg_type, float_)) if CONF_BELOW in conf: condition.set_max(templatable(conf[CONF_BELOW], arg_type, float_)) return condition raise ESPHomeYAMLError(u"Unsupported condition {}".format(config))
def to_code(config): rhs = App.make_template_cover(config[CONF_NAME]) make = variable(config[CONF_MAKE_ID], rhs) cover.setup_cover(make.Ptemplate_, make.Pmqtt, config) setup_component(make.Ptemplate_, config) if CONF_LAMBDA in config: for template_ in process_lambda(config[CONF_LAMBDA], [], return_type=optional.template( cover.CoverState)): yield add(make.Ptemplate_.set_state_lambda(template_)) if CONF_OPEN_ACTION in config: automation.build_automation(make.Ptemplate_.get_open_trigger(), NoArg, config[CONF_OPEN_ACTION]) if CONF_CLOSE_ACTION in config: automation.build_automation(make.Ptemplate_.get_close_trigger(), NoArg, config[CONF_CLOSE_ACTION]) if CONF_STOP_ACTION in config: automation.build_automation(make.Ptemplate_.get_stop_trigger(), NoArg, config[CONF_STOP_ACTION]) if CONF_OPTIMISTIC in config: add(make.Ptemplate_.set_optimistic(config[CONF_OPTIMISTIC]))
def to_code(config): template_ = process_lambda(config[CONF_LAMBDA], []) rhs = App.make_template_binary_sensor(config[CONF_NAME], template_) make = variable(MakeTemplateBinarySensor, config[CONF_MAKE_ID], rhs) binary_sensor.setup_binary_sensor(make.Ptemplate_, make.Pmqtt, config)
def to_code(config): template_ = process_lambda(config[CONF_LAMBDA], []) rhs = App.make_template_sensor(config[CONF_NAME], template_, config.get(CONF_UPDATE_INTERVAL)) make = variable(MakeTemplateSensor, config[CONF_MAKE_ID], rhs) sensor.setup_sensor(make.Ptemplate_, make.Pmqtt, config)
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_FASTLED_LAMBDA: lambda_ = None args = [(RawExpression('FastLEDLightOutputComponent &'), 'it')] for lambda_ in process_lambda(config[CONF_LAMBDA], args): yield None yield FastLEDLambdaLightEffect.new(config[CONF_NAME], lambda_, config[CONF_UPDATE_INTERVAL]) elif key == CONF_FASTLED_RAINBOW: rhs = FastLEDRainbowLightEffect.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_FASTLED_COLOR_WIPE: rhs = FastLEDColorWipeEffect.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( FastLEDColorWipeEffectColor, ('r', color[CONF_RED]), ('g', color[CONF_GREEN]), ('b', color[CONF_BLUE]), ('random', color[CONF_RANDOM]), ('num_leds', color[CONF_NUM_LEDS]), )) if colors: add(effect.set_colors(ArrayInitializer(*colors))) yield effect elif key == CONF_FASTLED_SCAN: rhs = FastLEDScanEffect.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_FASTLED_TWINKLE: rhs = FastLEDTwinkleEffect.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_FASTLED_RANDOM_TWINKLE: rhs = FastLEDRandomTwinkleEffect.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_FASTLED_FIREWORKS: rhs = FastLEDFireworksEffect.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_FASTLED_FLICKER: rhs = FastLEDFlickerEffect.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())))
def build_action(full_config, arg_type): from esphomeyaml.components import light, mqtt, switch template_arg = TemplateArguments(arg_type) # Keep pylint from freaking out var = None action_id = full_config[CONF_ACTION_ID] key, config = next( (k, v) for k, v in full_config.items() if k in ACTION_KEYS) if key == CONF_DELAY: 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 elif key == CONF_LAMBDA: for lambda_ in process_lambda(config, [(arg_type, 'x')]): yield None rhs = LambdaAction.new(template_arg, lambda_) type = LambdaAction.template(template_arg) yield Pvariable(action_id, rhs, type=type) elif key == CONF_MQTT_PUBLISH: rhs = App.Pget_mqtt_client().Pmake_publish_action(template_arg) type = mqtt.MQTTPublishAction.template(template_arg) action = Pvariable(action_id, rhs, type=type) for template_ in templatable(config[CONF_TOPIC], arg_type, std_string): yield None add(action.set_topic(template_)) for template_ in templatable(config[CONF_PAYLOAD], arg_type, std_string): yield None add(action.set_payload(template_)) if CONF_QOS in config: for template_ in templatable(config[CONF_QOS], arg_type, uint8): yield add(action.set_qos(template_)) if CONF_RETAIN in config: for template_ in templatable(config[CONF_RETAIN], arg_type, bool_): yield None add(action.set_retain(template_)) yield action elif key == CONF_LIGHT_TOGGLE: for var in get_variable(config[CONF_ID]): yield None rhs = var.make_toggle_action(template_arg) type = light.ToggleAction.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 elif key == CONF_LIGHT_TURN_OFF: for var in get_variable(config[CONF_ID]): yield None rhs = var.make_turn_off_action(template_arg) type = light.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 elif key == CONF_LIGHT_TURN_ON: for var in get_variable(config[CONF_ID]): yield None rhs = var.make_turn_on_action(template_arg) type = light.TurnOnAction.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_)) if CONF_FLASH_LENGTH in config: for template_ in templatable(config[CONF_FLASH_LENGTH], arg_type, uint32): yield None add(action.set_flash_length(template_)) if CONF_BRIGHTNESS in config: for template_ in templatable(config[CONF_BRIGHTNESS], arg_type, float_): yield None add(action.set_brightness(template_)) if CONF_RED in config: for template_ in templatable(config[CONF_RED], arg_type, float_): yield None add(action.set_red(template_)) if CONF_GREEN in config: for template_ in templatable(config[CONF_GREEN], arg_type, float_): yield None add(action.set_green(template_)) if CONF_BLUE in config: for template_ in templatable(config[CONF_BLUE], arg_type, float_): yield None add(action.set_blue(template_)) if CONF_WHITE in config: for template_ in templatable(config[CONF_WHITE], arg_type, float_): yield None add(action.set_white(template_)) if CONF_COLOR_TEMPERATURE in config: for template_ in templatable(config[CONF_COLOR_TEMPERATURE], arg_type, float_): yield None add(action.set_color_temperature(template_)) if CONF_EFFECT in config: for template_ in templatable(config[CONF_EFFECT], arg_type, std_string): yield None add(action.set_effect(template_)) yield action elif key == CONF_SWITCH_TOGGLE: for var in get_variable(config[CONF_ID]): yield None rhs = var.make_toggle_action(template_arg) type = switch.ToggleAction.template(arg_type) yield Pvariable(action_id, rhs, type=type) elif key == CONF_SWITCH_TURN_OFF: for var in get_variable(config[CONF_ID]): yield None rhs = var.make_turn_off_action(template_arg) type = switch.TurnOffAction.template(arg_type) yield Pvariable(action_id, rhs, type=type) elif key == CONF_SWITCH_TURN_ON: for var in get_variable(config[CONF_ID]): yield None rhs = var.make_turn_on_action(template_arg) type = switch.TurnOnAction.template(arg_type) yield Pvariable(action_id, rhs, type=type) elif key == CONF_COVER_OPEN: for var in get_variable(config[CONF_ID]): yield None rhs = var.make_open_action(template_arg) type = cover.OpenAction.template(arg_type) yield Pvariable(action_id, rhs, type=type) elif key == CONF_COVER_CLOSE: for var in get_variable(config[CONF_ID]): yield None rhs = var.make_close_action(template_arg) type = cover.CloseAction.template(arg_type) yield Pvariable(action_id, rhs, type=type) elif key == CONF_COVER_STOP: for var in get_variable(config[CONF_ID]): yield None rhs = var.make_stop_action(template_arg) type = cover.StopAction.template(arg_type) yield Pvariable(action_id, rhs, type=type) elif key == CONF_FAN_TOGGLE: for var in get_variable(config[CONF_ID]): yield None rhs = var.make_toggle_action(template_arg) type = fan.ToggleAction.template(arg_type) yield Pvariable(action_id, rhs, type=type) elif key == CONF_FAN_TURN_OFF: for var in get_variable(config[CONF_ID]): yield None rhs = var.make_turn_off_action(template_arg) type = fan.TurnOffAction.template(arg_type) yield Pvariable(action_id, rhs, type=type) elif key == CONF_FAN_TURN_ON: for var in get_variable(config[CONF_ID]): yield None rhs = var.make_turn_on_action(template_arg) type = fan.TurnOnAction.template(arg_type) action = Pvariable(action_id, rhs, type=type) if CONF_OSCILLATING in config: for template_ in templatable(config[CONF_OSCILLATING], arg_type, bool_): yield None add(action.set_oscillating(template_)) if CONF_SPEED in config: for template_ in templatable(config[CONF_SPEED], arg_type, fan.FanSpeed): yield None add(action.set_speed(template_)) yield action elif key == CONF_OUTPUT_TURN_OFF: for var in get_variable(config[CONF_ID]): yield None rhs = var.make_turn_off_action(template_arg) type = output.TurnOffAction.template(arg_type) yield Pvariable(action_id, rhs, type=type) elif key == CONF_OUTPUT_TURN_ON: for var in get_variable(config[CONF_ID]): yield None rhs = var.make_turn_on_action(template_arg) type = output.TurnOnAction.template(arg_type) yield Pvariable(action_id, rhs, type=type) elif key == CONF_OUTPUT_SET_LEVEL: for var in get_variable(config[CONF_ID]): yield None rhs = var.make_set_level_action(template_arg) type = output.SetLevelAction.template(arg_type) action = Pvariable(action_id, rhs, type=type) for template_ in templatable(config[CONF_LEVEL], arg_type, bool_): yield None add(action.set_level(template_)) yield action elif key == CONF_IF: for conditions in build_conditions(config[CONF_CONDITION], arg_type): yield None rhs = IfAction.new(template_arg, conditions) type = IfAction.template(template_arg) action = Pvariable(action_id, rhs, type=type) if CONF_THEN in config: for actions in build_actions(config[CONF_THEN], arg_type): yield None add(action.add_then(actions)) if CONF_ELSE in config: for actions in build_actions(config[CONF_ELSE], arg_type): yield None add(action.add_else(actions)) yield action elif key == CONF_DEEP_SLEEP_ENTER: for var in get_variable(config[CONF_ID]): yield None rhs = var.make_enter_deep_sleep_action(template_arg) type = deep_sleep.EnterDeepSleepAction.template(arg_type) yield Pvariable(action_id, rhs, type=type) elif key == CONF_DEEP_SLEEP_PREVENT: for var in get_variable(config[CONF_ID]): yield None rhs = var.make_prevent_deep_sleep_action(template_arg) type = deep_sleep.PreventDeepSleepAction.template(arg_type) yield Pvariable(action_id, rhs, type=type) else: raise ESPHomeYAMLError(u"Unsupported action {}".format(config))
def build_action(config, arg_type): from esphomeyaml.components import light, mqtt, switch template_arg = TemplateArguments(arg_type) # Keep pylint from freaking out var = None if CONF_DELAY in config: rhs = App.register_component(DelayAction.new(template_arg)) type = DelayAction.template(template_arg) action = Pvariable(config[CONF_ACTION_ID], rhs, type=type) template_ = None for template_ in templatable(config[CONF_DELAY], arg_type, uint32): yield add(action.set_delay(template_)) yield action elif CONF_LAMBDA in config: lambda_ = None for lambda_ in process_lambda(config[CONF_LAMBDA], [(arg_type, 'x')]): yield None rhs = LambdaAction.new(template_arg, lambda_) type = LambdaAction.template(template_arg) yield Pvariable(config[CONF_ACTION_ID], rhs, type=type) elif CONF_MQTT_PUBLISH in config: conf = config[CONF_MQTT_PUBLISH] rhs = App.Pget_mqtt_client().Pmake_publish_action(template_arg) type = mqtt.MQTTPublishAction.template(template_arg) action = Pvariable(config[CONF_ACTION_ID], rhs, type=type) template_ = None for template_ in templatable(conf[CONF_TOPIC], arg_type, std_string): yield None add(action.set_topic(template_)) template_ = None for template_ in templatable(conf[CONF_PAYLOAD], arg_type, std_string): yield None add(action.set_payload(template_)) if CONF_QOS in conf: template_ = None for template_ in templatable(conf[CONF_QOS], arg_type, uint8): yield add(action.set_qos(template_)) if CONF_RETAIN in conf: template_ = None for template_ in templatable(conf[CONF_RETAIN], arg_type, bool_): yield None add(action.set_retain(template_)) yield action elif CONF_LIGHT_TOGGLE in config: conf = config[CONF_LIGHT_TOGGLE] for var in get_variable(conf[CONF_ID]): yield None rhs = var.make_toggle_action(template_arg) type = light.ToggleAction.template(template_arg) action = Pvariable(config[CONF_ACTION_ID], rhs, type=type) if CONF_TRANSITION_LENGTH in conf: template_ = None for template_ in templatable(conf[CONF_TRANSITION_LENGTH], arg_type, uint32): yield None add(action.set_transition_length(template_)) yield action elif CONF_LIGHT_TURN_OFF in config: conf = config[CONF_LIGHT_TURN_OFF] for var in get_variable(conf[CONF_ID]): yield None rhs = var.make_turn_off_action(template_arg) type = light.TurnOffAction.template(template_arg) action = Pvariable(config[CONF_ACTION_ID], rhs, type=type) if CONF_TRANSITION_LENGTH in conf: template_ = None for template_ in templatable(conf[CONF_TRANSITION_LENGTH], arg_type, uint32): yield None add(action.set_transition_length(template_)) yield action elif CONF_LIGHT_TURN_ON in config: conf = config[CONF_LIGHT_TURN_ON] for var in get_variable(conf[CONF_ID]): yield None rhs = var.make_turn_on_action(template_arg) type = light.TurnOnAction.template(template_arg) action = Pvariable(config[CONF_ACTION_ID], rhs, type=type) if CONF_TRANSITION_LENGTH in conf: template_ = None for template_ in templatable(conf[CONF_TRANSITION_LENGTH], arg_type, uint32): yield None add(action.set_transition_length(template_)) if CONF_FLASH_LENGTH in conf: template_ = None for template_ in templatable(conf[CONF_FLASH_LENGTH], arg_type, uint32): yield None add(action.set_flash_length(template_)) if CONF_BRIGHTNESS in conf: template_ = None for template_ in templatable(conf[CONF_BRIGHTNESS], arg_type, float_): yield None add(action.set_brightness(template_)) if CONF_RED in conf: template_ = None for template_ in templatable(conf[CONF_RED], arg_type, float_): yield None add(action.set_red(template_)) if CONF_GREEN in conf: template_ = None for template_ in templatable(conf[CONF_GREEN], arg_type, float_): yield None add(action.set_green(template_)) if CONF_BLUE in conf: template_ = None for template_ in templatable(conf[CONF_BLUE], arg_type, float_): yield None add(action.set_blue(template_)) if CONF_WHITE in conf: template_ = None for template_ in templatable(conf[CONF_WHITE], arg_type, float_): yield None add(action.set_white(template_)) if CONF_EFFECT in conf: template_ = None for template_ in templatable(conf[CONF_EFFECT], arg_type, std_string): yield None add(action.set_effect(template_)) yield action elif CONF_SWITCH_TOGGLE in config: conf = config[CONF_SWITCH_TOGGLE] for var in get_variable(conf[CONF_ID]): yield None rhs = var.make_toggle_action(template_arg) type = switch.ToggleAction.template(arg_type) yield Pvariable(config[CONF_ACTION_ID], rhs, type=type) elif CONF_SWITCH_TURN_OFF in config: conf = config[CONF_SWITCH_TURN_OFF] for var in get_variable(conf[CONF_ID]): yield None rhs = var.make_turn_off_action(template_arg) type = switch.TurnOffAction.template(arg_type) yield Pvariable(config[CONF_ACTION_ID], rhs, type=type) elif CONF_SWITCH_TURN_ON in config: conf = config[CONF_SWITCH_TURN_ON] for var in get_variable(conf[CONF_ID]): yield None rhs = var.make_turn_on_action(template_arg) type = switch.TurnOnAction.template(arg_type) yield Pvariable(config[CONF_ACTION_ID], rhs, type=type) elif CONF_COVER_OPEN in config: conf = config[CONF_COVER_OPEN] for var in get_variable(conf[CONF_ID]): yield None rhs = var.make_open_action(template_arg) type = cover.OpenAction.template(arg_type) yield Pvariable(config[CONF_ACTION_ID], rhs, type=type) elif CONF_COVER_CLOSE in config: conf = config[CONF_COVER_CLOSE] for var in get_variable(conf[CONF_ID]): yield None rhs = var.make_close_action(template_arg) type = cover.CloseAction.template(arg_type) yield Pvariable(config[CONF_ACTION_ID], rhs, type=type) elif CONF_COVER_STOP in config: conf = config[CONF_COVER_STOP] for var in get_variable(conf[CONF_ID]): yield None rhs = var.make_stop_action(template_arg) type = cover.StopAction.template(arg_type) yield Pvariable(config[CONF_ACTION_ID], rhs, type=type) elif CONF_FAN_TOGGLE in config: conf = config[CONF_FAN_TOGGLE] for var in get_variable(conf[CONF_ID]): yield None rhs = var.make_toggle_action(template_arg) type = fan.ToggleAction.template(arg_type) yield Pvariable(config[CONF_ACTION_ID], rhs, type=type) elif CONF_FAN_TURN_OFF in config: conf = config[CONF_FAN_TURN_OFF] for var in get_variable(conf[CONF_ID]): yield None rhs = var.make_turn_off_action(template_arg) type = fan.TurnOffAction.template(arg_type) yield Pvariable(config[CONF_ACTION_ID], rhs, type=type) elif CONF_FAN_TURN_ON in config: conf = config[CONF_FAN_TURN_ON] for var in get_variable(conf[CONF_ID]): yield None rhs = var.make_turn_on_action(template_arg) type = fan.TurnOnAction.template(arg_type) action = Pvariable(config[CONF_ACTION_ID], rhs, type=type) if CONF_OSCILLATING in config: template_ = None for template_ in templatable(conf[CONF_OSCILLATING], arg_type, bool_): yield None add(action.set_oscillating(template_)) if CONF_SPEED in config: template_ = None for template_ in templatable(conf[CONF_SPEED], arg_type, fan.FanSpeed): yield None add(action.set_speed(template_)) yield action else: raise ESPHomeYAMLError(u"Unsupported action {}".format(config))