Fix update sequence when update is set to false (#5225) (#7407)

This commit is contained in:
Niclas Larsson 2024-10-13 22:17:37 +02:00 committed by GitHub
parent 77d0bfc4bb
commit 39e922580a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 34 deletions

View File

@ -64,46 +64,46 @@ uint16_t shelly_dimmer_checksum(const uint8_t *buf, int len) {
return std::accumulate<decltype(buf), uint16_t>(buf, buf + len, 0); return std::accumulate<decltype(buf), uint16_t>(buf, buf + len, 0);
} }
bool ShellyDimmer::is_running_configured_version() const {
return this->version_major_ == USE_SHD_FIRMWARE_MAJOR_VERSION &&
this->version_minor_ == USE_SHD_FIRMWARE_MINOR_VERSION;
}
void ShellyDimmer::handle_firmware() {
// Reset the STM32 and check the firmware version.
this->reset_normal_boot_();
this->send_command_(SHELLY_DIMMER_PROTO_CMD_VERSION, nullptr, 0);
ESP_LOGI(TAG, "STM32 current firmware version: %d.%d, desired version: %d.%d", this->version_major_,
this->version_minor_, USE_SHD_FIRMWARE_MAJOR_VERSION, USE_SHD_FIRMWARE_MINOR_VERSION);
if (!is_running_configured_version()) {
#ifdef USE_SHD_FIRMWARE_DATA
if (!this->upgrade_firmware_()) {
ESP_LOGW(TAG, "Failed to upgrade firmware");
this->mark_failed();
return;
}
this->reset_normal_boot_();
this->send_command_(SHELLY_DIMMER_PROTO_CMD_VERSION, nullptr, 0);
if (!is_running_configured_version()) {
ESP_LOGE(TAG, "STM32 firmware upgrade already performed, but version is still incorrect");
this->mark_failed();
return;
}
#else
ESP_LOGW(TAG, "Firmware version mismatch, put 'update: true' in the yaml to flash an update.");
#endif
}
}
void ShellyDimmer::setup() { void ShellyDimmer::setup() {
this->pin_nrst_->setup(); this->pin_nrst_->setup();
this->pin_boot0_->setup(); this->pin_boot0_->setup();
ESP_LOGI(TAG, "Initializing Shelly Dimmer..."); ESP_LOGI(TAG, "Initializing Shelly Dimmer...");
// Reset the STM32 and check the firmware version. this->handle_firmware();
for (int i = 0; i < 2; i++) {
this->reset_normal_boot_();
this->send_command_(SHELLY_DIMMER_PROTO_CMD_VERSION, nullptr, 0);
ESP_LOGI(TAG, "STM32 current firmware version: %d.%d, desired version: %d.%d", this->version_major_,
this->version_minor_, USE_SHD_FIRMWARE_MAJOR_VERSION, USE_SHD_FIRMWARE_MINOR_VERSION);
if (this->version_major_ != USE_SHD_FIRMWARE_MAJOR_VERSION ||
this->version_minor_ != USE_SHD_FIRMWARE_MINOR_VERSION) {
#ifdef USE_SHD_FIRMWARE_DATA
// Update firmware if needed.
ESP_LOGW(TAG, "Unsupported STM32 firmware version, flashing");
if (i > 0) {
// Upgrade was already performed but the reported version is still not right.
ESP_LOGE(TAG, "STM32 firmware upgrade already performed, but version is still incorrect");
this->mark_failed();
return;
}
if (!this->upgrade_firmware_()) {
ESP_LOGW(TAG, "Failed to upgrade firmware");
this->mark_failed();
return;
}
// Firmware upgrade completed, do the checks again.
continue;
#else
ESP_LOGW(TAG, "Firmware version mismatch, put 'update: true' in the yaml to flash an update.");
this->mark_failed();
return;
#endif
}
break;
}
this->send_settings_(); this->send_settings_();
// Do an immediate poll to refresh current state. // Do an immediate poll to refresh current state.

View File

@ -20,6 +20,8 @@ class ShellyDimmer : public PollingComponent, public light::LightOutput, public
public: public:
float get_setup_priority() const override { return setup_priority::LATE; } float get_setup_priority() const override { return setup_priority::LATE; }
bool is_running_configured_version() const;
void handle_firmware();
void setup() override; void setup() override;
void update() override; void update() override;
void dump_config() override; void dump_config() override;