diff --git a/esphome/components/sun/text_sensor/sun_text_sensor.h b/esphome/components/sun/text_sensor/sun_text_sensor.h index 9345a32223..c3b60ffd65 100644 --- a/esphome/components/sun/text_sensor/sun_text_sensor.h +++ b/esphome/components/sun/text_sensor/sun_text_sensor.h @@ -14,7 +14,9 @@ class SunTextSensor : public text_sensor::TextSensor, public PollingComponent { void set_parent(Sun *parent) { parent_ = parent; } void set_elevation(double elevation) { elevation_ = elevation; } void set_sunrise(bool sunrise) { sunrise_ = sunrise; } - void set_format(const std::string &format) { format_ = format; } + void set_format(const char *format) { this->format_ = format; } + /// Prevent accidental use of std::string which would dangle + void set_format(const std::string &format) = delete; void update() override { optional res; @@ -29,14 +31,14 @@ class SunTextSensor : public text_sensor::TextSensor, public PollingComponent { } char buf[ESPTime::STRFTIME_BUFFER_SIZE]; - size_t len = res->strftime_to(buf, this->format_.c_str()); + size_t len = res->strftime_to(buf, this->format_); this->publish_state(buf, len); } void dump_config() override; protected: - std::string format_{}; + const char *format_{nullptr}; Sun *parent_; double elevation_; bool sunrise_;