mirror of
https://github.com/esphome/esphome.git
synced 2026-09-16 01:28:39 +00:00
[binary_sensor] Skip old_state construction when no full_state_callbacks registered
This commit is contained in:
@@ -351,9 +351,12 @@ template<typename T> class StatefulEntityBase : public EntityBase {
|
||||
} else if (!had_state) {
|
||||
return false; // already invalidated, no change
|
||||
}
|
||||
// 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<T> old_state = had_state ? optional<T>(this->get_state()) : nullopt;
|
||||
// State changed — update storage before firing callbacks so callback code
|
||||
// can inspect the entity's current state via get_state()/has_state()
|
||||
// Only construct old_state optional when full_state_callbacks need it
|
||||
optional<T> old_state;
|
||||
if (!this->full_state_callbacks_.empty())
|
||||
old_state = had_state ? optional<T>(this->get_state()) : nullopt;
|
||||
this->flags_.has_state = new_state.has_value();
|
||||
if (new_state.has_value()) {
|
||||
this->set_state_value_(new_state.value());
|
||||
|
||||
Reference in New Issue
Block a user