mirror of
https://github.com/esphome/esphome.git
synced 2025-09-16 18:22:22 +01:00
preen
This commit is contained in:
@@ -21,7 +21,6 @@ std::string state_class_to_string(StateClass state_class) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Sensor::Sensor() : state(NAN), raw_state(NAN) {}
|
Sensor::Sensor() : state(NAN), raw_state(NAN) {}
|
||||||
Sensor::~Sensor() { delete this->raw_callback_; }
|
|
||||||
|
|
||||||
int8_t Sensor::get_accuracy_decimals() {
|
int8_t Sensor::get_accuracy_decimals() {
|
||||||
if (this->accuracy_decimals_.has_value())
|
if (this->accuracy_decimals_.has_value())
|
||||||
@@ -39,7 +38,7 @@ StateClass Sensor::get_state_class() {
|
|||||||
|
|
||||||
void Sensor::publish_state(float state) {
|
void Sensor::publish_state(float state) {
|
||||||
this->raw_state = state;
|
this->raw_state = state;
|
||||||
if (this->raw_callback_ != nullptr) {
|
if (this->raw_callback_) {
|
||||||
this->raw_callback_->call(state);
|
this->raw_callback_->call(state);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,8 +53,8 @@ void Sensor::publish_state(float state) {
|
|||||||
|
|
||||||
void Sensor::add_on_state_callback(std::function<void(float)> &&callback) { this->callback_.add(std::move(callback)); }
|
void Sensor::add_on_state_callback(std::function<void(float)> &&callback) { this->callback_.add(std::move(callback)); }
|
||||||
void Sensor::add_on_raw_state_callback(std::function<void(float)> &&callback) {
|
void Sensor::add_on_raw_state_callback(std::function<void(float)> &&callback) {
|
||||||
if (this->raw_callback_ == nullptr) {
|
if (!this->raw_callback_) {
|
||||||
this->raw_callback_ = new CallbackManager<void(float)>(); // NOLINT
|
this->raw_callback_ = std::make_unique<CallbackManager<void(float)>>();
|
||||||
}
|
}
|
||||||
this->raw_callback_->add(std::move(callback));
|
this->raw_callback_->add(std::move(callback));
|
||||||
}
|
}
|
||||||
|
@@ -7,6 +7,7 @@
|
|||||||
#include "esphome/components/sensor/filter.h"
|
#include "esphome/components/sensor/filter.h"
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace sensor {
|
namespace sensor {
|
||||||
@@ -61,7 +62,6 @@ std::string state_class_to_string(StateClass state_class);
|
|||||||
class Sensor : public EntityBase, public EntityBase_DeviceClass, public EntityBase_UnitOfMeasurement {
|
class Sensor : public EntityBase, public EntityBase_DeviceClass, public EntityBase_UnitOfMeasurement {
|
||||||
public:
|
public:
|
||||||
explicit Sensor();
|
explicit Sensor();
|
||||||
~Sensor();
|
|
||||||
|
|
||||||
/// Get the accuracy in decimals, using the manual override if set.
|
/// Get the accuracy in decimals, using the manual override if set.
|
||||||
int8_t get_accuracy_decimals();
|
int8_t get_accuracy_decimals();
|
||||||
@@ -153,8 +153,8 @@ class Sensor : public EntityBase, public EntityBase_DeviceClass, public EntityBa
|
|||||||
void internal_send_state_to_frontend(float state);
|
void internal_send_state_to_frontend(float state);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
CallbackManager<void(float)> *raw_callback_{nullptr}; ///< Storage for raw state callbacks (lazy allocated).
|
std::unique_ptr<CallbackManager<void(float)>> raw_callback_; ///< Storage for raw state callbacks (lazy allocated).
|
||||||
CallbackManager<void(float)> callback_; ///< Storage for filtered state callbacks.
|
CallbackManager<void(float)> callback_; ///< Storage for filtered state callbacks.
|
||||||
|
|
||||||
Filter *filter_list_{nullptr}; ///< Store all active filters.
|
Filter *filter_list_{nullptr}; ///< Store all active filters.
|
||||||
|
|
||||||
|
@@ -6,11 +6,9 @@ namespace text_sensor {
|
|||||||
|
|
||||||
static const char *const TAG = "text_sensor";
|
static const char *const TAG = "text_sensor";
|
||||||
|
|
||||||
TextSensor::~TextSensor() { delete this->raw_callback_; }
|
|
||||||
|
|
||||||
void TextSensor::publish_state(const std::string &state) {
|
void TextSensor::publish_state(const std::string &state) {
|
||||||
this->raw_state = state;
|
this->raw_state = state;
|
||||||
if (this->raw_callback_ != nullptr) {
|
if (this->raw_callback_) {
|
||||||
this->raw_callback_->call(state);
|
this->raw_callback_->call(state);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,8 +55,8 @@ void TextSensor::add_on_state_callback(std::function<void(std::string)> callback
|
|||||||
this->callback_.add(std::move(callback));
|
this->callback_.add(std::move(callback));
|
||||||
}
|
}
|
||||||
void TextSensor::add_on_raw_state_callback(std::function<void(std::string)> callback) {
|
void TextSensor::add_on_raw_state_callback(std::function<void(std::string)> callback) {
|
||||||
if (this->raw_callback_ == nullptr) {
|
if (!this->raw_callback_) {
|
||||||
this->raw_callback_ = new CallbackManager<void(std::string)>(); // NOLINT
|
this->raw_callback_ = std::make_unique<CallbackManager<void(std::string)>>();
|
||||||
}
|
}
|
||||||
this->raw_callback_->add(std::move(callback));
|
this->raw_callback_->add(std::move(callback));
|
||||||
}
|
}
|
||||||
|
@@ -6,6 +6,7 @@
|
|||||||
#include "esphome/components/text_sensor/filter.h"
|
#include "esphome/components/text_sensor/filter.h"
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace text_sensor {
|
namespace text_sensor {
|
||||||
@@ -34,7 +35,6 @@ namespace text_sensor {
|
|||||||
class TextSensor : public EntityBase, public EntityBase_DeviceClass {
|
class TextSensor : public EntityBase, public EntityBase_DeviceClass {
|
||||||
public:
|
public:
|
||||||
TextSensor() = default;
|
TextSensor() = default;
|
||||||
~TextSensor();
|
|
||||||
|
|
||||||
/// Getter-syntax for .state.
|
/// Getter-syntax for .state.
|
||||||
std::string get_state() const;
|
std::string get_state() const;
|
||||||
@@ -75,8 +75,9 @@ class TextSensor : public EntityBase, public EntityBase_DeviceClass {
|
|||||||
void internal_send_state_to_frontend(const std::string &state);
|
void internal_send_state_to_frontend(const std::string &state);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
CallbackManager<void(std::string)> *raw_callback_{nullptr}; ///< Storage for raw state callbacks (lazy allocated).
|
std::unique_ptr<CallbackManager<void(std::string)>>
|
||||||
CallbackManager<void(std::string)> callback_; ///< Storage for filtered state callbacks.
|
raw_callback_; ///< Storage for raw state callbacks (lazy allocated).
|
||||||
|
CallbackManager<void(std::string)> callback_; ///< Storage for filtered state callbacks.
|
||||||
|
|
||||||
Filter *filter_list_{nullptr}; ///< Store all active filters.
|
Filter *filter_list_{nullptr}; ///< Store all active filters.
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user