ESPHome 2026.11.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 ezb_zcl_time_interface_t time_interface = {
31 };
32 ezb_err_t ret;
33 if (!esp_zigbee_lock_acquire(10 / portTICK_PERIOD_MS)) {
34 this->set_timeout(REGISTER_RETRY_TIMEOUT_ID, 100, [this]() { this->register_zb_time_(); });
35 return;
36 }
37 ret = ezb_zcl_time_server_interface_register(this->endpoint_, time_interface);
38 esp_zigbee_lock_release();
39 if (ret != EZB_ERR_NONE) {
40 ESP_LOGW(TAG, "Setup failed: %d", ret);
41 this->mark_failed();
42 return;
43 }
44 this->registered_ = true;
45 this->parent_->add_on_join_callback([this](bool x) { this->update(); });
46 if (this->parent_->is_joined()) {
47 this->update();
48 }
49}
50
51void ZigbeeTime::status_cb(ezb_err_t status) {
52 if (status == EZB_ERR_NONE) {
53 ESP_LOGV(TAG, "Time synchronization successful");
54 } else if (status == EZB_ERR_TIMEOUT) {
55 ESP_LOGW(TAG, "Time synchronization timed out");
56 } else {
57 ESP_LOGW(TAG, "Time synchronization failed with error: %d", status);
58 }
59}
60
62 if (this->parent_->is_joined() && this->registered_) {
63 if (esp_zigbee_lock_acquire(10 / portTICK_PERIOD_MS)) {
64 ESP_LOGV(TAG, "Updating time sync from Zigbee network...");
65 ezb_zcl_time_server_synchronize_time(this->endpoint_, 10, esphome::zigbee::ZigbeeTime::status_cb,
66 EZB_ZCL_TIME_SERVER_RANK_MASTER);
67 esp_zigbee_lock_release();
68 this->retry_count_ = 0;
69 } else {
70 if (this->retry_count_ == 0) {
71 ESP_LOGW(TAG, "Could not acquire Zigbee lock to synchronize time, will retry maximum 3 times");
72 }
73 if (this->retry_count_ < 3) {
74 this->set_timeout(SYNC_RETRY_TIMEOUT_ID, 100, [this]() { this->update(); });
75 this->retry_count_++;
76 } else {
77 ESP_LOGW(TAG, "Could not acquire Zigbee lock to synchronize time");
78 this->retry_count_ = 0;
79 }
80 }
81 } else {
82 ESP_LOGD(TAG, "Not connected to Zigbee network, cannot synchronize time");
83 }
84}
85
87 const time_t now = global_time->timestamp_now();
88 if (now < EPOCH_2000) {
89 return 0xFFFFFFFF; // ZCL invalid UTCTime
90 }
91 return (uint32_t) (now - EPOCH_2000);
92}
93
95 // prevent overflow
96 if (utc <= (std::numeric_limits<uint32_t>::max() - EPOCH_2000)) {
97 global_time->set_epoch_time(utc + EPOCH_2000);
98 }
99}
100
102 // called from zigbee task, defer to main loop
103 this->defer([this, utc]() {
104 ESP_LOGV(TAG, "Setting device time to UTC: %u", static_cast<unsigned>(utc));
105 this->synchronize_epoch_(utc);
106 });
107}
108
110 ESP_LOGCONFIG(TAG,
111 "Zigbee Time\n"
112 " Endpoint: %u",
113 this->endpoint_);
114 RealTimeClock::dump_config();
115}
116
117} // namespace esphome::zigbee
118
119#endif
uint8_t status
Definition bl0942.h:8
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.
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