From 0534d1bfcf606ebd13f06f24cc1cc9b3c1f13f0f Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 21 Jul 2025 21:46:37 -1000 Subject: [PATCH] preen --- .../homeassistant/number/homeassistant_number.cpp | 10 +++++++--- .../homeassistant/switch/homeassistant_switch.cpp | 10 +++++++--- esphome/core/entity_base.h | 11 +++++++---- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/esphome/components/homeassistant/number/homeassistant_number.cpp b/esphome/components/homeassistant/number/homeassistant_number.cpp index d56fe64f5d..cf37c7744f 100644 --- a/esphome/components/homeassistant/number/homeassistant_number.cpp +++ b/esphome/components/homeassistant/number/homeassistant_number.cpp @@ -83,17 +83,21 @@ void HomeassistantNumber::control(float value) { this->publish_state(value); + static constexpr auto SERVICE_NAME = StringRef::from_lit("number.set_value"); + static constexpr auto ENTITY_ID_KEY = StringRef::from_lit("entity_id"); + static constexpr auto VALUE_KEY = StringRef::from_lit("value"); + api::HomeassistantServiceResponse resp; - resp.set_service(StringRef("number.set_value")); + resp.set_service(SERVICE_NAME); resp.data.emplace_back(); auto &entity_id = resp.data.back(); - entity_id.set_key(StringRef("entity_id")); + entity_id.set_key(ENTITY_ID_KEY); entity_id.set_value(StringRef(this->entity_id_)); resp.data.emplace_back(); auto &entity_value = resp.data.back(); - entity_value.set_key(StringRef("value")); + entity_value.set_key(VALUE_KEY); std::string value_str = to_string(value); entity_value.set_value(StringRef(value_str)); diff --git a/esphome/components/homeassistant/switch/homeassistant_switch.cpp b/esphome/components/homeassistant/switch/homeassistant_switch.cpp index 68177e634b..0fe609bf43 100644 --- a/esphome/components/homeassistant/switch/homeassistant_switch.cpp +++ b/esphome/components/homeassistant/switch/homeassistant_switch.cpp @@ -40,16 +40,20 @@ void HomeassistantSwitch::write_state(bool state) { return; } + static constexpr auto SERVICE_ON = StringRef::from_lit("homeassistant.turn_on"); + static constexpr auto SERVICE_OFF = StringRef::from_lit("homeassistant.turn_off"); + static constexpr auto ENTITY_ID_KEY = StringRef::from_lit("entity_id"); + api::HomeassistantServiceResponse resp; if (state) { - resp.set_service(StringRef("homeassistant.turn_on")); + resp.set_service(SERVICE_ON); } else { - resp.set_service(StringRef("homeassistant.turn_off")); + resp.set_service(SERVICE_OFF); } resp.data.emplace_back(); auto &entity_id_kv = resp.data.back(); - entity_id_kv.set_key(StringRef("entity_id")); + entity_id_kv.set_key(ENTITY_ID_KEY); entity_id_kv.set_value(StringRef(this->entity_id_)); api::global_api_server->send_homeassistant_service_call(resp); diff --git a/esphome/core/entity_base.h b/esphome/core/entity_base.h index 0073d72641..e60e0728bc 100644 --- a/esphome/core/entity_base.h +++ b/esphome/core/entity_base.h @@ -55,10 +55,11 @@ class EntityBase { std::string get_icon() const; void set_icon(const char *icon); StringRef get_icon_ref() const { + static constexpr auto EMPTY_STRING = StringRef::from_lit(""); #ifdef USE_ENTITY_ICON - return this->icon_c_str_ == nullptr ? StringRef("") : StringRef(this->icon_c_str_); + return this->icon_c_str_ == nullptr ? EMPTY_STRING : StringRef(this->icon_c_str_); #else - return StringRef(""); + return EMPTY_STRING; #endif } @@ -114,7 +115,8 @@ class EntityBase_DeviceClass { // NOLINT(readability-identifier-naming) void set_device_class(const char *device_class); /// Get the device class as StringRef StringRef get_device_class_ref() const { - return this->device_class_ == nullptr ? StringRef("") : StringRef(this->device_class_); + static constexpr auto EMPTY_STRING = StringRef::from_lit(""); + return this->device_class_ == nullptr ? EMPTY_STRING : StringRef(this->device_class_); } protected: @@ -129,7 +131,8 @@ class EntityBase_UnitOfMeasurement { // NOLINT(readability-identifier-naming) void set_unit_of_measurement(const char *unit_of_measurement); /// Get the unit of measurement as StringRef StringRef get_unit_of_measurement_ref() const { - return this->unit_of_measurement_ == nullptr ? StringRef("") : StringRef(this->unit_of_measurement_); + static constexpr auto EMPTY_STRING = StringRef::from_lit(""); + return this->unit_of_measurement_ == nullptr ? EMPTY_STRING : StringRef(this->unit_of_measurement_); } protected: