Keep Device Class in Flash. (#4639)

* Keep Device Class in Flash.

* Remove blank line

---------

Co-authored-by: Your Name <you@example.com>
This commit is contained in:
Fabian 2023-04-20 05:53:35 +02:00 committed by GitHub
parent 0f7e34e7ec
commit afc2b3b74f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 31 additions and 76 deletions

View file

@ -43,12 +43,7 @@ void BinarySensor::send_state_internal(bool state, bool is_initial) {
} }
BinarySensor::BinarySensor() : state(false) {} BinarySensor::BinarySensor() : state(false) {}
void BinarySensor::set_device_class(const std::string &device_class) { this->device_class_ = device_class; }
std::string BinarySensor::get_device_class() {
if (this->device_class_.has_value())
return *this->device_class_;
return "";
}
void BinarySensor::add_filter(Filter *filter) { void BinarySensor::add_filter(Filter *filter) {
filter->parent_ = this; filter->parent_ = this;
if (this->filter_list_ == nullptr) { if (this->filter_list_ == nullptr) {

View file

@ -34,7 +34,7 @@ namespace binary_sensor {
* The sub classes should notify the front-end of new states via the publish_state() method which * The sub classes should notify the front-end of new states via the publish_state() method which
* handles inverted inputs for you. * handles inverted inputs for you.
*/ */
class BinarySensor : public EntityBase { class BinarySensor : public EntityBase, public EntityBase_DeviceClass {
public: public:
explicit BinarySensor(); explicit BinarySensor();
@ -60,12 +60,6 @@ class BinarySensor : public EntityBase {
/// The current reported state of the binary sensor. /// The current reported state of the binary sensor.
bool state; bool state;
/// Manually set the Home Assistant device class (see binary_sensor::device_class)
void set_device_class(const std::string &device_class);
/// Get the device class for this binary sensor, using the manual override if specified.
std::string get_device_class();
void add_filter(Filter *filter); void add_filter(Filter *filter);
void add_filters(const std::vector<Filter *> &filters); void add_filters(const std::vector<Filter *> &filters);
@ -82,7 +76,6 @@ class BinarySensor : public EntityBase {
protected: protected:
CallbackManager<void(bool)> state_callback_{}; CallbackManager<void(bool)> state_callback_{};
optional<std::string> device_class_{}; ///< Stores the override of the device class
Filter *filter_list_{nullptr}; Filter *filter_list_{nullptr};
bool has_state_{false}; bool has_state_{false};
bool publish_initial_state_{false}; bool publish_initial_state_{false};

View file

@ -13,8 +13,5 @@ void Button::press() {
} }
void Button::add_on_press_callback(std::function<void()> &&callback) { this->press_callback_.add(std::move(callback)); } void Button::add_on_press_callback(std::function<void()> &&callback) { this->press_callback_.add(std::move(callback)); }
void Button::set_device_class(const std::string &device_class) { this->device_class_ = device_class; }
std::string Button::get_device_class() { return this->device_class_; }
} // namespace button } // namespace button
} // namespace esphome } // namespace esphome

View file

@ -26,7 +26,7 @@ namespace button {
* *
* A button is just a momentary switch that does not have a state, only a trigger. * A button is just a momentary switch that does not have a state, only a trigger.
*/ */
class Button : public EntityBase { class Button : public EntityBase, public EntityBase_DeviceClass {
public: public:
/** Press this button. This is called by the front-end. /** Press this button. This is called by the front-end.
* *
@ -40,19 +40,12 @@ class Button : public EntityBase {
*/ */
void add_on_press_callback(std::function<void()> &&callback); void add_on_press_callback(std::function<void()> &&callback);
/// Set the Home Assistant device class (see button::device_class).
void set_device_class(const std::string &device_class);
/// Get the device class for this button.
std::string get_device_class();
protected: protected:
/** You should implement this virtual method if you want to create your own button. /** You should implement this virtual method if you want to create your own button.
*/ */
virtual void press_action() = 0; virtual void press_action() = 0;
CallbackManager<void()> press_callback_{}; CallbackManager<void()> press_callback_{};
std::string device_class_{};
}; };
} // namespace button } // namespace button

View file

@ -145,7 +145,7 @@ CoverCall &CoverCall::set_stop(bool stop) {
return *this; return *this;
} }
bool CoverCall::get_stop() const { return this->stop_; } bool CoverCall::get_stop() const { return this->stop_; }
void Cover::set_device_class(const std::string &device_class) { this->device_class_override_ = device_class; }
CoverCall Cover::make_call() { return {this}; } CoverCall Cover::make_call() { return {this}; }
void Cover::open() { void Cover::open() {
auto call = this->make_call(); auto call = this->make_call();
@ -204,11 +204,7 @@ optional<CoverRestoreState> Cover::restore_state_() {
return {}; return {};
return recovered; return recovered;
} }
std::string Cover::get_device_class() {
if (this->device_class_override_.has_value())
return *this->device_class_override_;
return "";
}
bool Cover::is_fully_open() const { return this->position == COVER_OPEN; } bool Cover::is_fully_open() const { return this->position == COVER_OPEN; }
bool Cover::is_fully_closed() const { return this->position == COVER_CLOSED; } bool Cover::is_fully_closed() const { return this->position == COVER_CLOSED; }

View file

@ -108,7 +108,7 @@ const char *cover_operation_to_str(CoverOperation op);
* to control all values of the cover. Also implement get_traits() to return what operations * to control all values of the cover. Also implement get_traits() to return what operations
* the cover supports. * the cover supports.
*/ */
class Cover : public EntityBase { class Cover : public EntityBase, public EntityBase_DeviceClass {
public: public:
explicit Cover(); explicit Cover();
@ -156,8 +156,6 @@ class Cover : public EntityBase {
void publish_state(bool save = true); void publish_state(bool save = true);
virtual CoverTraits get_traits() = 0; virtual CoverTraits get_traits() = 0;
void set_device_class(const std::string &device_class);
std::string get_device_class();
/// Helper method to check if the cover is fully open. Equivalent to comparing .position against 1.0 /// Helper method to check if the cover is fully open. Equivalent to comparing .position against 1.0
bool is_fully_open() const; bool is_fully_open() const;
@ -172,7 +170,6 @@ class Cover : public EntityBase {
optional<CoverRestoreState> restore_state_(); optional<CoverRestoreState> restore_state_();
CallbackManager<void()> state_callback_{}; CallbackManager<void()> state_callback_{};
optional<std::string> device_class_override_{};
ESPPreferenceObject rtc_; ESPPreferenceObject rtc_;
}; };

View file

@ -16,13 +16,5 @@ std::string NumberTraits::get_unit_of_measurement() {
return ""; return "";
} }
void NumberTraits::set_device_class(const std::string &device_class) { this->device_class_ = device_class; }
std::string NumberTraits::get_device_class() {
if (this->device_class_.has_value())
return *this->device_class_;
return "";
}
} // namespace number } // namespace number
} // namespace esphome } // namespace esphome

View file

@ -1,5 +1,6 @@
#pragma once #pragma once
#include "esphome/core/entity_base.h"
#include "esphome/core/helpers.h" #include "esphome/core/helpers.h"
namespace esphome { namespace esphome {
@ -11,7 +12,7 @@ enum NumberMode : uint8_t {
NUMBER_MODE_SLIDER = 2, NUMBER_MODE_SLIDER = 2,
}; };
class NumberTraits { class NumberTraits : public EntityBase_DeviceClass {
public: public:
// Set/get the number value boundaries. // Set/get the number value boundaries.
void set_min_value(float min_value) { min_value_ = min_value; } void set_min_value(float min_value) { min_value_ = min_value; }
@ -32,17 +33,12 @@ class NumberTraits {
void set_mode(NumberMode mode) { this->mode_ = mode; } void set_mode(NumberMode mode) { this->mode_ = mode; }
NumberMode get_mode() const { return this->mode_; } NumberMode get_mode() const { return this->mode_; }
// Set/get the device class.
void set_device_class(const std::string &device_class);
std::string get_device_class();
protected: protected:
float min_value_ = NAN; float min_value_ = NAN;
float max_value_ = NAN; float max_value_ = NAN;
float step_ = NAN; float step_ = NAN;
optional<std::string> unit_of_measurement_; ///< Unit of measurement override optional<std::string> unit_of_measurement_; ///< Unit of measurement override
NumberMode mode_{NUMBER_MODE_AUTO}; NumberMode mode_{NUMBER_MODE_AUTO};
optional<std::string> device_class_;
}; };
} // namespace number } // namespace number

View file

@ -38,13 +38,6 @@ int8_t Sensor::get_accuracy_decimals() {
} }
void Sensor::set_accuracy_decimals(int8_t accuracy_decimals) { this->accuracy_decimals_ = accuracy_decimals; } void Sensor::set_accuracy_decimals(int8_t accuracy_decimals) { this->accuracy_decimals_ = accuracy_decimals; }
std::string Sensor::get_device_class() {
if (this->device_class_.has_value())
return *this->device_class_;
return "";
}
void Sensor::set_device_class(const std::string &device_class) { this->device_class_ = device_class; }
void Sensor::set_state_class(StateClass state_class) { this->state_class_ = state_class; } void Sensor::set_state_class(StateClass state_class) { this->state_class_ = state_class; }
StateClass Sensor::get_state_class() { StateClass Sensor::get_state_class() {
if (this->state_class_.has_value()) if (this->state_class_.has_value())

View file

@ -54,7 +54,7 @@ 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. * 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 { class Sensor : public EntityBase, public EntityBase_DeviceClass {
public: public:
explicit Sensor(); explicit Sensor();
@ -68,11 +68,6 @@ class Sensor : public EntityBase {
/// Manually set the accuracy in decimals. /// Manually set the accuracy in decimals.
void set_accuracy_decimals(int8_t accuracy_decimals); void set_accuracy_decimals(int8_t accuracy_decimals);
/// Get the device class, using the manual override if set.
std::string get_device_class();
/// Manually set the device class.
void set_device_class(const std::string &device_class);
/// Get the state class, using the manual override if set. /// Get the state class, using the manual override if set.
StateClass get_state_class(); StateClass get_state_class();
/// Manually set the state class. /// Manually set the state class.
@ -165,7 +160,6 @@ class Sensor : public EntityBase {
optional<std::string> unit_of_measurement_; ///< Unit of measurement override optional<std::string> unit_of_measurement_; ///< Unit of measurement override
optional<int8_t> accuracy_decimals_; ///< Accuracy in decimals override optional<int8_t> accuracy_decimals_; ///< Accuracy in decimals override
optional<std::string> device_class_; ///< Device class override
optional<StateClass> state_class_{STATE_CLASS_NONE}; ///< State class override optional<StateClass> state_class_{STATE_CLASS_NONE}; ///< State class override
bool force_update_{false}; ///< Force update mode bool force_update_{false}; ///< Force update mode
bool has_state_{false}; bool has_state_{false};

View file

@ -63,13 +63,6 @@ void Switch::add_on_state_callback(std::function<void(bool)> &&callback) {
void Switch::set_inverted(bool inverted) { this->inverted_ = inverted; } void Switch::set_inverted(bool inverted) { this->inverted_ = inverted; }
bool Switch::is_inverted() const { return this->inverted_; } bool Switch::is_inverted() const { return this->inverted_; }
std::string Switch::get_device_class() {
if (this->device_class_.has_value())
return *this->device_class_;
return "";
}
void Switch::set_device_class(const std::string &device_class) { this->device_class_ = device_class; }
void log_switch(const char *tag, const char *prefix, const char *type, Switch *obj) { void log_switch(const char *tag, const char *prefix, const char *type, Switch *obj) {
if (obj != nullptr) { if (obj != nullptr) {
ESP_LOGCONFIG(tag, "%s%s '%s'", prefix, type, obj->get_name().c_str()); ESP_LOGCONFIG(tag, "%s%s '%s'", prefix, type, obj->get_name().c_str());

View file

@ -29,7 +29,7 @@ enum SwitchRestoreMode {
* A switch is basically just a combination of a binary sensor (for reporting switch values) * A switch is basically just a combination of a binary sensor (for reporting switch values)
* and a write_state method that writes a state to the hardware. * and a write_state method that writes a state to the hardware.
*/ */
class Switch : public EntityBase { class Switch : public EntityBase, public EntityBase_DeviceClass {
public: public:
explicit Switch(); explicit Switch();
@ -103,10 +103,6 @@ class Switch : public EntityBase {
bool is_inverted() const; bool is_inverted() const;
/// Get the device class for this switch.
std::string get_device_class();
/// Set the Home Assistant device class for this switch.
void set_device_class(const std::string &device_class);
void set_restore_mode(SwitchRestoreMode restore_mode) { this->restore_mode = restore_mode; } void set_restore_mode(SwitchRestoreMode restore_mode) { this->restore_mode = restore_mode; }
protected: protected:
@ -124,7 +120,6 @@ class Switch : public EntityBase {
bool inverted_{false}; bool inverted_{false};
Deduplicator<bool> publish_dedup_; Deduplicator<bool> publish_dedup_;
ESPPreferenceObject rtc_; ESPPreferenceObject rtc_;
optional<std::string> device_class_;
}; };
#define LOG_SWITCH(prefix, type, obj) log_switch((TAG), (prefix), LOG_STR_LITERAL(type), (obj)) #define LOG_SWITCH(prefix, type, obj) log_switch((TAG), (prefix), LOG_STR_LITERAL(type), (obj))

View file

@ -72,6 +72,16 @@ void EntityBase::calc_object_id_() {
this->object_id_hash_ = fnv1_hash(this->object_id_c_str_); this->object_id_hash_ = fnv1_hash(this->object_id_c_str_);
} }
} }
uint32_t EntityBase::get_object_id_hash() { return this->object_id_hash_; } uint32_t EntityBase::get_object_id_hash() { return this->object_id_hash_; }
std::string EntityBase_DeviceClass::get_device_class() {
if (this->device_class_ == nullptr) {
return "";
}
return this->device_class_;
}
void EntityBase_DeviceClass::set_device_class(const char *device_class) { this->device_class_ = device_class; }
} // namespace esphome } // namespace esphome

View file

@ -63,4 +63,15 @@ class EntityBase {
EntityCategory entity_category_{ENTITY_CATEGORY_NONE}; EntityCategory entity_category_{ENTITY_CATEGORY_NONE};
}; };
class EntityBase_DeviceClass {
public:
/// Get the device class, using the manual override if set.
std::string get_device_class();
/// Manually set the device class.
void set_device_class(const char *device_class);
protected:
const char *device_class_{nullptr}; ///< Device class override
};
} // namespace esphome } // namespace esphome