ESPHome 2026.9.0-dev
Loading...
Searching...
No Matches
zigbee_time_esp32.cpp
Go to the documentation of this file.
1#include "zigbee_time_esp32.h"
2#if defined(USE_ZIGBEE) && defined(USE_ESP32) && defined(USE_TIME)
3#include "esphome/core/log.h"
5
6namespace esphome::zigbee {
7
8static const char *const TAG = "zigbee.time";
9
10// This time standard is the number of
11// seconds since 0 hrs 0 mins 0 sec on 1st January 2000 UTC (Universal Coordinated Time).
12constexpr time_t EPOCH_2000 = 946684800;
13
14static ZigbeeTime *global_time = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
15
17 global_time = this;
18 if (this->parent_->is_started()) {
19 this->register_zb_time_();
20 } else {
21 this->parent_->add_on_start_callback([this]() { this->register_zb_time_(); });
22 }
23}
24
26 ezb_zcl_time_interface_t time_interface = {
29 };
30 ezb_err_t ret;
31 if (!esp_zigbee_lock_acquire(10 / portTICK_PERIOD_MS)) {
32 this->set_timeout("zb_time_register", 100, [this]() { this->register_zb_time_(); });
33 return;
34 }
35 ret = ezb_zcl_time_server_interface_register(this->endpoint_, time_interface);
36 esp_zigbee_lock_release();
37 if (ret != EZB_ERR_NONE) {
38 ESP_LOGW(TAG, "Setup failed: %d", ret);
39 this->mark_failed();
40 return;
41 }
42 this->registered_ = true;
43 this->parent_->add_on_join_callback([this](bool x) { this->update(); });
44 if (this->parent_->is_joined()) {
45 this->update();
46 }
47}
48
49void ZigbeeTime::status_cb(ezb_err_t status) {
50 if (status == EZB_ERR_NONE) {
51 ESP_LOGV(TAG, "Time synchronization successful");
52 } else if (status == EZB_ERR_TIMEOUT) {
53 ESP_LOGW(TAG, "Time synchronization timed out");
54 } else {
55 ESP_LOGW(TAG, "Time synchronization failed with error: %d", status);
56 }
57}
58
60 if (this->parent_->is_joined() && this->registered_) {
61 if (esp_zigbee_lock_acquire(10 / portTICK_PERIOD_MS)) {
62 ESP_LOGV(TAG, "Updating time sync from Zigbee network...");
63 ezb_zcl_time_server_synchronize_time(this->endpoint_, 10, esphome::zigbee::ZigbeeTime::status_cb,
64 EZB_ZCL_TIME_SERVER_RANK_MASTER);
65 esp_zigbee_lock_release();
66 this->retry_count_ = 0;
67 } else {
68 if (this->retry_count_ == 0) {
69 ESP_LOGW(TAG, "Could not acquire Zigbee lock to synchronize time, will retry maximum 3 times");
70 }
71 if (this->retry_count_ < 3) {
72 this->set_timeout("zb_time_sync", 100, [this]() { this->update(); });
73 this->retry_count_++;
74 } else {
75 ESP_LOGW(TAG, "Could not acquire Zigbee lock to synchronize time");
76 this->retry_count_ = 0;
77 }
78 }
79 } else {
80 ESP_LOGD(TAG, "Not connected to Zigbee network, cannot synchronize time");
81 }
82}
83
85 const time_t now = global_time->timestamp_now();
86 if (now < EPOCH_2000) {
87 return 0xFFFFFFFF; // ZCL invalid UTCTime
88 }
89 return (uint32_t) (now - EPOCH_2000);
90}
91
93 // prevent overflow
94 if (utc <= (std::numeric_limits<uint32_t>::max() - EPOCH_2000)) {
95 global_time->set_epoch_time(utc + EPOCH_2000);
96 }
97}
98
100 // called from zigbee task, defer to main loop
101 this->defer([this, utc]() {
102 ESP_LOGV(TAG, "Setting device time to UTC: %u", static_cast<unsigned>(utc));
103 this->synchronize_epoch_(utc);
104 });
106}
107
109 ESP_LOGCONFIG(TAG,
110 "Zigbee Time\n"
111 " Endpoint: %u",
112 this->endpoint_);
113 RealTimeClock::dump_config();
114}
115
116} // namespace esphome::zigbee
117
118#endif
uint8_t status
Definition bl0942.h:8
void wake_loop_threadsafe()
Wake the main event loop from another thread or callback.
void mark_failed()
Mark this component as failed.
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
void defer(const char *name, std::function< void()> &&f)
Defer a callback to the next loop() call with a const char* name.
time_t timestamp_now()
Get the current time as the UTC epoch since January 1st 1970.
ESPTime now()
Get the time in the currently defined timezone.
void synchronize_epoch_(uint32_t epoch)
Report a unix epoch as current time.
static void status_cb(ezb_err_t status)
static void set_utc_time(uint32_t utc)
int ret
const char *const TAG
Definition spi.cpp:7
constexpr time_t EPOCH_2000
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t
uint16_t x
Definition tt21100.cpp:5