From 3d9ed604a0c9cfe861b29e0c9f68f28666b7fcad Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 5 Mar 2026 15:32:14 -1000 Subject: [PATCH] Fix empty friendly_name with MAC suffix and use UTF-8 byte lengths When name_add_mac_suffix is true and friendly_name is empty, emit a mutable static char[] buffer instead of a string literal to match the char* signature. Also use UTF-8 byte lengths instead of Python str len() to handle non-ASCII characters correctly. --- esphome/core/config.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/esphome/core/config.py b/esphome/core/config.py index be3c6466e2e..f21f8ae3742 100644 --- a/esphome/core/config.py +++ b/esphome/core/config.py @@ -569,19 +569,25 @@ async def to_code(config: ConfigType) -> None: With MAC suffix: emits a static mutable buffer with placeholder suffix. Without: passes the string literal directly as const char*. - Returns (expression, length). + Returns (expression, byte_length). """ - if not value: - return cg.RawExpression('""'), 0 if name_add_mac_suffix: - value_with_placeholder = f"{value}{sep}XXXXXX" + value_with_placeholder = "" if not value else f"{value}{sep}XXXXXX" cg.add_global( cg.RawStatement( f"static char {var_name}[] = {cpp_string_escape(value_with_placeholder)};" ) ) - return cg.RawExpression(var_name), len(value_with_placeholder) - return cg.RawExpression(cpp_string_escape(value)), len(value) + return ( + cg.RawExpression(var_name), + len(value_with_placeholder.encode("utf-8")), + ) + if not value: + return cg.RawExpression('""'), 0 + return ( + cg.RawExpression(cpp_string_escape(value)), + len(value.encode("utf-8")), + ) name_expr, name_len = _make_app_name_expr( name, _APP_NAME_BUF_VAR, _APP_NAME_MAC_SEP