[text_sensor] Avoid duplicate string storage when no filters configured (#12205)
This commit is contained in:
1 parent
9a0731437a
commit
10ddebc737
4 files changed
+182
-3
No files matched your search
@@ -25,7 +25,11 @@ void log_text_sensor(const char *tag, const char *prefix, const char *type, Text
|
||||
}
|
||||
|
||||
void TextSensor::publish_state(const std::string &state) {
|
||||
this->raw_state = 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;
|
||||
}
|
||||
if (this->raw_callback_) {
|
||||
this->raw_callback_->call(state);
|
||||
}
|
||||
@@ -80,7 +84,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 { return this->raw_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;
|
||||
}
|
||||
void TextSensor::internal_send_state_to_frontend(const std::string &state) {
|
||||
this->state = state;
|
||||
this->set_has_state(true);
|
||||
|
||||
Reference in new issue
Block a user