diff --git a/esphome/core/entity_base.cpp b/esphome/core/entity_base.cpp index 31e9618640..eafc04f92a 100644 --- a/esphome/core/entity_base.cpp +++ b/esphome/core/entity_base.cpp @@ -52,12 +52,22 @@ __attribute__((weak)) const char *entity_icon_lookup(uint8_t) { return ""; } // Entity device class (from index) StringRef EntityBase::get_device_class_ref() const { +#ifdef USE_ENTITY_DEVICE_CLASS return StringRef(entity_device_class_lookup(this->device_class_idx_)); +#else + return StringRef(entity_device_class_lookup(0)); +#endif } std::string EntityBase::get_device_class() const { return std::string(this->get_device_class_ref().c_str()); } // Entity unit of measurement (from index) -StringRef EntityBase::get_unit_of_measurement_ref() const { return StringRef(entity_uom_lookup(this->uom_idx_)); } +StringRef EntityBase::get_unit_of_measurement_ref() const { +#ifdef USE_ENTITY_UNIT_OF_MEASUREMENT + return StringRef(entity_uom_lookup(this->uom_idx_)); +#else + return StringRef(entity_uom_lookup(0)); +#endif +} std::string EntityBase::get_unit_of_measurement() const { return std::string(this->get_unit_of_measurement_ref().c_str()); } diff --git a/esphome/core/entity_base.h b/esphome/core/entity_base.h index e67f16422a..042eebb40f 100644 --- a/esphome/core/entity_base.h +++ b/esphome/core/entity_base.h @@ -97,10 +97,16 @@ class EntityBase { // Set entity string table indices — one call per entity from codegen. // Packed: [23..16] icon | [15..8] UoM | [7..0] device_class (each 8 bits) - void set_entity_strings(uint32_t packed) { + void set_entity_strings([[maybe_unused]] uint32_t packed) { +#ifdef USE_ENTITY_DEVICE_CLASS this->device_class_idx_ = packed & 0xFF; +#endif +#ifdef USE_ENTITY_UNIT_OF_MEASUREMENT this->uom_idx_ = (packed >> 8) & 0xFF; +#endif +#ifdef USE_ENTITY_ICON this->icon_idx_ = (packed >> 16) & 0xFF; +#endif } // Get device class as StringRef (from packed index) @@ -209,9 +215,15 @@ class EntityBase { uint8_t reserved : 2; // Reserved for future use } flags_{}; // String table indices — packed into the 3 padding bytes after flags_ +#ifdef USE_ENTITY_DEVICE_CLASS uint8_t device_class_idx_{}; +#endif +#ifdef USE_ENTITY_UNIT_OF_MEASUREMENT uint8_t uom_idx_{}; +#endif +#ifdef USE_ENTITY_ICON uint8_t icon_idx_{}; +#endif }; /// Log entity icon if set (for use in dump_config) diff --git a/esphome/core/entity_helpers.py b/esphome/core/entity_helpers.py index cc435e7c47..fd151d82d9 100644 --- a/esphome/core/entity_helpers.py +++ b/esphome/core/entity_helpers.py @@ -165,14 +165,18 @@ def register_icon(value: str) -> int: def setup_device_class(config: ConfigType) -> None: """Register config's device_class and store its index for finalize_entity_strings.""" - config[_KEY_DC_IDX] = register_device_class(config.get(CONF_DEVICE_CLASS, "")) + idx = register_device_class(config.get(CONF_DEVICE_CLASS, "")) + if idx: + cg.add_define("USE_ENTITY_DEVICE_CLASS") + config[_KEY_DC_IDX] = idx def setup_unit_of_measurement(config: ConfigType) -> None: """Register config's unit_of_measurement and store its index for finalize_entity_strings.""" - config[_KEY_UOM_IDX] = register_unit_of_measurement( - config.get(CONF_UNIT_OF_MEASUREMENT, "") - ) + idx = register_unit_of_measurement(config.get(CONF_UNIT_OF_MEASUREMENT, "")) + if idx: + cg.add_define("USE_ENTITY_UNIT_OF_MEASUREMENT") + config[_KEY_UOM_IDX] = idx def finalize_entity_strings(var: MockObj, config: ConfigType) -> None: