Refactor clock syncing (#3503)

* Expose `send_local_time()` as public, for use in lambdas.
  This will send the current time configured in `time_id`.
* Add a new `set_clock()` public method, separate from time_id.
  This allows setting the clock manually, without syncing from a Time
  Component. Again this can only be called from ESPHome; i.e.,
  generally from a lambda.
This commit is contained in:
Joe 2022-06-02 21:53:20 -04:00 committed by GitHub
parent 4f57bf786b
commit a6ff02a3cf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 22 deletions

View file

@ -308,7 +308,7 @@ void Bedjet::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc
#ifdef USE_TIME #ifdef USE_TIME
if (this->time_id_.has_value()) { if (this->time_id_.has_value()) {
this->send_local_time_(); this->send_local_time();
} }
#endif #endif
break; break;
@ -463,40 +463,47 @@ uint8_t Bedjet::write_notify_config_descriptor_(bool enable) {
#ifdef USE_TIME #ifdef USE_TIME
/** Attempts to sync the local time (via `time_id`) to the BedJet device. */ /** Attempts to sync the local time (via `time_id`) to the BedJet device. */
void Bedjet::send_local_time_() { void Bedjet::send_local_time() {
if (this->node_state != espbt::ClientState::ESTABLISHED) { if (this->time_id_.has_value()) {
ESP_LOGV(TAG, "[%s] Not connected, cannot send time.", this->get_name().c_str()); auto *time_id = *this->time_id_;
return; time::ESPTime now = time_id->now();
} if (now.is_valid()) {
auto *time_id = *this->time_id_; this->set_clock(now.hour, now.minute);
time::ESPTime now = time_id->now(); ESP_LOGD(TAG, "Using time component to set BedJet clock: %d:%02d", now.hour, now.minute);
if (now.is_valid()) {
uint8_t hour = now.hour;
uint8_t minute = now.minute;
BedjetPacket *pkt = this->codec_->get_set_time_request(hour, minute);
auto status = this->write_bedjet_packet_(pkt);
if (status) {
ESP_LOGW(TAG, "Failed setting BedJet clock: %d", status);
} else {
ESP_LOGD(TAG, "[%s] BedJet clock set to: %d:%02d", this->get_name().c_str(), hour, minute);
} }
} else {
ESP_LOGI(TAG, "`time_id` is not configured: will not sync BedJet clock.");
} }
} }
/** Initializes time sync callbacks to support syncing current time to the BedJet. */ /** Initializes time sync callbacks to support syncing current time to the BedJet. */
void Bedjet::setup_time_() { void Bedjet::setup_time_() {
if (this->time_id_.has_value()) { if (this->time_id_.has_value()) {
this->send_local_time_(); this->send_local_time();
auto *time_id = *this->time_id_; auto *time_id = *this->time_id_;
time_id->add_on_time_sync_callback([this] { this->send_local_time_(); }); time_id->add_on_time_sync_callback([this] { this->send_local_time(); });
time::ESPTime now = time_id->now();
ESP_LOGD(TAG, "Using time component to set BedJet clock: %d:%02d", now.hour, now.minute);
} else { } else {
ESP_LOGI(TAG, "`time_id` is not configured: will not sync BedJet clock."); ESP_LOGI(TAG, "`time_id` is not configured: will not sync BedJet clock.");
} }
} }
#endif #endif
/** Attempt to set the BedJet device's clock to the specified time. */
void Bedjet::set_clock(uint8_t hour, uint8_t minute) {
if (this->node_state != espbt::ClientState::ESTABLISHED) {
ESP_LOGV(TAG, "[%s] Not connected, cannot send time.", this->get_name().c_str());
return;
}
BedjetPacket *pkt = this->codec_->get_set_time_request(hour, minute);
auto status = this->write_bedjet_packet_(pkt);
if (status) {
ESP_LOGW(TAG, "Failed setting BedJet clock: %d", status);
} else {
ESP_LOGD(TAG, "[%s] BedJet clock set to: %d:%02d", this->get_name().c_str(), hour, minute);
}
}
/** Writes one BedjetPacket to the BLE client on the BEDJET_COMMAND_UUID. */ /** Writes one BedjetPacket to the BLE client on the BEDJET_COMMAND_UUID. */
uint8_t Bedjet::write_bedjet_packet_(BedjetPacket *pkt) { uint8_t Bedjet::write_bedjet_packet_(BedjetPacket *pkt) {
if (this->node_state != espbt::ClientState::ESTABLISHED) { if (this->node_state != espbt::ClientState::ESTABLISHED) {

View file

@ -38,7 +38,9 @@ class Bedjet : public climate::Climate, public esphome::ble_client::BLEClientNod
#ifdef USE_TIME #ifdef USE_TIME
void set_time_id(time::RealTimeClock *time_id) { this->time_id_ = time_id; } void set_time_id(time::RealTimeClock *time_id) { this->time_id_ = time_id; }
void send_local_time();
#endif #endif
void set_clock(uint8_t hour, uint8_t minute);
void set_status_timeout(uint32_t timeout) { this->timeout_ = timeout; } void set_status_timeout(uint32_t timeout) { this->timeout_ = timeout; }
/** Sets the default strategy to use for climate::CLIMATE_MODE_HEAT. */ /** Sets the default strategy to use for climate::CLIMATE_MODE_HEAT. */
void set_heating_mode(BedjetHeatMode mode) { this->heating_mode_ = mode; } void set_heating_mode(BedjetHeatMode mode) { this->heating_mode_ = mode; }
@ -90,7 +92,6 @@ class Bedjet : public climate::Climate, public esphome::ble_client::BLEClientNod
#ifdef USE_TIME #ifdef USE_TIME
void setup_time_(); void setup_time_();
void send_local_time_();
optional<time::RealTimeClock *> time_id_{}; optional<time::RealTimeClock *> time_id_{};
#endif #endif