Optimize memory usage by lazy-allocating raw callbacks in sensors (#9077)
This commit is contained in:
1 parent
4305c44440
commit
cb019fff9a
4 files changed
+23
-8
No files matched your search
@@ -8,7 +8,9 @@ static const char *const TAG = "text_sensor";
|
||||
|
||||
void TextSensor::publish_state(const std::string &state) {
|
||||
this->raw_state = state;
|
||||
this->raw_callback_.call(state);
|
||||
if (this->raw_callback_) {
|
||||
this->raw_callback_->call(state);
|
||||
}
|
||||
|
||||
ESP_LOGV(TAG, "'%s': Received new state %s", this->name_.c_str(), state.c_str());
|
||||
|
||||
@@ -53,7 +55,10 @@ void TextSensor::add_on_state_callback(std::function<void(std::string)> callback
|
||||
this->callback_.add(std::move(callback));
|
||||
}
|
||||
void TextSensor::add_on_raw_state_callback(std::function<void(std::string)> callback) {
|
||||
this->raw_callback_.add(std::move(callback));
|
||||
if (!this->raw_callback_) {
|
||||
this->raw_callback_ = std::make_unique<CallbackManager<void(std::string)>>();
|
||||
}
|
||||
this->raw_callback_->add(std::move(callback));
|
||||
}
|
||||
|
||||
std::string TextSensor::get_state() const { return this->state; }
|
||||
|
||||
Reference in new issue
Block a user