diff --git a/esphome/components/api/__init__.py b/esphome/components/api/__init__.py index f4c1b2b0bd..4d6a116e67 100644 --- a/esphome/components/api/__init__.py +++ b/esphome/components/api/__init__.py @@ -530,17 +530,23 @@ async def homeassistant_service_to_code( # Initialize FixedVectors with exact sizes from config cg.add(var.init_data(len(config[CONF_DATA]))) for key, value in config[CONF_DATA].items(): - templ = await cg.templatable(value, args, cg.std_string) + # output_type=None because lambdas can return non-string types (int, + # float, char*) that TemplatableStringValue converts via to_string. + # Static strings are manually wrapped for PROGMEM on ESP8266. + templ = await cg.templatable(value, args, None) + if isinstance(templ, str): + templ = cg.FlashStringLiteral(templ) cg.add(var.add_data(cg.FlashStringLiteral(key), templ)) cg.add(var.init_data_template(len(config[CONF_DATA_TEMPLATE]))) for key, value in config[CONF_DATA_TEMPLATE].items(): - templ = await cg.templatable(value, args, cg.std_string) + templ = await cg.templatable(value, args, None) + if isinstance(templ, str): + templ = cg.FlashStringLiteral(templ) cg.add(var.add_data_template(cg.FlashStringLiteral(key), templ)) cg.add(var.init_variables(len(config[CONF_VARIABLES]))) for key, value in config[CONF_VARIABLES].items(): - # Variables can return any type (float, int, etc.), not just strings templ = await cg.templatable(value, args, None) cg.add(var.add_variable(cg.FlashStringLiteral(key), templ)) @@ -616,17 +622,23 @@ async def homeassistant_event_to_code(config, action_id, template_arg, args): # Initialize FixedVectors with exact sizes from config cg.add(var.init_data(len(config[CONF_DATA]))) for key, value in config[CONF_DATA].items(): - templ = await cg.templatable(value, args, cg.std_string) + # output_type=None because lambdas can return non-string types (int, + # float, char*) that TemplatableStringValue converts via to_string. + # Static strings are manually wrapped for PROGMEM on ESP8266. + templ = await cg.templatable(value, args, None) + if isinstance(templ, str): + templ = cg.FlashStringLiteral(templ) cg.add(var.add_data(cg.FlashStringLiteral(key), templ)) cg.add(var.init_data_template(len(config[CONF_DATA_TEMPLATE]))) for key, value in config[CONF_DATA_TEMPLATE].items(): - templ = await cg.templatable(value, args, cg.std_string) + templ = await cg.templatable(value, args, None) + if isinstance(templ, str): + templ = cg.FlashStringLiteral(templ) cg.add(var.add_data_template(cg.FlashStringLiteral(key), templ)) cg.add(var.init_variables(len(config[CONF_VARIABLES]))) for key, value in config[CONF_VARIABLES].items(): - # Variables can return any type (float, int, etc.), not just strings templ = await cg.templatable(value, args, None) cg.add(var.add_variable(cg.FlashStringLiteral(key), templ))