diff --git a/esphome/components/binary_sensor/binary_sensor.cpp b/esphome/components/binary_sensor/binary_sensor.cpp index 33b3de6d72..2fc8d9d01f 100644 --- a/esphome/components/binary_sensor/binary_sensor.cpp +++ b/esphome/components/binary_sensor/binary_sensor.cpp @@ -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); } } diff --git a/esphome/components/button/button.cpp b/esphome/components/button/button.cpp index c968d31088..3a2624a6bf 100644 --- a/esphome/components/button/button.cpp +++ b/esphome/components/button/button.cpp @@ -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); } } diff --git a/esphome/components/cover/cover.h b/esphome/components/cover/cover.h index d5db6cfb4f..3308255f9a 100644 --- a/esphome/components/cover/cover.h +++ b/esphome/components/cover/cover.h @@ -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; diff --git a/esphome/components/datetime/date_entity.h b/esphome/components/datetime/date_entity.h index ba2edb127a..ba2a64062d 100644 --- a/esphome/components/datetime/date_entity.h +++ b/esphome/components/datetime/date_entity.h @@ -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; diff --git a/esphome/components/datetime/datetime_entity.h b/esphome/components/datetime/datetime_entity.h index 43bff5a181..9955686d8d 100644 --- a/esphome/components/datetime/datetime_entity.h +++ b/esphome/components/datetime/datetime_entity.h @@ -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; diff --git a/esphome/components/datetime/time_entity.h b/esphome/components/datetime/time_entity.h index c5cbeb52da..30f73f3be2 100644 --- a/esphome/components/datetime/time_entity.h +++ b/esphome/components/datetime/time_entity.h @@ -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; diff --git a/esphome/components/event/event.h b/esphome/components/event/event.h index 2f6267a200..f2a619eb38 100644 --- a/esphome/components/event/event.h +++ b/esphome/components/event/event.h @@ -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 { diff --git a/esphome/components/lock/lock.h b/esphome/components/lock/lock.h index 9737569921..842c4e732b 100644 --- a/esphome/components/lock/lock.h +++ b/esphome/components/lock/lock.h @@ -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); \ } \ diff --git a/esphome/components/number/number.cpp b/esphome/components/number/number.cpp index da08faf655..85e0d41b9c 100644 --- a/esphome/components/number/number.cpp +++ b/esphome/components/number/number.cpp @@ -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); } } diff --git a/esphome/components/select/select.h b/esphome/components/select/select.h index f859594cd1..bc3d35fa3d 100644 --- a/esphome/components/select/select.h +++ b/esphome/components/select/select.h @@ -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) \ diff --git a/esphome/components/sensor/sensor.cpp b/esphome/components/sensor/sensor.cpp index 92da4345b7..a472f9bec6 100644 --- a/esphome/components/sensor/sensor.cpp +++ b/esphome/components/sensor/sensor.cpp @@ -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); + } } } diff --git a/esphome/components/switch/switch.cpp b/esphome/components/switch/switch.cpp index 02cee91a76..659d15d883 100644 --- a/esphome/components/switch/switch.cpp +++ b/esphome/components/switch/switch.cpp @@ -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); } } diff --git a/esphome/components/text/text.h b/esphome/components/text/text.h index 74d08eda8a..18d5ba3f50 100644 --- a/esphome/components/text/text.h +++ b/esphome/components/text/text.h @@ -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. diff --git a/esphome/components/text_sensor/text_sensor.cpp b/esphome/components/text_sensor/text_sensor.cpp index 0294d65861..e5eea3c7af 100644 --- a/esphome/components/text_sensor/text_sensor.cpp +++ b/esphome/components/text_sensor/text_sensor.cpp @@ -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); } } diff --git a/esphome/components/valve/valve.h b/esphome/components/valve/valve.h index ab7ff5abe1..cd41c6d34a 100644 --- a/esphome/components/valve/valve.h +++ b/esphome/components/valve/valve.h @@ -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; diff --git a/esphome/core/entity_base.cpp b/esphome/core/entity_base.cpp index 4883c72cf1..404297cbfd 100644 --- a/esphome/core/entity_base.cpp +++ b/esphome/core/entity_base.cpp @@ -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 ""; diff --git a/esphome/core/entity_base.h b/esphome/core/entity_base.h index 6e5362464f..94436c0d47 100644 --- a/esphome/core/entity_base.h +++ b/esphome/core/entity_base.h @@ -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 class StatefulEntityBase : public EntityBase { CallbackManager previous, optional current)> *full_state_callbacks_{}; CallbackManager *state_callbacks_{}; }; + } // namespace esphome