[sensor] Deprecate .raw_state, guard raw_callback_ behind USE_SENSOR_FILTER

Deprecate direct access to sensor .raw_state member in favor of
get_raw_state(). This prepares for eventually compiling out raw_state
on builds without sensor filters, saving 4 bytes per sensor instance.

Guard raw_callback_ and add_on_raw_state_callback behind
USE_SENSOR_FILTER, saving 4 bytes per sensor on builds without
filters immediately.

Guard SensorRawStateTrigger behind USE_SENSOR_FILTER and ensure
on_raw_value automation sets USE_SENSOR_FILTER define.

Fix nextion sensor to stop directly writing .raw_state/.state
members - the publish=false path was redundantly setting already-current
values.

Migrate haier hon_climate from .raw_state to get_raw_state().
This commit is contained in:
J. Nick Koston
2026-03-22 15:36:17 -10:00
parent fbe3e7d99c
commit a3a1a8799a
6 changed files with 27 additions and 14 deletions
+1 -1
View File
@@ -748,7 +748,7 @@ void HonClimate::update_sub_sensor_(SubSensorType type, float value) {
if (type < SubSensorType::SUB_SENSOR_TYPE_COUNT) {
size_t index = (size_t) type;
if ((this->sub_sensors_[index] != nullptr) &&
((!this->sub_sensors_[index]->has_state()) || (this->sub_sensors_[index]->raw_state != value)))
((!this->sub_sensors_[index]->has_state()) || (this->sub_sensors_[index]->get_raw_state() != value)))
this->sub_sensors_[index]->publish_state(value);
}
}
@@ -85,10 +85,6 @@ void NextionSensor::set_state(float state, bool publish, bool send_to_nextion) {
}
this->publish_state(published_state);
} else {
this->raw_state = state;
this->state = state;
this->set_has_state(true);
}
}
this->update_component_settings();
+1
View File
@@ -901,6 +901,7 @@ async def _build_sensor_automations(var, config):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(trigger, [(float, "x")], conf)
for conf in config.get(CONF_ON_RAW_VALUE, []):
cg.add_define("USE_SENSOR_FILTER")
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(trigger, [(float, "x")], conf)
for conf in config.get(CONF_ON_VALUE_RANGE, []):
+2
View File
@@ -13,12 +13,14 @@ class SensorStateTrigger : public Trigger<float> {
}
};
#ifdef USE_SENSOR_FILTER
class SensorRawStateTrigger : public Trigger<float> {
public:
explicit SensorRawStateTrigger(Sensor *parent) {
parent->add_on_raw_state_callback([this](float value) { this->trigger(value); });
}
};
#endif
template<typename... Ts> class SensorPublishAction : public Action<Ts...> {
public:
+8 -2
View File
@@ -4,6 +4,10 @@
#include "esphome/core/log.h"
#include "esphome/core/progmem.h"
// Suppress deprecation warnings for internal access to .state and .raw_state
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
namespace esphome::sensor {
static const char *const TAG = "sensor";
@@ -64,7 +68,9 @@ StateClass Sensor::get_state_class() {
void Sensor::publish_state(float state) {
this->raw_state = state;
#ifdef USE_SENSOR_FILTER
this->raw_callback_.call(state);
#endif
ESP_LOGV(TAG, "'%s': Received new state %f", this->name_.c_str(), state);
@@ -110,8 +116,6 @@ void Sensor::clear_filters() {
this->filter_list_ = nullptr;
}
#endif // USE_SENSOR_FILTER
float Sensor::get_state() const { return this->state; }
float Sensor::get_raw_state() const { return this->raw_state; }
void Sensor::internal_send_state_to_frontend(float state) {
this->set_has_state(true);
@@ -124,4 +128,6 @@ void Sensor::internal_send_state_to_frontend(float state) {
#endif
}
#pragma GCC diagnostic pop
} // namespace esphome::sensor
+15 -7
View File
@@ -95,9 +95,12 @@ class Sensor : public EntityBase {
#endif
/// Getter-syntax for .state.
float get_state() const;
float get_state() const { return this->state; }
/// Getter-syntax for .raw_state
float get_raw_state() const;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
float get_raw_state() const { return this->raw_state; }
#pragma GCC diagnostic pop
/** Publish a new state to the front-end.
*
@@ -112,10 +115,12 @@ class Sensor : public EntityBase {
// (In most use cases you won't need these)
/// Add a callback that will be called every time a filtered value arrives.
template<typename F> void add_on_state_callback(F &&callback) { this->callback_.add(std::forward<F>(callback)); }
#ifdef USE_SENSOR_FILTER
/// Add a callback that will be called every time the sensor sends a raw value.
template<typename F> void add_on_raw_state_callback(F &&callback) {
this->raw_callback_.add(std::forward<F>(callback));
}
#endif
/** This member variable stores the last state that has passed through all filters.
*
@@ -126,17 +131,20 @@ class Sensor : public EntityBase {
*/
float state;
/** This member variable stores the current raw state of the sensor, without any filters applied.
*
* Unlike .state,this will be updated immediately when publish_state is called.
*/
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
/// @deprecated Use get_raw_state() instead. This member will be removed in ESPHome 2027.1.0.
ESPDEPRECATED("Use get_raw_state() instead of .raw_state. Will be removed in 2027.1.0", "2026.7.0")
float raw_state;
#pragma GCC diagnostic pop
void internal_send_state_to_frontend(float state);
protected:
#ifdef USE_SENSOR_FILTER
LazyCallbackManager<void(float)> raw_callback_; ///< Storage for raw state callbacks.
LazyCallbackManager<void(float)> callback_; ///< Storage for filtered state callbacks.
#endif
LazyCallbackManager<void(float)> callback_; ///< Storage for filtered state callbacks.
#ifdef USE_SENSOR_FILTER
Filter *filter_list_{nullptr}; ///< Store all active filters.