mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-30 22:53:59 +00:00 
			
		
		
		
	[sntp] Resolve warnings from ESP-IDF 5.x (#7913)
This commit is contained in:
		| @@ -9,11 +9,6 @@ | |||||||
| #include "lwip/apps/sntp.h" | #include "lwip/apps/sntp.h" | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
| // Yes, the server names are leaked, but that's fine. |  | ||||||
| #ifdef CLANG_TIDY |  | ||||||
| #define strdup(x) (const_cast<char *>(x)) |  | ||||||
| #endif |  | ||||||
|  |  | ||||||
| namespace esphome { | namespace esphome { | ||||||
| namespace sntp { | namespace sntp { | ||||||
|  |  | ||||||
| @@ -26,30 +21,29 @@ void SNTPComponent::setup() { | |||||||
|     esp_sntp_stop(); |     esp_sntp_stop(); | ||||||
|   } |   } | ||||||
|   esp_sntp_setoperatingmode(ESP_SNTP_OPMODE_POLL); |   esp_sntp_setoperatingmode(ESP_SNTP_OPMODE_POLL); | ||||||
|  |   size_t i = 0; | ||||||
|  |   for (auto &server : this->servers_) { | ||||||
|  |     esp_sntp_setservername(i++, server.c_str()); | ||||||
|  |   } | ||||||
|  |   esp_sntp_set_sync_interval(this->get_update_interval()); | ||||||
|  |   esp_sntp_init(); | ||||||
| #else | #else | ||||||
|   sntp_stop(); |   sntp_stop(); | ||||||
|   sntp_setoperatingmode(SNTP_OPMODE_POLL); |   sntp_setoperatingmode(SNTP_OPMODE_POLL); | ||||||
| #endif |  | ||||||
|  |  | ||||||
|   sntp_setservername(0, strdup(this->server_1_.c_str())); |   size_t i = 0; | ||||||
|   if (!this->server_2_.empty()) { |   for (auto &server : this->servers_) { | ||||||
|     sntp_setservername(1, strdup(this->server_2_.c_str())); |     sntp_setservername(i++, server.c_str()); | ||||||
|   } |   } | ||||||
|   if (!this->server_3_.empty()) { |  | ||||||
|     sntp_setservername(2, strdup(this->server_3_.c_str())); |  | ||||||
|   } |  | ||||||
| #ifdef USE_ESP_IDF |  | ||||||
|   esp_sntp_set_sync_interval(this->get_update_interval()); |  | ||||||
| #endif |  | ||||||
|  |  | ||||||
|   sntp_init(); |   sntp_init(); | ||||||
|  | #endif | ||||||
| } | } | ||||||
| void SNTPComponent::dump_config() { | void SNTPComponent::dump_config() { | ||||||
|   ESP_LOGCONFIG(TAG, "SNTP Time:"); |   ESP_LOGCONFIG(TAG, "SNTP Time:"); | ||||||
|   ESP_LOGCONFIG(TAG, "  Server 1: '%s'", this->server_1_.c_str()); |   size_t i = 0; | ||||||
|   ESP_LOGCONFIG(TAG, "  Server 2: '%s'", this->server_2_.c_str()); |   for (auto &server : this->servers_) { | ||||||
|   ESP_LOGCONFIG(TAG, "  Server 3: '%s'", this->server_3_.c_str()); |     ESP_LOGCONFIG(TAG, "  Server %zu: '%s'", i++, server.c_str()); | ||||||
|   ESP_LOGCONFIG(TAG, "  Timezone: '%s'", this->timezone_.c_str()); |   } | ||||||
| } | } | ||||||
| void SNTPComponent::update() { | void SNTPComponent::update() { | ||||||
| #if !defined(USE_ESP_IDF) | #if !defined(USE_ESP_IDF) | ||||||
|   | |||||||
| @@ -14,23 +14,20 @@ namespace sntp { | |||||||
| /// \see https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html | /// \see https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html | ||||||
| class SNTPComponent : public time::RealTimeClock { | class SNTPComponent : public time::RealTimeClock { | ||||||
|  public: |  public: | ||||||
|  |   SNTPComponent(const std::vector<std::string> &servers) : servers_(servers) {} | ||||||
|  |  | ||||||
|  |   // Note: set_servers() has been removed and replaced by a constructor - calling set_servers after setup would | ||||||
|  |   // have had no effect anyway, and making the strings immutable avoids the need to strdup their contents. | ||||||
|  |  | ||||||
|   void setup() override; |   void setup() override; | ||||||
|   void dump_config() override; |   void dump_config() override; | ||||||
|   /// Change the servers used by SNTP for timekeeping |  | ||||||
|   void set_servers(const std::string &server_1, const std::string &server_2, const std::string &server_3) { |  | ||||||
|     this->server_1_ = server_1; |  | ||||||
|     this->server_2_ = server_2; |  | ||||||
|     this->server_3_ = server_3; |  | ||||||
|   } |  | ||||||
|   float get_setup_priority() const override { return setup_priority::BEFORE_CONNECTION; } |   float get_setup_priority() const override { return setup_priority::BEFORE_CONNECTION; } | ||||||
|  |  | ||||||
|   void update() override; |   void update() override; | ||||||
|   void loop() override; |   void loop() override; | ||||||
|  |  | ||||||
|  protected: |  protected: | ||||||
|   std::string server_1_; |   std::vector<std::string> servers_; | ||||||
|   std::string server_2_; |  | ||||||
|   std::string server_3_; |  | ||||||
|   bool has_time_{false}; |   bool has_time_{false}; | ||||||
| }; | }; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,16 +1,16 @@ | |||||||
|  | import esphome.codegen as cg | ||||||
| from esphome.components import time as time_ | from esphome.components import time as time_ | ||||||
| import esphome.config_validation as cv | import esphome.config_validation as cv | ||||||
| import esphome.codegen as cg |  | ||||||
| from esphome.core import CORE |  | ||||||
| from esphome.const import ( | from esphome.const import ( | ||||||
|     CONF_ID, |     CONF_ID, | ||||||
|     CONF_SERVERS, |     CONF_SERVERS, | ||||||
|  |     PLATFORM_BK72XX, | ||||||
|     PLATFORM_ESP32, |     PLATFORM_ESP32, | ||||||
|     PLATFORM_ESP8266, |     PLATFORM_ESP8266, | ||||||
|     PLATFORM_RP2040, |     PLATFORM_RP2040, | ||||||
|     PLATFORM_RTL87XX, |     PLATFORM_RTL87XX, | ||||||
|     PLATFORM_BK72XX, |  | ||||||
| ) | ) | ||||||
|  | from esphome.core import CORE | ||||||
|  |  | ||||||
| DEPENDENCIES = ["network"] | DEPENDENCIES = ["network"] | ||||||
| sntp_ns = cg.esphome_ns.namespace("sntp") | sntp_ns = cg.esphome_ns.namespace("sntp") | ||||||
| @@ -40,11 +40,8 @@ CONFIG_SCHEMA = cv.All( | |||||||
|  |  | ||||||
|  |  | ||||||
| async def to_code(config): | async def to_code(config): | ||||||
|     var = cg.new_Pvariable(config[CONF_ID]) |  | ||||||
|  |  | ||||||
|     servers = config[CONF_SERVERS] |     servers = config[CONF_SERVERS] | ||||||
|     servers += [""] * (3 - len(servers)) |     var = cg.new_Pvariable(config[CONF_ID], servers) | ||||||
|     cg.add(var.set_servers(*servers)) |  | ||||||
|  |  | ||||||
|     await cg.register_component(var, config) |     await cg.register_component(var, config) | ||||||
|     await time_.register_time(var, config) |     await time_.register_time(var, config) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user