mirror of
				https://github.com/esphome/esphome.git
				synced 2025-11-04 00:51:49 +00:00 
			
		
		
		
	Keep Unit of Measurement in Flash. (#4719)
Co-authored-by: Your Name <you@example.com>
This commit is contained in:
		@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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<std::string> unit_of_measurement_;  ///< Unit of measurement override
 | 
			
		||||
  NumberMode mode_{NUMBER_MODE_AUTO};
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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_;
 | 
			
		||||
 
 | 
			
		||||
@@ -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<std::string> unit_of_measurement_;           ///< Unit of measurement override
 | 
			
		||||
  optional<int8_t> accuracy_decimals_;                  ///< Accuracy in decimals override
 | 
			
		||||
  optional<StateClass> state_class_{STATE_CLASS_NONE};  ///< State class override
 | 
			
		||||
  bool force_update_{false};                            ///< Force update mode
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user