mirror of
https://github.com/esphome/esphome.git
synced 2025-11-17 07:15:48 +00:00
[core] Deduplicate entity icon and device class logging
This commit is contained in:
@@ -9,14 +9,9 @@ static const char *const TAG = "binary_sensor";
|
||||
|
||||
// Function implementation of LOG_BINARY_SENSOR macro to reduce code size
|
||||
void log_binary_sensor(const char *tag, const char *prefix, const char *type, BinarySensor *obj) {
|
||||
if (obj == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
ESP_LOGCONFIG(tag, "%s%s '%s'", prefix, type, obj->get_name().c_str());
|
||||
|
||||
if (!obj->get_device_class_ref().empty()) {
|
||||
ESP_LOGCONFIG(tag, "%s Device Class: '%s'", prefix, obj->get_device_class_ref().c_str());
|
||||
if (obj != nullptr) {
|
||||
ESP_LOGCONFIG(tag, "%s%s '%s'", prefix, type, obj->get_name().c_str());
|
||||
obj->log_device_class(tag, prefix);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,14 +8,9 @@ static const char *const TAG = "button";
|
||||
|
||||
// Function implementation of LOG_BUTTON macro to reduce code size
|
||||
void log_button(const char *tag, const char *prefix, const char *type, Button *obj) {
|
||||
if (obj == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
ESP_LOGCONFIG(tag, "%s%s '%s'", prefix, type, obj->get_name().c_str());
|
||||
|
||||
if (!obj->get_icon_ref().empty()) {
|
||||
ESP_LOGCONFIG(tag, "%s Icon: '%s'", prefix, obj->get_icon_ref().c_str());
|
||||
if (obj != nullptr) {
|
||||
ESP_LOGCONFIG(tag, "%s%s '%s'", prefix, type, obj->get_name().c_str());
|
||||
obj->log_icon(tag, prefix);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,9 +20,7 @@ const extern float COVER_CLOSED;
|
||||
if (traits_.get_is_assumed_state()) { \
|
||||
ESP_LOGCONFIG(TAG, "%s Assumed State: YES", prefix); \
|
||||
} \
|
||||
if (!(obj)->get_device_class_ref().empty()) { \
|
||||
ESP_LOGCONFIG(TAG, "%s Device Class: '%s'", prefix, (obj)->get_device_class_ref().c_str()); \
|
||||
} \
|
||||
(obj)->log_device_class(TAG, prefix); \
|
||||
}
|
||||
|
||||
class Cover;
|
||||
|
||||
@@ -16,9 +16,7 @@ namespace datetime {
|
||||
#define LOG_DATETIME_DATE(prefix, type, obj) \
|
||||
if ((obj) != nullptr) { \
|
||||
ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
|
||||
if (!(obj)->get_icon_ref().empty()) { \
|
||||
ESP_LOGCONFIG(TAG, "%s Icon: '%s'", prefix, (obj)->get_icon_ref().c_str()); \
|
||||
} \
|
||||
(obj)->log_icon(TAG, prefix); \
|
||||
}
|
||||
|
||||
class DateCall;
|
||||
|
||||
@@ -16,9 +16,7 @@ namespace datetime {
|
||||
#define LOG_DATETIME_DATETIME(prefix, type, obj) \
|
||||
if ((obj) != nullptr) { \
|
||||
ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
|
||||
if (!(obj)->get_icon_ref().empty()) { \
|
||||
ESP_LOGCONFIG(TAG, "%s Icon: '%s'", prefix, (obj)->get_icon_ref().c_str()); \
|
||||
} \
|
||||
(obj)->log_icon(TAG, prefix); \
|
||||
}
|
||||
|
||||
class DateTimeCall;
|
||||
|
||||
@@ -16,9 +16,7 @@ namespace datetime {
|
||||
#define LOG_DATETIME_TIME(prefix, type, obj) \
|
||||
if ((obj) != nullptr) { \
|
||||
ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
|
||||
if (!(obj)->get_icon_ref().empty()) { \
|
||||
ESP_LOGCONFIG(TAG, "%s Icon: '%s'", prefix, (obj)->get_icon_ref().c_str()); \
|
||||
} \
|
||||
(obj)->log_icon(TAG, prefix); \
|
||||
}
|
||||
|
||||
class TimeCall;
|
||||
|
||||
@@ -12,12 +12,8 @@ namespace event {
|
||||
#define LOG_EVENT(prefix, type, obj) \
|
||||
if ((obj) != nullptr) { \
|
||||
ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
|
||||
if (!(obj)->get_icon_ref().empty()) { \
|
||||
ESP_LOGCONFIG(TAG, "%s Icon: '%s'", prefix, (obj)->get_icon_ref().c_str()); \
|
||||
} \
|
||||
if (!(obj)->get_device_class_ref().empty()) { \
|
||||
ESP_LOGCONFIG(TAG, "%s Device Class: '%s'", prefix, (obj)->get_device_class_ref().c_str()); \
|
||||
} \
|
||||
(obj)->log_icon(TAG, prefix); \
|
||||
(obj)->log_device_class(TAG, prefix); \
|
||||
}
|
||||
|
||||
class Event : public EntityBase, public EntityBase_DeviceClass {
|
||||
|
||||
@@ -15,9 +15,7 @@ class Lock;
|
||||
#define LOG_LOCK(prefix, type, obj) \
|
||||
if ((obj) != nullptr) { \
|
||||
ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
|
||||
if (!(obj)->get_icon_ref().empty()) { \
|
||||
ESP_LOGCONFIG(TAG, "%s Icon: '%s'", prefix, (obj)->get_icon_ref().c_str()); \
|
||||
} \
|
||||
(obj)->log_icon(TAG, prefix); \
|
||||
if ((obj)->traits.get_assumed_state()) { \
|
||||
ESP_LOGCONFIG(TAG, "%s Assumed State: YES", prefix); \
|
||||
} \
|
||||
|
||||
@@ -8,22 +8,15 @@ static const char *const TAG = "number";
|
||||
|
||||
// Function implementation of LOG_NUMBER macro to reduce code size
|
||||
void log_number(const char *tag, const char *prefix, const char *type, Number *obj) {
|
||||
if (obj == nullptr) {
|
||||
return;
|
||||
}
|
||||
if (obj != nullptr) {
|
||||
ESP_LOGCONFIG(tag, "%s%s '%s'", prefix, type, obj->get_name().c_str());
|
||||
obj->log_icon(tag, prefix);
|
||||
|
||||
ESP_LOGCONFIG(tag, "%s%s '%s'", prefix, type, obj->get_name().c_str());
|
||||
if (!obj->traits.get_unit_of_measurement_ref().empty()) {
|
||||
ESP_LOGCONFIG(tag, "%s Unit of Measurement: '%s'", prefix, obj->traits.get_unit_of_measurement_ref().c_str());
|
||||
}
|
||||
|
||||
if (!obj->get_icon_ref().empty()) {
|
||||
ESP_LOGCONFIG(tag, "%s Icon: '%s'", prefix, obj->get_icon_ref().c_str());
|
||||
}
|
||||
|
||||
if (!obj->traits.get_unit_of_measurement_ref().empty()) {
|
||||
ESP_LOGCONFIG(tag, "%s Unit of Measurement: '%s'", prefix, obj->traits.get_unit_of_measurement_ref().c_str());
|
||||
}
|
||||
|
||||
if (!obj->traits.get_device_class_ref().empty()) {
|
||||
ESP_LOGCONFIG(tag, "%s Device Class: '%s'", prefix, obj->traits.get_device_class_ref().c_str());
|
||||
obj->traits.log_device_class(tag, prefix);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,9 +12,7 @@ namespace select {
|
||||
#define LOG_SELECT(prefix, type, obj) \
|
||||
if ((obj) != nullptr) { \
|
||||
ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
|
||||
if (!(obj)->get_icon_ref().empty()) { \
|
||||
ESP_LOGCONFIG(TAG, "%s Icon: '%s'", prefix, (obj)->get_icon_ref().c_str()); \
|
||||
} \
|
||||
(obj)->log_icon(TAG, prefix); \
|
||||
}
|
||||
|
||||
#define SUB_SELECT(name) \
|
||||
|
||||
@@ -8,29 +8,22 @@ static const char *const TAG = "sensor";
|
||||
|
||||
// Function implementation of LOG_SENSOR macro to reduce code size
|
||||
void log_sensor(const char *tag, const char *prefix, const char *type, Sensor *obj) {
|
||||
if (obj == nullptr) {
|
||||
return;
|
||||
}
|
||||
if (obj != nullptr) {
|
||||
ESP_LOGCONFIG(tag,
|
||||
"%s%s '%s'\n"
|
||||
"%s State Class: '%s'\n"
|
||||
"%s Unit of Measurement: '%s'\n"
|
||||
"%s Accuracy Decimals: %d",
|
||||
prefix, type, obj->get_name().c_str(), prefix,
|
||||
LOG_STR_ARG(state_class_to_string(obj->get_state_class())), prefix,
|
||||
obj->get_unit_of_measurement_ref().c_str(), prefix, obj->get_accuracy_decimals());
|
||||
|
||||
ESP_LOGCONFIG(tag,
|
||||
"%s%s '%s'\n"
|
||||
"%s State Class: '%s'\n"
|
||||
"%s Unit of Measurement: '%s'\n"
|
||||
"%s Accuracy Decimals: %d",
|
||||
prefix, type, obj->get_name().c_str(), prefix,
|
||||
LOG_STR_ARG(state_class_to_string(obj->get_state_class())), prefix,
|
||||
obj->get_unit_of_measurement_ref().c_str(), prefix, obj->get_accuracy_decimals());
|
||||
obj->log_device_class(tag, prefix);
|
||||
obj->log_icon(tag, prefix);
|
||||
|
||||
if (!obj->get_device_class_ref().empty()) {
|
||||
ESP_LOGCONFIG(tag, "%s Device Class: '%s'", prefix, obj->get_device_class_ref().c_str());
|
||||
}
|
||||
|
||||
if (!obj->get_icon_ref().empty()) {
|
||||
ESP_LOGCONFIG(tag, "%s Icon: '%s'", prefix, obj->get_icon_ref().c_str());
|
||||
}
|
||||
|
||||
if (obj->get_force_update()) {
|
||||
ESP_LOGV(tag, "%s Force Update: YES", prefix);
|
||||
if (obj->get_force_update()) {
|
||||
ESP_LOGV(tag, "%s Force Update: YES", prefix);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -91,18 +91,14 @@ void log_switch(const char *tag, const char *prefix, const char *type, Switch *o
|
||||
LOG_STR_ARG(onoff));
|
||||
|
||||
// Add optional fields separately
|
||||
if (!obj->get_icon_ref().empty()) {
|
||||
ESP_LOGCONFIG(tag, "%s Icon: '%s'", prefix, obj->get_icon_ref().c_str());
|
||||
}
|
||||
obj->log_icon(tag, prefix);
|
||||
if (obj->assumed_state()) {
|
||||
ESP_LOGCONFIG(tag, "%s Assumed State: YES", prefix);
|
||||
}
|
||||
if (obj->is_inverted()) {
|
||||
ESP_LOGCONFIG(tag, "%s Inverted: YES", prefix);
|
||||
}
|
||||
if (!obj->get_device_class_ref().empty()) {
|
||||
ESP_LOGCONFIG(tag, "%s Device Class: '%s'", prefix, obj->get_device_class_ref().c_str());
|
||||
}
|
||||
obj->log_device_class(tag, prefix);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,9 +12,7 @@ namespace text {
|
||||
#define LOG_TEXT(prefix, type, obj) \
|
||||
if ((obj) != nullptr) { \
|
||||
ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
|
||||
if (!(obj)->get_icon_ref().empty()) { \
|
||||
ESP_LOGCONFIG(TAG, "%s Icon: '%s'", prefix, (obj)->get_icon_ref().c_str()); \
|
||||
} \
|
||||
(obj)->log_icon(TAG, prefix); \
|
||||
}
|
||||
|
||||
/** Base-class for all text inputs.
|
||||
|
||||
@@ -7,18 +7,10 @@ namespace text_sensor {
|
||||
static const char *const TAG = "text_sensor";
|
||||
|
||||
void log_text_sensor(const char *tag, const char *prefix, const char *type, TextSensor *obj) {
|
||||
if (obj == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
ESP_LOGCONFIG(tag, "%s%s '%s'", prefix, type, obj->get_name().c_str());
|
||||
|
||||
if (!obj->get_device_class_ref().empty()) {
|
||||
ESP_LOGCONFIG(tag, "%s Device Class: '%s'", prefix, obj->get_device_class_ref().c_str());
|
||||
}
|
||||
|
||||
if (!obj->get_icon_ref().empty()) {
|
||||
ESP_LOGCONFIG(tag, "%s Icon: '%s'", prefix, obj->get_icon_ref().c_str());
|
||||
if (obj != nullptr) {
|
||||
ESP_LOGCONFIG(tag, "%s%s '%s'", prefix, type, obj->get_name().c_str());
|
||||
obj->log_device_class(tag, prefix);
|
||||
obj->log_icon(tag, prefix);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -19,9 +19,7 @@ const extern float VALVE_CLOSED;
|
||||
if (traits_.get_is_assumed_state()) { \
|
||||
ESP_LOGCONFIG(TAG, "%s Assumed State: YES", prefix); \
|
||||
} \
|
||||
if (!(obj)->get_device_class_ref().empty()) { \
|
||||
ESP_LOGCONFIG(TAG, "%s Device Class: '%s'", prefix, (obj)->get_device_class_ref().c_str()); \
|
||||
} \
|
||||
(obj)->log_device_class(TAG, prefix); \
|
||||
}
|
||||
|
||||
class Valve;
|
||||
|
||||
@@ -45,6 +45,14 @@ void EntityBase::set_icon(const char *icon) {
|
||||
#endif
|
||||
}
|
||||
|
||||
void EntityBase::log_icon(const char *tag, const char *prefix) const {
|
||||
#ifdef USE_ENTITY_ICON
|
||||
if (!this->get_icon_ref().empty()) {
|
||||
ESP_LOGCONFIG(tag, "%s Icon: '%s'", prefix, this->get_icon_ref().c_str());
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
// Check if the object_id is dynamic (changes with MAC suffix)
|
||||
bool EntityBase::is_object_id_dynamic_() const {
|
||||
return !this->flags_.has_own_name && App.is_name_add_mac_suffix_enabled();
|
||||
@@ -91,6 +99,12 @@ std::string EntityBase_DeviceClass::get_device_class() {
|
||||
|
||||
void EntityBase_DeviceClass::set_device_class(const char *device_class) { this->device_class_ = device_class; }
|
||||
|
||||
void EntityBase_DeviceClass::log_device_class(const char *tag, const char *prefix) const {
|
||||
if (!this->get_device_class_ref().empty()) {
|
||||
ESP_LOGCONFIG(tag, "%s Device Class: '%s'", prefix, this->get_device_class_ref().c_str());
|
||||
}
|
||||
}
|
||||
|
||||
std::string EntityBase_UnitOfMeasurement::get_unit_of_measurement() {
|
||||
if (this->unit_of_measurement_ == nullptr)
|
||||
return "";
|
||||
|
||||
@@ -74,6 +74,9 @@ class EntityBase {
|
||||
#endif
|
||||
}
|
||||
|
||||
/// Log entity icon if present (guarded by USE_ENTITY_ICON)
|
||||
void log_icon(const char *tag, const char *prefix) const;
|
||||
|
||||
#ifdef USE_DEVICES
|
||||
// Get/set this entity's device id
|
||||
uint32_t get_device_id() const {
|
||||
@@ -171,6 +174,9 @@ class EntityBase_DeviceClass { // NOLINT(readability-identifier-naming)
|
||||
return this->device_class_ == nullptr ? EMPTY_STRING : StringRef(this->device_class_);
|
||||
}
|
||||
|
||||
/// Log entity device class if present
|
||||
void log_device_class(const char *tag, const char *prefix) const;
|
||||
|
||||
protected:
|
||||
const char *device_class_{nullptr}; ///< Device class override
|
||||
};
|
||||
@@ -247,4 +253,5 @@ template<typename T> class StatefulEntityBase : public EntityBase {
|
||||
CallbackManager<void(optional<T> previous, optional<T> current)> *full_state_callbacks_{};
|
||||
CallbackManager<void(T)> *state_callbacks_{};
|
||||
};
|
||||
|
||||
} // namespace esphome
|
||||
|
||||
Reference in New Issue
Block a user