Avoids requiring T to be default-constructible, which would break
future instantiations with non-default-constructible state types.
Capture old_state before set_state_value() since the pointer aliases
subclass storage that gets overwritten.
Check state == new_state directly before calling virtual set_new_state.
Avoids the virtual dispatch chain (BinarySensor::set_new_state →
StatefulEntityBase::set_new_state → virtual get_state()) on the
common no-change path. Fixes -20% CodSpeed regression on
BinarySensorPublish_NoChange benchmark.
- Cache get_state() result to avoid calling the virtual method twice
(once for comparison, once for old_state construction)
- Move full_state_callbacks_.call() inside the !empty() guard so both
the optional construction and the call are skipped when no callbacks
- Swap to had_state || get_trigger_on_initial_state() so the virtual
call is skipped on the common path (every change except the first)
Saves ~21 bytes of flash in set_new_state.
When Pvariable types contain template arguments with :: (e.g.
Automation<std::optional<bool>>), the namespace extraction logic
incorrectly split on :: inside the template params, producing
invalid C++ identifiers like Automation<std__automation_id__pstorage.
Fix by stripping template arguments before extracting the namespace.
Extract the logic into a testable _extract_component_ns helper.
set_trigger_on_initial_state is only called from codegen on concrete
BinarySensor instances. It's an implementation detail of BinarySensor,
not part of the StatefulEntityBase contract. Only get_trigger_on_initial_state
remains as a pure virtual — subclasses decide how to control it.
Making set_new_state virtual means callers like send_state_internal
and invalidate_state resolve via vtable dispatch to the .cpp, avoiding
template bloat without needing out-of-line tricks or hiding declarations.
BinarySensor overrides set_new_state directly for logging and
ControllerRegistry notification, matching the original pattern.
invalidate_state() can't be declaration-only on a template class.
Keep it inline on StatefulEntityBase, and add a hiding declaration
on BinarySensor with an out-of-line definition in the .cpp to prevent
template bloat from automation.h and filter.cpp callers.
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.
set_new_state is only called internally. Making it non-virtual
eliminates a vtable entry and allows the compiler to inline it.
BinarySensor now overrides on_state_changed_() for logging and
ControllerRegistry notification instead of overriding set_new_state.
Move set_has_state and set_state_value_ before callback invocations
so callback code can inspect the entity's current state via
get_state()/has_state(). The old state is passed as a parameter.
Addresses Copilot review feedback.
Move send_state_internal back to .cpp to avoid duplicating it at each
call site (publish_state and Filter::output).
Optimize set_new_state to compare has_state + value directly instead
of constructing optional<T> on stack for the comparison. Optionals
are only constructed in the changed path for callbacks that need them.
Use pure virtual get/set methods on StatefulEntityBase instead of
storing the flag in EntityBase::flags_. This keeps EntityBase clean
and avoids mixing binary_sensor-specific state into the base entity.
Replace optional<T> state_ in StatefulEntityBase with pure virtual
get_state()/set_state_value_() that subclasses implement. BinarySensor
stores its value in the existing public bool state member and uses
EntityBase::flags_.has_state instead of the optional's discriminant.
Move trigger_on_initial_state_ into EntityBase::flags_ (1 bit from
reserved), eliminating the standalone bool and its padding.
Saves 8 bytes per BinarySensor instance:
- optional<bool> (2B) + padding (2B) = 4B removed
- bool trigger_on_initial_state_ (1B) + padding (3B) = 4B removed