def to_code(config): cg.add_global(cg.global_ns.namespace('esphome').using) cg.add( cg.App.pre_setup(config[CONF_NAME], cg.RawExpression('__DATE__ ", " __TIME__'))) for conf in config.get(CONF_ON_BOOT, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], conf.get(CONF_PRIORITY)) yield cg.register_component(trigger, conf) yield automation.build_automation(trigger, [], conf) for conf in config.get(CONF_ON_SHUTDOWN, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID]) yield cg.register_component(trigger, conf) yield automation.build_automation(trigger, [], conf) for conf in config.get(CONF_ON_LOOP, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID]) yield cg.register_component(trigger, conf) yield automation.build_automation(trigger, [], conf) # Build flags if CORE.is_esp8266 and CORE.board in ESP8266_FLASH_SIZES and \ CORE.arduino_version != ARDUINO_VERSION_ESP8266_2_3_0: flash_size = ESP8266_FLASH_SIZES[CORE.board] ld_scripts = ESP8266_LD_SCRIPTS[flash_size] ld_script = None if CORE.arduino_version in ('[email protected]', '[email protected]', '[email protected]'): ld_script = ld_scripts[0] elif CORE.arduino_version in (ARDUINO_VERSION_ESP8266_DEV, ARDUINO_VERSION_ESP8266_2_5_0, ARDUINO_VERSION_ESP8266_2_5_1, ARDUINO_VERSION_ESP8266_2_5_2): ld_script = ld_scripts[1] if ld_script is not None: cg.add_build_flag('-Wl,-T{}'.format(ld_script)) cg.add_build_flag('-fno-exceptions') # Libraries if CORE.is_esp32: cg.add_library('ESPmDNS', None) elif CORE.is_esp8266: cg.add_library('ESP8266WiFi', None) cg.add_library('ESP8266mDNS', None) for lib in config[CONF_LIBRARIES]: if '@' in lib: name, vers = lib.split('@', 1) cg.add_library(name, vers) else: cg.add_library(lib, None) cg.add_build_flag('-Wno-unused-variable') cg.add_build_flag('-Wno-unused-but-set-variable') cg.add_build_flag('-Wno-sign-compare') if config.get(CONF_ESP8266_RESTORE_FROM_FLASH, False): cg.add_define('USE_ESP8266_PREFERENCES_FLASH') if config[CONF_INCLUDES]: CORE.add_job(add_includes, config[CONF_INCLUDES])
async def to_code(config): cg.add_platformio_option("board", config[CONF_BOARD]) cg.add_build_flag("-DUSE_ESP32") cg.add_define("ESPHOME_BOARD", config[CONF_BOARD]) cg.add_build_flag(f"-DUSE_ESP32_VARIANT_{config[CONF_VARIANT]}") cg.add_define("ESPHOME_VARIANT", VARIANT_FRIENDLY[config[CONF_VARIANT]]) cg.add_platformio_option("lib_ldf_mode", "off") conf = config[CONF_FRAMEWORK] cg.add_platformio_option("platform", conf[CONF_PLATFORM_VERSION]) cg.add_platformio_option("extra_scripts", ["post:post_build.py"]) if conf[CONF_TYPE] == FRAMEWORK_ESP_IDF: cg.add_platformio_option("framework", "espidf") cg.add_build_flag("-DUSE_ESP_IDF") cg.add_build_flag("-DUSE_ESP32_FRAMEWORK_ESP_IDF") cg.add_build_flag("-Wno-nonnull-compare") cg.add_platformio_option( "platform_packages", [f"platformio/framework-espidf @ {conf[CONF_SOURCE]}"], ) add_idf_sdkconfig_option("CONFIG_PARTITION_TABLE_SINGLE_APP", False) add_idf_sdkconfig_option("CONFIG_PARTITION_TABLE_CUSTOM", True) add_idf_sdkconfig_option( "CONFIG_PARTITION_TABLE_CUSTOM_FILENAME", "partitions.csv" ) add_idf_sdkconfig_option("CONFIG_COMPILER_OPTIMIZATION_DEFAULT", False) add_idf_sdkconfig_option("CONFIG_COMPILER_OPTIMIZATION_SIZE", True) # Increase freertos tick speed from 100Hz to 1kHz so that delay() resolution is 1ms add_idf_sdkconfig_option("CONFIG_FREERTOS_HZ", 1000) # Setup watchdog add_idf_sdkconfig_option("CONFIG_ESP_TASK_WDT", True) add_idf_sdkconfig_option("CONFIG_ESP_TASK_WDT_PANIC", True) add_idf_sdkconfig_option("CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0", False) add_idf_sdkconfig_option("CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1", False) cg.add_platformio_option("board_build.partitions", "partitions.csv") for name, value in conf[CONF_SDKCONFIG_OPTIONS].items(): add_idf_sdkconfig_option(name, RawSdkconfigValue(value)) if conf[CONF_ADVANCED][CONF_IGNORE_EFUSE_MAC_CRC]: cg.add_define("USE_ESP32_IGNORE_EFUSE_MAC_CRC") add_idf_sdkconfig_option( "CONFIG_ESP32_PHY_CALIBRATION_AND_DATA_STORAGE", False ) elif conf[CONF_TYPE] == FRAMEWORK_ARDUINO: cg.add_platformio_option("framework", "arduino") cg.add_build_flag("-DUSE_ARDUINO") cg.add_build_flag("-DUSE_ESP32_FRAMEWORK_ARDUINO") cg.add_platformio_option( "platform_packages", [f"platformio/framework-arduinoespressif32 @ {conf[CONF_SOURCE]}"], ) cg.add_platformio_option("board_build.partitions", "partitions.csv")
async def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) await cg.register_component(var, config) await display.register_display(var, config) await i2c.register_i2c_device(var, config) 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_)) cg.add(var.set_greyscale(config[CONF_GREYSCALE])) cg.add(var.set_partial_updating(config[CONF_PARTIAL_UPDATING])) cg.add(var.set_full_update_every(config[CONF_FULL_UPDATE_EVERY])) ckv = await cg.gpio_pin_expression(config[CONF_CKV_PIN]) cg.add(var.set_ckv_pin(ckv)) gmod = await cg.gpio_pin_expression(config[CONF_GMOD_PIN]) cg.add(var.set_gmod_pin(gmod)) gpio0_enable = await cg.gpio_pin_expression(config[CONF_GPIO0_ENABLE_PIN]) cg.add(var.set_gpio0_enable_pin(gpio0_enable)) oe = await cg.gpio_pin_expression(config[CONF_OE_PIN]) cg.add(var.set_oe_pin(oe)) powerup = await cg.gpio_pin_expression(config[CONF_POWERUP_PIN]) cg.add(var.set_powerup_pin(powerup)) sph = await cg.gpio_pin_expression(config[CONF_SPH_PIN]) cg.add(var.set_sph_pin(sph)) spv = await cg.gpio_pin_expression(config[CONF_SPV_PIN]) cg.add(var.set_spv_pin(spv)) vcom = await cg.gpio_pin_expression(config[CONF_VCOM_PIN]) cg.add(var.set_vcom_pin(vcom)) wakeup = await cg.gpio_pin_expression(config[CONF_WAKEUP_PIN]) cg.add(var.set_wakeup_pin(wakeup)) cl = await cg.gpio_pin_expression(config[CONF_CL_PIN]) cg.add(var.set_cl_pin(cl)) le = await cg.gpio_pin_expression(config[CONF_LE_PIN]) cg.add(var.set_le_pin(le)) display_data_0 = await cg.gpio_pin_expression( config[CONF_DISPLAY_DATA_0_PIN]) cg.add(var.set_display_data_0_pin(display_data_0)) display_data_1 = await cg.gpio_pin_expression( config[CONF_DISPLAY_DATA_1_PIN]) cg.add(var.set_display_data_1_pin(display_data_1)) display_data_2 = await cg.gpio_pin_expression( config[CONF_DISPLAY_DATA_2_PIN]) cg.add(var.set_display_data_2_pin(display_data_2)) display_data_3 = await cg.gpio_pin_expression( config[CONF_DISPLAY_DATA_3_PIN]) cg.add(var.set_display_data_3_pin(display_data_3)) display_data_4 = await cg.gpio_pin_expression( config[CONF_DISPLAY_DATA_4_PIN]) cg.add(var.set_display_data_4_pin(display_data_4)) display_data_5 = await cg.gpio_pin_expression( config[CONF_DISPLAY_DATA_5_PIN]) cg.add(var.set_display_data_5_pin(display_data_5)) display_data_6 = await cg.gpio_pin_expression( config[CONF_DISPLAY_DATA_6_PIN]) cg.add(var.set_display_data_6_pin(display_data_6)) display_data_7 = await cg.gpio_pin_expression( config[CONF_DISPLAY_DATA_7_PIN]) cg.add(var.set_display_data_7_pin(display_data_7)) cg.add_build_flag("-DBOARD_HAS_PSRAM")
async 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(f"-DESPHOME_LOG_LEVEL={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") if CORE.using_esp_idf: if config[CONF_HARDWARE_UART] == USB_CDC: add_idf_sdkconfig_option("CONFIG_ESP_CONSOLE_USB_CDC", True) elif config[CONF_HARDWARE_UART] == USB_SERIAL_JTAG: add_idf_sdkconfig_option("CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG", True) # Register at end for safe mode await 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])) await automation.build_automation( trigger, [ (cg.int_, "level"), (cg.const_char_ptr, "tag"), (cg.const_char_ptr, "message"), ], conf, )
def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) yield cg.register_component(var, config) # https://github.com/OttoWinter/async-mqtt-client/blob/master/library.json cg.add_library("AsyncMqttClient-esphome", "0.8.4") cg.add_define("USE_MQTT") cg.add_global(mqtt_ns.using) cg.add(var.set_broker_address(config[CONF_BROKER])) cg.add(var.set_broker_port(config[CONF_PORT])) cg.add(var.set_username(config[CONF_USERNAME])) cg.add(var.set_password(config[CONF_PASSWORD])) if CONF_CLIENT_ID in config: cg.add(var.set_client_id(config[CONF_CLIENT_ID])) discovery = config[CONF_DISCOVERY] discovery_retain = config[CONF_DISCOVERY_RETAIN] discovery_prefix = config[CONF_DISCOVERY_PREFIX] if not discovery: cg.add(var.disable_discovery()) elif discovery == "CLEAN": cg.add(var.set_discovery_info(discovery_prefix, discovery_retain, True)) elif CONF_DISCOVERY_RETAIN in config or CONF_DISCOVERY_PREFIX in config: cg.add(var.set_discovery_info(discovery_prefix, discovery_retain)) cg.add(var.set_topic_prefix(config[CONF_TOPIC_PREFIX])) birth_message = config[CONF_BIRTH_MESSAGE] if not birth_message: cg.add(var.disable_birth_message()) else: cg.add(var.set_birth_message(exp_mqtt_message(birth_message))) will_message = config[CONF_WILL_MESSAGE] if not will_message: cg.add(var.disable_last_will()) else: cg.add(var.set_last_will(exp_mqtt_message(will_message))) shutdown_message = config[CONF_SHUTDOWN_MESSAGE] if not shutdown_message: cg.add(var.disable_shutdown_message()) else: cg.add(var.set_shutdown_message(exp_mqtt_message(shutdown_message))) log_topic = config[CONF_LOG_TOPIC] if not log_topic: cg.add(var.disable_log_message()) else: cg.add(var.set_log_message_template(exp_mqtt_message(log_topic))) if CONF_LEVEL in log_topic: cg.add(var.set_log_level(logger.LOG_LEVELS[log_topic[CONF_LEVEL]])) if CONF_SSL_FINGERPRINTS in config: for fingerprint in config[CONF_SSL_FINGERPRINTS]: arr = [ cg.RawExpression("0x{}".format(fingerprint[i:i + 2])) for i in range(0, 40, 2) ] cg.add(var.add_ssl_fingerprint(arr)) cg.add_build_flag("-DASYNC_TCP_SSL_ENABLED=1") cg.add(var.set_keep_alive(config[CONF_KEEPALIVE])) cg.add(var.set_reboot_timeout(config[CONF_REBOOT_TIMEOUT])) for conf in config.get(CONF_ON_MESSAGE, []): trig = cg.new_Pvariable(conf[CONF_TRIGGER_ID], conf[CONF_TOPIC]) cg.add(trig.set_qos(conf[CONF_QOS])) if CONF_PAYLOAD in conf: cg.add(trig.set_payload(conf[CONF_PAYLOAD])) yield cg.register_component(trig, conf) yield automation.build_automation(trig, [(cg.std_string, "x")], conf) for conf in config.get(CONF_ON_JSON_MESSAGE, []): trig = cg.new_Pvariable(conf[CONF_TRIGGER_ID], conf[CONF_TOPIC], conf[CONF_QOS]) yield automation.build_automation(trig, [(cg.JsonObjectConstRef, "x")], conf)
async def to_code(config): cg.add(esp8266_ns.setup_preferences()) cg.add_platformio_option("lib_ldf_mode", "off") cg.add_platformio_option("board", config[CONF_BOARD]) cg.add_build_flag("-DUSE_ESP8266") cg.add_define("ESPHOME_BOARD", config[CONF_BOARD]) cg.add_define("ESPHOME_VARIANT", "ESP8266") cg.add_platformio_option("extra_scripts", ["post:post_build.py"]) conf = config[CONF_FRAMEWORK] cg.add_platformio_option("framework", "arduino") cg.add_build_flag("-DUSE_ARDUINO") cg.add_build_flag("-DUSE_ESP8266_FRAMEWORK_ARDUINO") cg.add_platformio_option("platform", conf[CONF_PLATFORM_VERSION]) cg.add_platformio_option( "platform_packages", [f"platformio/framework-arduinoespressif8266 @ {conf[CONF_SOURCE]}"], ) # Default for platformio is LWIP2_LOW_MEMORY with: # - MSS=536 # - LWIP_FEATURES enabled # - this only adds some optional features like IP incoming packet reassembly and NAPT # see also: # https://github.com/esp8266/Arduino/blob/master/tools/sdk/lwip2/include/lwipopts.h # Instead we use LWIP2_HIGHER_BANDWIDTH_LOW_FLASH with: # - MSS=1460 # - LWIP_FEATURES disabled (because we don't need them) # Other projects like Tasmota & ESPEasy also use this cg.add_build_flag( "-DPIO_FRAMEWORK_ARDUINO_LWIP2_HIGHER_BANDWIDTH_LOW_FLASH") if config[CONF_RESTORE_FROM_FLASH]: cg.add_define("USE_ESP8266_PREFERENCES_FLASH") # Arduino 2 has a non-standards conformant new that returns a nullptr instead of failing when # out of memory and exceptions are disabled. Since Arduino 2.6.0, this flag can be used to make # new abort instead. Use it so that OOM fails early (on allocation) instead of on dereference of # a NULL pointer (so the stacktrace makes more sense), and for consistency with Arduino 3, # which always aborts if exceptions are disabled. # For cases where nullptrs can be handled, use nothrow: `new (std::nothrow) T;` cg.add_build_flag("-DNEW_OOM_ABORT") cg.add_platformio_option("board_build.flash_mode", config[CONF_BOARD_FLASH_MODE]) if config[CONF_BOARD] in ESP8266_FLASH_SIZES: flash_size = ESP8266_FLASH_SIZES[config[CONF_BOARD]] ld_scripts = ESP8266_LD_SCRIPTS[flash_size] ver = CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION] if ver <= cv.Version(2, 3, 0): # No ld script support ld_script = None if ver <= cv.Version(2, 4, 2): # Old ld script path ld_script = ld_scripts[0] else: ld_script = ld_scripts[1] if ld_script is not None: cg.add_platformio_option("board_build.ldscript", ld_script)
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)