async def to_code(config): model_type, model = MODELS[config[CONF_MODEL]] if model_type == "a": rhs = WaveshareEPaperTypeA.new(model) var = cg.Pvariable(config[CONF_ID], rhs, WaveshareEPaperTypeA) elif model_type in ("b", "c"): rhs = model.new() var = cg.Pvariable(config[CONF_ID], rhs, model) else: raise NotImplementedError() await cg.register_component(var, config) await display.register_display(var, config) await spi.register_spi_device(var, config) dc = await cg.gpio_pin_expression(config[CONF_DC_PIN]) cg.add(var.set_dc_pin(dc)) if CONF_LAMBDA in config: lambda_ = await cg.process_lambda(config[CONF_LAMBDA], [(display.DisplayBufferRef, "it")], return_type=cg.void) cg.add(var.set_writer(lambda_)) if CONF_RESET_PIN in config: reset = await cg.gpio_pin_expression(config[CONF_RESET_PIN]) cg.add(var.set_reset_pin(reset)) if CONF_BUSY_PIN in config: reset = await cg.gpio_pin_expression(config[CONF_BUSY_PIN]) cg.add(var.set_busy_pin(reset)) if CONF_FULL_UPDATE_EVERY in config: cg.add(var.set_full_update_every(config[CONF_FULL_UPDATE_EVERY]))
async def to_code(config): if config[CONF_MODEL] == "M5STACK": lcd_type = ILI9341M5Stack if config[CONF_MODEL] == "TFT_2.4": lcd_type = ILI9341TFT24 rhs = lcd_type.new() var = cg.Pvariable(config[CONF_ID], rhs) await cg.register_component(var, config) await display.register_display(var, config) await spi.register_spi_device(var, config) cg.add(var.set_model(config[CONF_MODEL])) dc = await cg.gpio_pin_expression(config[CONF_DC_PIN]) cg.add(var.set_dc_pin(dc)) if CONF_LAMBDA in config: lambda_ = await cg.process_lambda(config[CONF_LAMBDA], [(display.DisplayBufferRef, "it")], return_type=cg.void) cg.add(var.set_writer(lambda_)) if CONF_RESET_PIN in config: reset = await cg.gpio_pin_expression(config[CONF_RESET_PIN]) cg.add(var.set_reset_pin(reset)) if CONF_LED_PIN in config: led_pin = await cg.gpio_pin_expression(config[CONF_LED_PIN]) cg.add(var.set_led_pin(led_pin))
def to_code(config): if config[CONF_MODEL] == 'M5STACK': lcd_type = ILI9341M5Stack if config[CONF_MODEL] == 'TFT_2.4': lcd_type = ILI9341_24_TFT rhs = lcd_type.new() var = cg.Pvariable(config[CONF_ID], rhs, type=lcd_type) yield cg.register_component(var, config) yield display.register_display(var, config) yield spi.register_spi_device(var, config) cg.add(var.set_model(config[CONF_MODEL])) dc = yield cg.gpio_pin_expression(config[CONF_DC_PIN]) cg.add(var.set_dc_pin(dc)) if CONF_LAMBDA in config: lambda_ = yield cg.process_lambda(config[CONF_LAMBDA], [(display.DisplayBufferRef, 'it')], return_type=cg.void) cg.add(var.set_writer(lambda_)) if CONF_RESET_PIN in config: reset = yield cg.gpio_pin_expression(config[CONF_RESET_PIN]) cg.add(var.set_reset_pin(reset)) if CONF_LED_PIN in config: led_pin = yield cg.gpio_pin_expression(config[CONF_LED_PIN]) cg.add(var.set_led_pin(led_pin))
async def to_code(config): has_white = "W" in config[CONF_TYPE] method = config[CONF_METHOD] method_template = METHODS[method[CONF_TYPE]].to_code( method, config[CONF_VARIANT], config[CONF_INVERT] ) if has_white: out_type = NeoPixelRGBWLightOutput.template(method_template) else: out_type = NeoPixelRGBLightOutput.template(method_template) rhs = out_type.new() var = cg.Pvariable(config[CONF_OUTPUT_ID], rhs, out_type) await light.register_light(var, config) await cg.register_component(var, config) if CONF_PIN in config: cg.add(var.add_leds(config[CONF_NUM_LEDS], config[CONF_PIN])) else: cg.add( var.add_leds( config[CONF_NUM_LEDS], config[CONF_CLOCK_PIN], config[CONF_DATA_PIN] ) ) cg.add(var.set_pixel_order(getattr(ESPNeoPixelOrder, config[CONF_TYPE]))) # https://github.com/Makuna/NeoPixelBus/blob/master/library.json cg.add_library("makuna/NeoPixelBus", "2.6.9")
async def to_code(config): pin = await cg.gpio_pin_expression(config[CONF_PIN]) rhs = StatusLED.new(pin) var = cg.Pvariable(config[CONF_ID], rhs) await cg.register_component(var, config) cg.add(var.pre_setup()) cg.add_define("USE_STATUS_LED")
def to_code(config): baud_rate = config[CONF_BAUD_RATE] rhs = Logger.new( baud_rate, config[CONF_TX_BUFFER_SIZE], HARDWARE_UART_TO_UART_SELECTION[config[CONF_HARDWARE_UART]]) log = cg.Pvariable(config[CONF_ID], rhs) cg.add(log.pre_setup()) for tag, level in config[CONF_LOGS].items(): cg.add(log.set_log_level(tag, LOG_LEVELS[level])) level = config[CONF_LEVEL] cg.add_define('USE_LOGGER') this_severity = LOG_LEVEL_SEVERITY.index(level) cg.add_build_flag('-DESPHOME_LOG_LEVEL={}'.format(LOG_LEVELS[level])) verbose_severity = LOG_LEVEL_SEVERITY.index('VERBOSE') very_verbose_severity = LOG_LEVEL_SEVERITY.index('VERY_VERBOSE') is_at_least_verbose = this_severity >= verbose_severity is_at_least_very_verbose = this_severity >= very_verbose_severity has_serial_logging = baud_rate != 0 if CORE.is_esp8266 and has_serial_logging and is_at_least_verbose: debug_serial_port = HARDWARE_UART_TO_SERIAL[config.get( CONF_HARDWARE_UART)] cg.add_build_flag(f"-DDEBUG_ESP_PORT={debug_serial_port}") cg.add_build_flag("-DLWIP_DEBUG") DEBUG_COMPONENTS = { 'HTTP_CLIENT', 'HTTP_SERVER', 'HTTP_UPDATE', 'OTA', 'SSL', 'TLS_MEM', 'UPDATER', 'WIFI', # Spams logs too much: # 'MDNS_RESPONDER', } for comp in DEBUG_COMPONENTS: cg.add_build_flag(f"-DDEBUG_ESP_{comp}") if CORE.is_esp32 and is_at_least_verbose: cg.add_build_flag('-DCORE_DEBUG_LEVEL=5') if CORE.is_esp32 and is_at_least_very_verbose: cg.add_build_flag('-DENABLE_I2C_DEBUG_BUFFER') if config.get(CONF_ESP8266_STORE_LOG_STRINGS_IN_FLASH): cg.add_build_flag('-DUSE_STORE_LOG_STR_IN_FLASH') # Register at end for safe mode yield cg.register_component(log, config) for conf in config.get(CONF_ON_MESSAGE, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], log, LOG_LEVEL_SEVERITY.index(conf[CONF_LEVEL])) yield automation.build_automation(trigger, [(cg.int_, 'level'), (cg.const_char_ptr, 'tag'), (cg.const_char_ptr, 'message')], conf)
def to_code(config): hub = yield cg.get_variable(config[CONF_DALLAS_ID]) if CONF_ADDRESS in config: address = config[CONF_ADDRESS] rhs = hub.Pget_sensor_by_address(address, config.get(CONF_RESOLUTION)) else: rhs = hub.Pget_sensor_by_index(config[CONF_INDEX], config.get(CONF_RESOLUTION)) var = cg.Pvariable(config[CONF_ID], rhs) yield sensor.register_sensor(var, config)
def to_code(config): template_ = yield cg.process_lambda( config[CONF_LAMBDA], [], return_type=cg.std_vector.template(switch.SwitchPtr)) rhs = CustomSwitchConstructor(template_) var = cg.variable(config[CONF_ID], rhs) for i, conf in enumerate(config[CONF_SWITCHES]): switch_ = cg.Pvariable(conf[CONF_ID], var.get_switch(i)) yield switch.register_switch(switch_, conf)
async def register_number( var, config, *, min_value: float, max_value: float, step: Optional[float] = None ): if not CORE.has_id(config[CONF_ID]): var = cg.Pvariable(config[CONF_ID], var) cg.add(cg.App.register_number(var)) await setup_number_core_( var, config, min_value=min_value, max_value=max_value, step=step )
def to_code(config): template_ = yield cg.process_lambda(config[CONF_LAMBDA], [], return_type=cg.std_vector.template( sensor.SensorPtr)) rhs = CustomSensorConstructor(template_) var = cg.variable(config[CONF_ID], rhs) for i, conf in enumerate(config[CONF_SENSORS]): sens = cg.Pvariable(conf[CONF_ID], var.get_sensor(i)) yield sensor.register_sensor(sens, conf)
def to_code(config): template_ = yield cg.process_lambda(config[CONF_LAMBDA], [], return_type=cg.std_vector.template( cg.ComponentPtr)) rhs = CustomComponentConstructor(template_) var = cg.variable(config[CONF_ID], rhs) for i, conf in enumerate(config.get(CONF_COMPONENTS, [])): comp = cg.Pvariable(conf[CONF_ID], var.get_component(i)) yield cg.register_component(comp, conf)
def to_code(config): rhs = mcp2515.new() var = cg.Pvariable(config[CONF_ID], rhs) yield canbus.register_canbus(var, config) if CONF_CLOCK in config: canclock = CAN_CLOCK[config[CONF_CLOCK]] cg.add(var.set_mcp_clock(canclock)) if CONF_MODE in config: mode = MCP_MODE[config[CONF_MODE]] cg.add(var.set_mcp_mode(mode)) yield spi.register_spi_device(var, config)
async def to_code(config): template_ = await cg.process_lambda( config[CONF_LAMBDA], [], return_type=cg.std_vector.template(text_sensor.TextSensorPtr), ) rhs = CustomTextSensorConstructor(template_) var = cg.variable(config[CONF_ID], rhs) for i, conf in enumerate(config[CONF_TEXT_SENSORS]): text = cg.Pvariable(conf[CONF_ID], var.get_text_sensor(i)) await text_sensor.register_text_sensor(text, conf)
def to_code(config): baud_rate = config[CONF_BAUD_RATE] rhs = Logger.new( baud_rate, config[CONF_TX_BUFFER_SIZE], HARDWARE_UART_TO_UART_SELECTION[config[CONF_HARDWARE_UART]]) log = cg.Pvariable(config[CONF_ID], rhs) cg.add(log.pre_setup()) for tag, level in config[CONF_LOGS].items(): cg.add(log.set_log_level(tag, LOG_LEVELS[level])) level = config[CONF_LEVEL] cg.add_define('USE_LOGGER') this_severity = LOG_LEVEL_SEVERITY.index(level) cg.add_build_flag('-DESPHOME_LOG_LEVEL={}'.format(LOG_LEVELS[level])) verbose_severity = LOG_LEVEL_SEVERITY.index('VERBOSE') very_verbose_severity = LOG_LEVEL_SEVERITY.index('VERY_VERBOSE') is_at_least_verbose = this_severity >= verbose_severity is_at_least_very_verbose = this_severity >= very_verbose_severity has_serial_logging = baud_rate != 0 if CORE.is_esp8266 and has_serial_logging and is_at_least_verbose: debug_serial_port = HARDWARE_UART_TO_SERIAL[config.get( CONF_HARDWARE_UART)] cg.add_build_flag("-DDEBUG_ESP_PORT={}".format(debug_serial_port)) cg.add_build_flag("-DLWIP_DEBUG") DEBUG_COMPONENTS = { 'HTTP_CLIENT', 'HTTP_SERVER', 'HTTP_UPDATE', 'OTA', 'SSL', 'TLS_MEM', 'UPDATER', 'WIFI', } for comp in DEBUG_COMPONENTS: cg.add_build_flag("-DDEBUG_ESP_{}".format(comp)) if CORE.is_esp32 and is_at_least_verbose: cg.add_build_flag('-DCORE_DEBUG_LEVEL=5') if CORE.is_esp32 and is_at_least_very_verbose: cg.add_build_flag('-DENABLE_I2C_DEBUG_BUFFER') if config.get(CONF_ESP8266_STORE_LOG_STRINGS_IN_FLASH): cg.add_build_flag('-DUSE_STORE_LOG_STR_IN_FLASH') # Register at end for safe mode yield cg.register_component(log, config)
def to_code(config): type = config[CONF_TYPE] if type == "binary": ret_type = output.BinaryOutputPtr klass = CustomBinaryOutputConstructor else: ret_type = output.FloatOutputPtr klass = CustomFloatOutputConstructor template_ = yield cg.process_lambda( config[CONF_LAMBDA], [], return_type=cg.std_vector.template(ret_type)) rhs = klass(template_) custom = cg.variable(config[CONF_ID], rhs) for i, conf in enumerate(config[CONF_OUTPUTS]): out = cg.Pvariable(conf[CONF_ID], custom.get_output(i)) yield output.register_output(out, conf)
def to_code(config): model = MODELS[config[CONF_MODEL]] rhs = model.new() var = cg.Pvariable(config[CONF_ID], rhs, model) yield cg.register_component(var, config) yield spi.register_spi_device(var, config) dc = yield cg.gpio_pin_expression(config[CONF_DC_PIN]) cg.add(var.set_dc_pin(dc)) reset = yield cg.gpio_pin_expression(config[CONF_RESET_PIN]) cg.add(var.set_reset_pin(reset)) if CONF_LAMBDA in config: lambda_ = yield cg.process_lambda( config[CONF_LAMBDA], [(display.DisplayBufferRef, 'it')], return_type=cg.void) cg.add(var.set_writer(lambda_)) yield display.register_display(var, config)
def to_code(config): type_ = cg.RawExpression(config[CONF_TYPE]) template_args = cg.TemplateArguments(type_) res_type = GlobalsComponent.template(template_args) initial_value = None if CONF_INITIAL_VALUE in config: initial_value = cg.RawExpression(config[CONF_INITIAL_VALUE]) rhs = GlobalsComponent.new(template_args, initial_value) glob = cg.Pvariable(config[CONF_ID], rhs, res_type) yield cg.register_component(glob, config) if config[CONF_RESTORE_VALUE]: value = config[CONF_ID].id if isinstance(value, str): value = value.encode() hash_ = int(hashlib.md5(value).hexdigest()[:8], 16) cg.add(glob.set_restore_value(hash_))
def to_code(config): has_white = 'W' in config[CONF_TYPE] template = cg.TemplateArguments(getattr(cg.global_ns, format_method(config))) if has_white: out_type = NeoPixelRGBWLightOutput.template(template) else: out_type = NeoPixelRGBLightOutput.template(template) rhs = out_type.new() var = cg.Pvariable(config[CONF_OUTPUT_ID], rhs, type=out_type) yield light.register_light(var, config) yield cg.register_component(var, config) if CONF_PIN in config: cg.add(var.add_leds(config[CONF_NUM_LEDS], config[CONF_PIN])) else: cg.add(var.add_leds(config[CONF_NUM_LEDS], config[CONF_CLOCK_PIN], config[CONF_DATA_PIN])) cg.add(var.set_pixel_order(getattr(ESPNeoPixelOrder, config[CONF_TYPE]))) cg.add_library('NeoPixelBus', '2.4.1')
async def to_code(config): if config[CONF_MODEL] == "M5STACK": lcd_type = ILI9341M5Stack if config[CONF_MODEL] == "TFT_2.4": lcd_type = ILI9341TFT24 if config[CONF_MODEL] == "TFT_2.4R": lcd_type = ILI9341TFT24R rhs = lcd_type.new() var = cg.Pvariable(config[CONF_ID], rhs) await cg.register_component(var, config) await display.register_display(var, config) await spi.register_spi_device(var, config) cg.add(var.set_model(config[CONF_MODEL])) dc = await cg.gpio_pin_expression(config[CONF_DC_PIN]) cg.add(var.set_dc_pin(dc)) if CONF_LAMBDA in config: lambda_ = await cg.process_lambda(config[CONF_LAMBDA], [(display.DisplayBufferRef, "it")], return_type=cg.void) cg.add(var.set_writer(lambda_)) if CONF_RESET_PIN in config: reset = await cg.gpio_pin_expression(config[CONF_RESET_PIN]) cg.add(var.set_reset_pin(reset)) if CONF_LED_PIN in config: led_pin = await cg.gpio_pin_expression(config[CONF_LED_PIN]) cg.add(var.set_led_pin(led_pin)) if config[CONF_COLOR_PALETTE] == "GRAYSCALE": cg.add(var.set_buffer_color_mode(ILI9341ColorMode.BITS_8_INDEXED)) rhs = [] for x in range(256): rhs.extend([HexInt(x), HexInt(x), HexInt(x)]) prog_arr = cg.progmem_array(config[CONF_RAW_DATA_ID], rhs) cg.add(var.set_palette(prog_arr)) else: pass
def to_code(config): rhs = WiFiComponent.new() wifi = cg.Pvariable(config[CONF_ID], rhs) cg.add(wifi.set_use_address(config[CONF_USE_ADDRESS])) for network in config.get(CONF_NETWORKS, []): cg.add(wifi.add_sta(wifi_network(network, config.get(CONF_MANUAL_IP)))) if CONF_AP in config: cg.add( wifi.set_ap( wifi_network(config[CONF_AP], config.get(CONF_MANUAL_IP)))) cg.add(wifi.set_reboot_timeout(config[CONF_REBOOT_TIMEOUT])) cg.add(wifi.set_power_save_mode(config[CONF_POWER_SAVE_MODE])) cg.add(wifi.set_fast_connect(config[CONF_FAST_CONNECT])) if CORE.is_esp8266: cg.add_library('ESP8266WiFi', None) cg.add_define('USE_WIFI') # Register at end for OTA safe mode yield cg.register_component(wifi, config)
async def to_code(config): has_white = "W" in config[CONF_TYPE] template = cg.TemplateArguments( getattr(cg.global_ns, format_method(config))) if has_white: out_type = NeoPixelRGBWLightOutput.template(template) else: out_type = NeoPixelRGBLightOutput.template(template) rhs = out_type.new() var = cg.Pvariable(config[CONF_OUTPUT_ID], rhs, out_type) await light.register_light(var, config) await cg.register_component(var, config) if CONF_PIN in config: cg.add(var.add_leds(config[CONF_NUM_LEDS], config[CONF_PIN])) else: cg.add( var.add_leds(config[CONF_NUM_LEDS], config[CONF_CLOCK_PIN], config[CONF_DATA_PIN])) cg.add(var.set_pixel_order(getattr(ESPNeoPixelOrder, config[CONF_TYPE]))) # https://github.com/Makuna/NeoPixelBus/blob/master/library.json cg.add_library("NeoPixelBus-esphome", "2.6.2")
def register_output(var, config): if not CORE.has_id(config[CONF_ID]): var = cg.Pvariable(config[CONF_ID], var) yield setup_output_platform_(var, config)
async def register_select(var, config, *, options: List[str]): if not CORE.has_id(config[CONF_ID]): var = cg.Pvariable(config[CONF_ID], var) cg.add(cg.App.register_select(var)) await setup_select_core_(var, config, options=options)
async def register_stepper(var, config): if not CORE.has_id(config[CONF_ID]): var = cg.Pvariable(config[CONF_ID], var) await setup_stepper_core_(var, config)
async def register_text_sensor(var, config): if not CORE.has_id(config[CONF_ID]): var = cg.Pvariable(config[CONF_ID], var) cg.add(cg.App.register_text_sensor(var)) await setup_text_sensor_core_(var, config)
async def to_code(config): paren = await cg.get_variable(config[CONF_TLC59208F_ID]) rhs = paren.create_channel(config[CONF_CHANNEL]) var = cg.Pvariable(config[CONF_ID], rhs) await output.register_output(var, config)
def register_binary_sensor(var, config): if not CORE.has_id(config[CONF_ID]): var = cg.Pvariable(config[CONF_ID], var) cg.add(cg.App.register_binary_sensor(var)) yield setup_binary_sensor_core_(var, config)
def register_fan(var, config): if not CORE.has_id(config[CONF_ID]): var = cg.Pvariable(config[CONF_ID], var) cg.add(cg.App.register_fan(var)) yield cg.register_component(var, config) yield setup_fan_core_(var, config)
def register_switch(var, config): if not CORE.has_id(config[CONF_ID]): var = cg.Pvariable(config[CONF_ID], var) cg.add(cg.App.register_switch(var)) yield setup_switch_core_(var, config)
async def register_button(var, config): if not CORE.has_id(config[CONF_ID]): var = cg.Pvariable(config[CONF_ID], var) cg.add(cg.App.register_button(var)) await setup_button_core_(var, config)