mirror of
https://github.com/esphome/esphome.git
synced 2026-09-02 02:56:01 +00:00
[binary_sensor] Make set_new_state non-virtual, add on_state_changed_ hook
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.
This commit is contained in:
@@ -33,16 +33,12 @@ void BinarySensor::publish_initial_state(bool new_state) {
|
||||
this->publish_state(new_state);
|
||||
}
|
||||
|
||||
bool BinarySensor::set_new_state(const optional<bool> &new_state) {
|
||||
if (StatefulEntityBase::set_new_state(new_state)) {
|
||||
// weirdly, this file could be compiled even without USE_BINARY_SENSOR defined
|
||||
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);
|
||||
#if defined(USE_BINARY_SENSOR) && defined(USE_CONTROLLER_REGISTRY)
|
||||
ControllerRegistry::notify_binary_sensor_update(this);
|
||||
ControllerRegistry::notify_binary_sensor_update(this);
|
||||
#endif
|
||||
ESP_LOGD(TAG, "'%s' >> %s", this->get_name().c_str(), ONOFFMAYBE(new_state));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
ESP_LOGD(TAG, "'%s' >> %s", this->get_name().c_str(), ONOFFMAYBE(new_state));
|
||||
}
|
||||
|
||||
#ifdef USE_BINARY_SENSOR_FILTER
|
||||
|
||||
@@ -74,7 +74,7 @@ class BinarySensor : public StatefulEntityBase<bool> {
|
||||
Filter *filter_list_{nullptr};
|
||||
#endif
|
||||
|
||||
bool set_new_state(const optional<bool> &new_state) override;
|
||||
void on_state_changed_(const optional<bool> &old_state, const optional<bool> &new_state, bool had_state) override;
|
||||
};
|
||||
|
||||
class BinarySensorInitiallyOff : public BinarySensor {
|
||||
|
||||
@@ -342,8 +342,8 @@ template<typename T> 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.
|
||||
*/
|
||||
virtual bool set_new_state(const optional<T> &new_state) {
|
||||
// Access flags_ directly to avoid virtual/function call overhead in this hot path
|
||||
bool set_new_state(const optional<T> &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()) {
|
||||
if (had_state && this->get_state() == new_state.value())
|
||||
@@ -358,10 +358,14 @@ template<typename T> class StatefulEntityBase : public EntityBase {
|
||||
if (new_state.has_value()) {
|
||||
this->set_state_value_(new_state.value());
|
||||
}
|
||||
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<T> &old_state, const optional<T> &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());
|
||||
return true;
|
||||
}
|
||||
/// Subclasses implement this to store the actual value into their own storage.
|
||||
virtual void set_state_value_(const T &value) = 0;
|
||||
|
||||
Reference in New Issue
Block a user