[text_sensor] Add deprecation warning for raw_state member access (#12246)

This commit is contained in:
J. Nick Koston
2025-12-02 10:03:58 -06:00
committed by GitHub
parent 77477bd330
commit 6ce2a45691
2 changed files with 17 additions and 12 deletions

View File

@@ -25,11 +25,11 @@ void log_text_sensor(const char *tag, const char *prefix, const char *type, Text
}
void TextSensor::publish_state(const std::string &state) {
// Only store raw_state_ separately when filters exist
// When no filters, raw_state == state, so we avoid the duplicate storage
if (this->filter_list_ != nullptr) {
this->raw_state_ = state;
}
// Suppress deprecation warning - we need to populate raw_state for backwards compatibility
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
this->raw_state = state;
#pragma GCC diagnostic pop
if (this->raw_callback_) {
this->raw_callback_->call(state);
}
@@ -85,9 +85,11 @@ void TextSensor::add_on_raw_state_callback(std::function<void(std::string)> call
std::string TextSensor::get_state() const { return this->state; }
std::string TextSensor::get_raw_state() const {
// When no filters exist, raw_state == state, so return state to avoid
// requiring separate storage
return this->filter_list_ != nullptr ? this->raw_state_ : this->state;
// Suppress deprecation warning - get_raw_state() is the replacement API
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
return this->raw_state;
#pragma GCC diagnostic pop
}
void TextSensor::internal_send_state_to_frontend(const std::string &state) {
this->state = state;

View File

@@ -51,6 +51,13 @@ class TextSensor : public EntityBase, public EntityBase_DeviceClass {
std::string state;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
/// @deprecated Use get_raw_state() instead. This member will be removed in ESPHome 2026.6.0.
ESPDEPRECATED("Use get_raw_state() instead of .raw_state. Will be removed in 2026.6.0", "2025.12.0")
std::string raw_state;
#pragma GCC diagnostic pop
// ========== INTERNAL METHODS ==========
// (In most use cases you won't need these)
@@ -62,10 +69,6 @@ class TextSensor : public EntityBase, public EntityBase_DeviceClass {
CallbackManager<void(std::string)> callback_; ///< Storage for filtered state callbacks.
Filter *filter_list_{nullptr}; ///< Store all active filters.
/// Raw state (before filters). Only populated when filters are configured.
/// When no filters exist, get_raw_state() returns state directly.
std::string raw_state_;
};
} // namespace text_sensor