diff --git a/esphome/core/macros.h b/esphome/core/macros.h index 8b2383321b..0f0d556616 100644 --- a/esphome/core/macros.h +++ b/esphome/core/macros.h @@ -6,3 +6,14 @@ #ifdef USE_ARDUINO #include #endif + +// Portable PROGMEM string macro +// On ESP8266, PSTR() stores strings in flash memory +// On other platforms, it's a no-op +#ifndef ESPHOME_PSTR +#ifdef USE_ESP8266 +#define ESPHOME_PSTR(x) PSTR(x) +#else +#define ESPHOME_PSTR(x) (x) +#endif +#endif diff --git a/esphome/cpp_helpers.py b/esphome/cpp_helpers.py index 5164923b2d..26a7084010 100644 --- a/esphome/cpp_helpers.py +++ b/esphome/cpp_helpers.py @@ -76,11 +76,8 @@ async def register_component(var, config): "Error while finding name of component, please report this", exc_info=e ) if name is not None: - # On ESP8266, store component source strings in PROGMEM to save RAM - if CORE.is_esp8266: - add(var.set_component_source(RawExpression(f'PSTR("{name}")'))) - else: - add(var.set_component_source(name)) + # Use ESPHOME_PSTR macro which stores strings in PROGMEM on ESP8266, no-op on other platforms + add(var.set_component_source(RawExpression(f'ESPHOME_PSTR("{name}")'))) add(App.register_component(var)) return var