From 5854fa1d8235349915b57d91fbdb3c008d5107e9 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 22 Mar 2026 16:17:35 -1000 Subject: [PATCH] [binary_sensor] Access flags_ directly in set_new_state to reduce flash --- esphome/core/entity_base.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/esphome/core/entity_base.h b/esphome/core/entity_base.h index e542d60e65..d19ef558ec 100644 --- a/esphome/core/entity_base.h +++ b/esphome/core/entity_base.h @@ -343,8 +343,8 @@ template class StatefulEntityBase : public EntityBase { * Returns true if the state actually changed, false if it was the same. */ virtual bool set_new_state(const optional &new_state) { - // Compare without constructing optional — avoid unnecessary codegen - bool had_state = this->has_state(); + // Access flags_ directly to avoid virtual/function call overhead in this hot path + bool had_state = this->flags_.has_state; if (new_state.has_value()) { if (had_state && this->get_state() == new_state.value()) return false; // same value, no change @@ -354,7 +354,7 @@ template class StatefulEntityBase : public EntityBase { // State changed — capture old state, then update storage before firing callbacks // so callback code can inspect the entity's current state via get_state()/has_state() optional old_state = had_state ? optional(this->get_state()) : nullopt; - this->set_has_state(new_state.has_value()); + this->flags_.has_state = new_state.has_value(); if (new_state.has_value()) { this->set_state_value_(new_state.value()); }