From 1bdeb405f24981c3a964dd436ad46d527a4586ea Mon Sep 17 00:00:00 2001 From: Oleg Tarasov Date: Fri, 15 Nov 2024 18:46:00 +0300 Subject: [PATCH] Some fixes, added an ability to clear calibration --- .../components/shelly_dimmer/shelly_dimmer.cpp | 18 +++++++++++++----- .../components/shelly_dimmer/shelly_dimmer.h | 11 +++++++++-- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/esphome/components/shelly_dimmer/shelly_dimmer.cpp b/esphome/components/shelly_dimmer/shelly_dimmer.cpp index 9637e21d73..9244715560 100644 --- a/esphome/components/shelly_dimmer/shelly_dimmer.cpp +++ b/esphome/components/shelly_dimmer/shelly_dimmer.cpp @@ -574,6 +574,7 @@ void ShellyDimmer::start_calibration() { this->set_brightness_no_transition_(1); // Init calibration data + this->calibration_data_.fill(0); this->calibration_measurements_.fill(0); this->calibration_measurement_cnt_ = 0; this->calibration_step_ = -20; @@ -646,11 +647,7 @@ void ShellyDimmer::complete_calibration_() { value = remap(value, min, max, 0.0f, 1.0f); } - if (this->rtc_.save(&this->calibration_data_)) { - ESP_LOGD(TAG, "Saved calibration to flash"); - } else { - ESP_LOGW(TAG, "Couldn't save calibration to flash"); - } + this->save_calibration_(); ESP_LOGD(TAG, "Finished calibration. Values:"); for (float value : this->calibration_data_) { @@ -659,6 +656,13 @@ void ShellyDimmer::complete_calibration_() { this->set_brightness_no_transition_(1); } +void ShellyDimmer::save_calibration_() { + if (this->rtc_.save(&this->calibration_data_)) { + ESP_LOGD(TAG, "Saved calibration to flash"); + } else { + ESP_LOGW(TAG, "Couldn't save calibration to flash"); + } +} void ShellyDimmer::set_brightness_no_transition_(float brightness) { auto call = this->state_->make_call(); call.set_brightness(brightness); @@ -666,6 +670,10 @@ void ShellyDimmer::set_brightness_no_transition_(float brightness) { call.set_state(true); call.perform(); } +void ShellyDimmer::clear_calibration() { + this->calibration_data_.fill(0); + this->save_calibration_(); +} } // namespace shelly_dimmer } // namespace esphome diff --git a/esphome/components/shelly_dimmer/shelly_dimmer.h b/esphome/components/shelly_dimmer/shelly_dimmer.h index ec6a92a862..358cd7da4a 100644 --- a/esphome/components/shelly_dimmer/shelly_dimmer.h +++ b/esphome/components/shelly_dimmer/shelly_dimmer.h @@ -50,8 +50,12 @@ class ShellyDimmer : public PollingComponent, public light::LightOutput, public void set_voltage_sensor(sensor::Sensor *voltage_sensor) { this->voltage_sensor_ = voltage_sensor; } void set_current_sensor(sensor::Sensor *current_sensor) { this->current_sensor_ = current_sensor; } + /// Starts the calibration process. void start_calibration(); + /// Clears calibration values. + void clear_calibration(); + protected: GPIOPin *pin_nrst_; GPIOPin *pin_boot0_; @@ -132,10 +136,13 @@ class ShellyDimmer : public PollingComponent, public light::LightOutput, public /// Complete a single calibration step averaging over accumulated measurements. void complete_calibration_step_(); - // Complete the whole calibration process. + /// Complete the whole calibration process. void complete_calibration_(); - // Set brightness with no transition during calibration. + /// Saves calibration values to flash. + void save_calibration_(); + + /// Set brightness with no transition during calibration. void set_brightness_no_transition_(float brightness); };