diff --git a/esphome/components/binary_sensor/binary_sensor.cpp b/esphome/components/binary_sensor/binary_sensor.cpp index fe7e169fea..a78fc9229c 100644 --- a/esphome/components/binary_sensor/binary_sensor.cpp +++ b/esphome/components/binary_sensor/binary_sensor.cpp @@ -34,11 +34,11 @@ void BinarySensor::publish_initial_state(bool new_state) { } // Defined out-of-line to prevent set_new_state template from being inlined at each call site, // which would duplicate ~189 bytes of template code per caller (publish_state, Filter::output, etc.) -void BinarySensor::send_state_internal(bool new_state) { this->set_new_state(new_state); } -void BinarySensor::invalidate_state() { this->set_new_state({}); } +void BinarySensor::send_state_internal(bool new_state) { this->set_new_state_(new_state); } +void BinarySensor::invalidate_state() { this->set_new_state_({}); } -void BinarySensor::on_state_changed_(const optional &old_state, const optional &new_state, bool had_state) { - StatefulEntityBase::on_state_changed_(old_state, new_state, had_state); +void BinarySensor::on_state_changed(const optional &old_state, const optional &new_state, bool had_state) { + StatefulEntityBase::on_state_changed(old_state, new_state, had_state); #if defined(USE_BINARY_SENSOR) && defined(USE_CONTROLLER_REGISTRY) ControllerRegistry::notify_binary_sensor_update(this); #endif diff --git a/esphome/components/binary_sensor/binary_sensor.h b/esphome/components/binary_sensor/binary_sensor.h index 7cd8a44a0f..11dccf7c0d 100644 --- a/esphome/components/binary_sensor/binary_sensor.h +++ b/esphome/components/binary_sensor/binary_sensor.h @@ -68,14 +68,14 @@ class BinarySensor : public StatefulEntityBase { protected: bool get_trigger_on_initial_state() const override { return this->trigger_on_initial_state_; } - void set_state_value_(const bool &value) override { this->state = value; } + void set_state_value(const bool &value) override { this->state = value; } bool trigger_on_initial_state_{true}; #ifdef USE_BINARY_SENSOR_FILTER Filter *filter_list_{nullptr}; #endif - void on_state_changed_(const optional &old_state, const optional &new_state, bool had_state) override; + void on_state_changed(const optional &old_state, const optional &new_state, bool had_state) override; }; class BinarySensorInitiallyOff : public BinarySensor { diff --git a/esphome/core/entity_base.h b/esphome/core/entity_base.h index 202f386234..de3499a6c3 100644 --- a/esphome/core/entity_base.h +++ b/esphome/core/entity_base.h @@ -300,8 +300,10 @@ void log_entity_unit_of_measurement(const char *tag, const char *prefix, const E * * Subclasses must implement: * - get_state(): return a const reference to the current value - * - set_state_value_(): store a new value (called only when the state actually changes) + * - set_state_value(): store a new value (called only when the state actually changes) * - get_trigger_on_initial_state() / set_trigger_on_initial_state(): control initial callback behavior + * - on_state_changed() (optional override): called after state updates, for logging/notifications + * - invalidate_state(): must be defined out-of-line in subclass .cpp to avoid template bloat * * This class does not store the state value — subclasses own their storage. Whether a state * has been set is tracked by EntityBase::has_state(). @@ -320,7 +322,7 @@ template class StatefulEntityBase : public EntityBase { /// Return the current state if available, otherwise return the provided default. T get_state_default(T default_value) const { return this->has_state() ? this->get_state() : default_value; } /// Clear the state — sets has_state() to false and fires callbacks with nullopt. - /// Defined out-of-line in subclass .cpp to avoid inlining set_new_state template code at every call site. + /// Defined out-of-line in subclass .cpp to avoid inlining set_new_state_ template code at every call site. void invalidate_state(); template void add_full_state_callback(F &&callback) { @@ -343,7 +345,7 @@ template class StatefulEntityBase : public EntityBase { * Pass nullopt to invalidate (clear) the state. Pass a value to set it. * Returns true if the state actually changed, false if it was the same. */ - bool set_new_state(const optional &new_state) { + bool set_new_state_(const optional &new_state) { // Access flags_ directly to avoid function call overhead in this hot path bool had_state = this->flags_.has_state; if (new_state.has_value()) { @@ -360,19 +362,19 @@ template class StatefulEntityBase : public EntityBase { old_state = had_state ? optional(this->get_state()) : nullopt; this->flags_.has_state = new_state.has_value(); if (new_state.has_value()) { - this->set_state_value_(new_state.value()); + this->set_state_value(new_state.value()); } - this->on_state_changed_(old_state, new_state, had_state); + this->on_state_changed(old_state, new_state, had_state); return true; } /// Called after state storage is updated. Subclasses override for logging/notifications. - virtual void on_state_changed_(const optional &old_state, const optional &new_state, bool had_state) { + virtual void on_state_changed(const optional &old_state, const optional &new_state, bool had_state) { this->full_state_callbacks_.call(old_state, new_state); if (new_state.has_value() && (this->get_trigger_on_initial_state() || had_state)) this->state_callbacks_.call(new_state.value()); } /// Subclasses implement this to store the actual value into their own storage. - virtual void set_state_value_(const T &value) = 0; + virtual void set_state_value(const T &value) = 0; LazyCallbackManager previous, optional current)> full_state_callbacks_; LazyCallbackManager state_callbacks_; };