[binary_sensor] Move send_state_internal and invalidate_state to .cpp

set_new_state is a template method (~189 bytes when instantiated).
Inlining callers like send_state_internal and invalidate_state causes
the template code to be duplicated at every call site (publish_state,
Filter::output, BinarySensorInvalidateAction::play, etc.), adding
~384 bytes of flash.

Keep these as out-of-line definitions in the .cpp so the template
is instantiated once.
This commit is contained in:
J. Nick Koston
2026-03-22 16:26:16 -10:00
parent 58498bacff
commit 349f45f582
3 changed files with 8 additions and 2 deletions
@@ -32,6 +32,10 @@ void BinarySensor::publish_initial_state(bool new_state) {
this->invalidate_state();
this->publish_state(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::on_state_changed_(const optional<bool> &old_state, const optional<bool> &new_state, bool had_state) {
StatefulEntityBase::on_state_changed_(old_state, new_state, had_state);
@@ -57,7 +57,8 @@ class BinarySensor : public StatefulEntityBase<bool> {
// ========== INTERNAL METHODS ==========
// (In most use cases you won't need these)
void send_state_internal(bool new_state) { this->set_new_state(new_state); }
/// Defined in .cpp to avoid inlining set_new_state template code at every call site.
void send_state_internal(bool new_state);
/// Return whether this binary sensor has outputted a state.
virtual bool is_status_binary_sensor() const;
+2 -1
View File
@@ -320,7 +320,8 @@ template<typename T> 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.
void invalidate_state() { this->set_new_state({}); }
/// Defined out-of-line in subclass .cpp to avoid inlining set_new_state template code at every call site.
void invalidate_state();
template<typename F> void add_full_state_callback(F &&callback) {
this->full_state_callbacks_.add(std::forward<F>(callback));