diff --git a/esphome/components/number/number_traits.cpp b/esphome/components/number/number_traits.cpp index dcd05daa2a..89035661f5 100644 --- a/esphome/components/number/number_traits.cpp +++ b/esphome/components/number/number_traits.cpp @@ -6,15 +6,5 @@ namespace number { static const char *const TAG = "number"; -void NumberTraits::set_unit_of_measurement(const std::string &unit_of_measurement) { - this->unit_of_measurement_ = unit_of_measurement; -} - -std::string NumberTraits::get_unit_of_measurement() { - if (this->unit_of_measurement_.has_value()) - return *this->unit_of_measurement_; - return ""; -} - } // namespace number } // namespace esphome diff --git a/esphome/components/number/number_traits.h b/esphome/components/number/number_traits.h index 5b14b77718..fa68c2390a 100644 --- a/esphome/components/number/number_traits.h +++ b/esphome/components/number/number_traits.h @@ -12,7 +12,7 @@ enum NumberMode : uint8_t { NUMBER_MODE_SLIDER = 2, }; -class NumberTraits : public EntityBase_DeviceClass { +class NumberTraits : public EntityBase_DeviceClass, public EntityBase_UnitOfMeasurement { public: // Set/get the number value boundaries. void set_min_value(float min_value) { min_value_ = min_value; } @@ -24,11 +24,6 @@ class NumberTraits : public EntityBase_DeviceClass { void set_step(float step) { step_ = step; } float get_step() const { return step_; } - /// Manually set the unit of measurement. - void set_unit_of_measurement(const std::string &unit_of_measurement); - /// Get the unit of measurement, using the manual override if set. - std::string get_unit_of_measurement(); - // Set/get the frontend mode. void set_mode(NumberMode mode) { this->mode_ = mode; } NumberMode get_mode() const { return this->mode_; } @@ -37,7 +32,6 @@ class NumberTraits : public EntityBase_DeviceClass { float min_value_ = NAN; float max_value_ = NAN; float step_ = NAN; - optional unit_of_measurement_; ///< Unit of measurement override NumberMode mode_{NUMBER_MODE_AUTO}; }; diff --git a/esphome/components/sensor/sensor.cpp b/esphome/components/sensor/sensor.cpp index 6ce1e193f5..14a8b3d490 100644 --- a/esphome/components/sensor/sensor.cpp +++ b/esphome/components/sensor/sensor.cpp @@ -22,15 +22,6 @@ std::string state_class_to_string(StateClass state_class) { Sensor::Sensor() : state(NAN), raw_state(NAN) {} -std::string Sensor::get_unit_of_measurement() { - if (this->unit_of_measurement_.has_value()) - return *this->unit_of_measurement_; - return ""; -} -void Sensor::set_unit_of_measurement(const std::string &unit_of_measurement) { - this->unit_of_measurement_ = unit_of_measurement; -} - int8_t Sensor::get_accuracy_decimals() { if (this->accuracy_decimals_.has_value()) return *this->accuracy_decimals_; diff --git a/esphome/components/sensor/sensor.h b/esphome/components/sensor/sensor.h index 165d013b2a..98356c943d 100644 --- a/esphome/components/sensor/sensor.h +++ b/esphome/components/sensor/sensor.h @@ -54,15 +54,10 @@ std::string state_class_to_string(StateClass state_class); * * A sensor has unit of measurement and can use publish_state to send out a new value with the specified accuracy. */ -class Sensor : public EntityBase, public EntityBase_DeviceClass { +class Sensor : public EntityBase, public EntityBase_DeviceClass, public EntityBase_UnitOfMeasurement { public: explicit Sensor(); - /// Get the unit of measurement, using the manual override if set. - std::string get_unit_of_measurement(); - /// Manually set the unit of measurement. - void set_unit_of_measurement(const std::string &unit_of_measurement); - /// Get the accuracy in decimals, using the manual override if set. int8_t get_accuracy_decimals(); /// Manually set the accuracy in decimals. @@ -158,7 +153,6 @@ class Sensor : public EntityBase, public EntityBase_DeviceClass { Filter *filter_list_{nullptr}; ///< Store all active filters. - optional unit_of_measurement_; ///< Unit of measurement override optional accuracy_decimals_; ///< Accuracy in decimals override optional state_class_{STATE_CLASS_NONE}; ///< State class override bool force_update_{false}; ///< Force update mode diff --git a/esphome/core/entity_base.cpp b/esphome/core/entity_base.cpp index 1e2ccc35b5..725a8569a3 100644 --- a/esphome/core/entity_base.cpp +++ b/esphome/core/entity_base.cpp @@ -84,4 +84,13 @@ std::string EntityBase_DeviceClass::get_device_class() { void EntityBase_DeviceClass::set_device_class(const char *device_class) { this->device_class_ = device_class; } +std::string EntityBase_UnitOfMeasurement::get_unit_of_measurement() { + if (this->unit_of_measurement_ == nullptr) + return ""; + return this->unit_of_measurement_; +} +void EntityBase_UnitOfMeasurement::set_unit_of_measurement(const char *unit_of_measurement) { + this->unit_of_measurement_ = unit_of_measurement; +} + } // namespace esphome diff --git a/esphome/core/entity_base.h b/esphome/core/entity_base.h index d717674450..434111de79 100644 --- a/esphome/core/entity_base.h +++ b/esphome/core/entity_base.h @@ -74,4 +74,15 @@ class EntityBase_DeviceClass { const char *device_class_{nullptr}; ///< Device class override }; +class EntityBase_UnitOfMeasurement { + public: + /// Get the unit of measurement, using the manual override if set. + std::string get_unit_of_measurement(); + /// Manually set the unit of measurement. + void set_unit_of_measurement(const char *unit_of_measurement); + + protected: + const char *unit_of_measurement_{nullptr}; ///< Unit of measurement override +}; + } // namespace esphome