sntp_component.h 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #pragma once
  2. #include "esphome/core/component.h"
  3. #include "esphome/components/time/real_time_clock.h"
  4. namespace esphome {
  5. namespace sntp {
  6. /// The SNTP component allows you to configure local timekeeping via Simple Network Time Protocol.
  7. ///
  8. /// \note
  9. /// The C library (newlib) available on ESPs only supports TZ strings that specify an offset and DST info;
  10. /// you cannot specify zone names or paths to zoneinfo files.
  11. /// \see https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html
  12. class SNTPComponent : public time::RealTimeClock {
  13. public:
  14. void setup() override;
  15. void dump_config() override;
  16. /// Change the servers used by SNTP for timekeeping
  17. void set_servers(const std::string &server_1, const std::string &server_2, const std::string &server_3) {
  18. this->server_1_ = server_1;
  19. this->server_2_ = server_2;
  20. this->server_3_ = server_3;
  21. }
  22. float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
  23. void update() override;
  24. void loop() override;
  25. protected:
  26. std::string server_1_;
  27. std::string server_2_;
  28. std::string server_3_;
  29. bool has_time_{false};
  30. };
  31. } // namespace sntp
  32. } // namespace esphome