[sensor] Only store raw state when filters are compiled in

This commit is contained in:
J. Nick Koston
2026-09-10 16:04:57 -05:00
parent b3f0382d70
commit a952e7236b
2 changed files with 10 additions and 8 deletions
+2 -5
View File
@@ -40,10 +40,7 @@ const LogString *state_class_to_string(StateClass state_class) {
return StateClassStrings::get_log_str(static_cast<uint8_t>(state_class), 0);
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
Sensor::Sensor() : state(NAN), raw_state_(NAN) {}
#pragma GCC diagnostic pop
Sensor::Sensor() : state(NAN) {}
int8_t Sensor::get_accuracy_decimals() {
if (this->sensor_flags_.has_accuracy_override)
@@ -66,8 +63,8 @@ StateClass Sensor::get_state_class() {
}
void Sensor::publish_state(float state) {
this->raw_state_ = state;
#ifdef USE_SENSOR_FILTER
this->raw_state_ = state;
this->raw_callback_.call(state);
#endif
+8 -3
View File
@@ -97,7 +97,13 @@ class Sensor : public EntityBase {
/// Getter-syntax for .state.
float get_state() const { return this->state; }
/// Get the last state received by publish_state(), before any filters were applied.
float get_raw_state() const { return this->raw_state_; }
float get_raw_state() const {
#ifdef USE_SENSOR_FILTER
return this->raw_state_;
#else
return this->state; // No filters compiled in, raw == filtered
#endif
}
/** Publish a new state to the front-end.
*
@@ -135,9 +141,8 @@ class Sensor : public EntityBase {
void internal_send_state_to_frontend(float state);
protected:
float raw_state_; ///< The last state passed to publish_state(), before filters.
#ifdef USE_SENSOR_FILTER
float raw_state_{NAN}; ///< The last state passed to publish_state(), before filters.
LazyCallbackManager<void(float)> raw_callback_; ///< Storage for raw state callbacks.
#endif
LazyCallbackManager<void(float)> callback_; ///< Storage for filtered state callbacks.