tuya: add time sync callback only once to prevent memleak (#5234)

This commit is contained in:
Kjell Braden 2023-08-13 23:09:51 +02:00 committed by Jesse Hills
parent f26238e824
commit 3a899e28dc
No known key found for this signature in database
GPG Key ID: BEAAE804EFD8E83A
2 changed files with 8 additions and 2 deletions

View File

@ -246,8 +246,13 @@ void Tuya::handle_command_(uint8_t command, uint8_t version, const uint8_t *buff
#ifdef USE_TIME
if (this->time_id_.has_value()) {
this->send_local_time_();
auto *time_id = *this->time_id_;
time_id->add_on_time_sync_callback([this] { this->send_local_time_(); });
if (!this->time_sync_callback_registered_) {
// tuya mcu supports time, so we let them know when our time changed
auto *time_id = *this->time_id_;
time_id->add_on_time_sync_callback([this] { this->send_local_time_(); });
this->time_sync_callback_registered_ = true;
}
} else {
ESP_LOGW(TAG, "LOCAL_TIME_QUERY is not handled because time is not configured");
}

View File

@ -130,6 +130,7 @@ class Tuya : public Component, public uart::UARTDevice {
#ifdef USE_TIME
void send_local_time_();
optional<time::RealTimeClock *> time_id_{};
bool time_sync_callback_registered_{false};
#endif
TuyaInitState init_state_ = TuyaInitState::INIT_HEARTBEAT;
bool init_failed_{false};