From ed1a9fd1e2ea475329585c14dbcddb0fb7b337b4 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 3 Feb 2026 03:54:06 +0100 Subject: [PATCH] [light] Store "none" effect string in flash and avoid heap allocation --- esphome/components/light/light_call.cpp | 3 ++- esphome/components/light/light_state.h | 10 +++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/esphome/components/light/light_call.cpp b/esphome/components/light/light_call.cpp index 6d42dd1513..2d8df62c5a 100644 --- a/esphome/components/light/light_call.cpp +++ b/esphome/components/light/light_call.cpp @@ -4,6 +4,7 @@ #include "light_state.h" #include "esphome/core/log.h" #include "esphome/core/optional.h" +#include "esphome/core/progmem.h" namespace esphome::light { @@ -509,7 +510,7 @@ color_mode_bitmask_t LightCall::get_suitable_color_modes_mask_() { } LightCall &LightCall::set_effect(const char *effect, size_t len) { - if (len == 4 && strncasecmp(effect, "none", 4) == 0) { + if (len == 4 && ESPHOME_strncasecmp_P(effect, ESPHOME_PSTR("none"), 4) == 0) { this->set_effect(0); return *this; } diff --git a/esphome/components/light/light_state.h b/esphome/components/light/light_state.h index 83b9226d03..13fdd337df 100644 --- a/esphome/components/light/light_state.h +++ b/esphome/components/light/light_state.h @@ -12,6 +12,7 @@ #include "light_transformer.h" #include "esphome/core/helpers.h" +#include "esphome/core/progmem.h" #include #include @@ -188,17 +189,20 @@ class LightState : public EntityBase, public Component { uint32_t get_current_effect_index() const { return this->active_effect_index_; } /// Get effect index by name. Returns 0 if effect not found. - uint32_t get_effect_index(const std::string &effect_name) const { - if (str_equals_case_insensitive(effect_name, "none")) { + uint32_t get_effect_index(const char *effect_name) const { + if (ESPHOME_strcasecmp_P(effect_name, ESPHOME_PSTR("none")) == 0) { return 0; } for (size_t i = 0; i < this->effects_.size(); i++) { - if (str_equals_case_insensitive(effect_name, this->effects_[i]->get_name())) { + if (strcasecmp(effect_name, this->effects_[i]->get_name().c_str()) == 0) { return i + 1; // Effects are 1-indexed in active_effect_index_ } } return 0; // Effect not found } + uint32_t get_effect_index(const std::string &effect_name) const { + return this->get_effect_index(effect_name.c_str()); + } /// Get effect by index. Returns nullptr if index is invalid. LightEffect *get_effect_by_index(uint32_t index) const {