mirror of
https://github.com/esphome/esphome.git
synced 2024-12-21 16:27:44 +01:00
[sntp] Resolve warnings from ESP-IDF 5.x (#7913)
This commit is contained in:
parent
dbed74b50d
commit
79478cdb8a
@ -9,11 +9,6 @@
|
||||
#include "lwip/apps/sntp.h"
|
||||
#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 sntp {
|
||||
|
||||
@ -26,30 +21,29 @@ void SNTPComponent::setup() {
|
||||
esp_sntp_stop();
|
||||
}
|
||||
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
|
||||
sntp_stop();
|
||||
sntp_setoperatingmode(SNTP_OPMODE_POLL);
|
||||
#endif
|
||||
|
||||
sntp_setservername(0, strdup(this->server_1_.c_str()));
|
||||
if (!this->server_2_.empty()) {
|
||||
sntp_setservername(1, strdup(this->server_2_.c_str()));
|
||||
size_t i = 0;
|
||||
for (auto &server : this->servers_) {
|
||||
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();
|
||||
#endif
|
||||
}
|
||||
void SNTPComponent::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "SNTP Time:");
|
||||
ESP_LOGCONFIG(TAG, " Server 1: '%s'", this->server_1_.c_str());
|
||||
ESP_LOGCONFIG(TAG, " Server 2: '%s'", this->server_2_.c_str());
|
||||
ESP_LOGCONFIG(TAG, " Server 3: '%s'", this->server_3_.c_str());
|
||||
ESP_LOGCONFIG(TAG, " Timezone: '%s'", this->timezone_.c_str());
|
||||
size_t i = 0;
|
||||
for (auto &server : this->servers_) {
|
||||
ESP_LOGCONFIG(TAG, " Server %zu: '%s'", i++, server.c_str());
|
||||
}
|
||||
}
|
||||
void SNTPComponent::update() {
|
||||
#if !defined(USE_ESP_IDF)
|
||||
|
@ -14,23 +14,20 @@ namespace sntp {
|
||||
/// \see https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html
|
||||
class SNTPComponent : public time::RealTimeClock {
|
||||
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 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; }
|
||||
|
||||
void update() override;
|
||||
void loop() override;
|
||||
|
||||
protected:
|
||||
std::string server_1_;
|
||||
std::string server_2_;
|
||||
std::string server_3_;
|
||||
std::vector<std::string> servers_;
|
||||
bool has_time_{false};
|
||||
};
|
||||
|
||||
|
@ -1,16 +1,16 @@
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import time as time_
|
||||
import esphome.config_validation as cv
|
||||
import esphome.codegen as cg
|
||||
from esphome.core import CORE
|
||||
from esphome.const import (
|
||||
CONF_ID,
|
||||
CONF_SERVERS,
|
||||
PLATFORM_BK72XX,
|
||||
PLATFORM_ESP32,
|
||||
PLATFORM_ESP8266,
|
||||
PLATFORM_RP2040,
|
||||
PLATFORM_RTL87XX,
|
||||
PLATFORM_BK72XX,
|
||||
)
|
||||
from esphome.core import CORE
|
||||
|
||||
DEPENDENCIES = ["network"]
|
||||
sntp_ns = cg.esphome_ns.namespace("sntp")
|
||||
@ -40,11 +40,8 @@ CONFIG_SCHEMA = cv.All(
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
|
||||
servers = config[CONF_SERVERS]
|
||||
servers += [""] * (3 - len(servers))
|
||||
cg.add(var.set_servers(*servers))
|
||||
var = cg.new_Pvariable(config[CONF_ID], servers)
|
||||
|
||||
await cg.register_component(var, config)
|
||||
await time_.register_time(var, config)
|
||||
|
Loading…
Reference in New Issue
Block a user