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