@@ -932,6 +932,7 @@ void loop() {
932932 JsonDocument mqttDoc;
933933 mqttDoc[" installed_version" ] = VERSION ;
934934 mqttDoc[" latest_version" ] = VERSION ;
935+ mqttDoc[" in_progress" ] = false ;
935936 char topic[MQTT_TOPIC_UPDATE_STATE_PATTERN_LENGTH +1 ];
936937 snprintf (topic, sizeof (topic), MQTT_TOPIC_UPDATE_STATE_PATTERN , deviceIdentity.data .uuid );
937938 mqttClient.beginPublish (topic, measureJson (mqttDoc), true );
@@ -3594,66 +3595,6 @@ void setup_OtaFirmware(){
35943595 }
35953596 }
35963597
3597- otaFirmware.onProgress ([](const char * partition, size_t written, size_t total){
3598- _otaUpdateInProcess = true ;
3599- if (strcmp (partition, _otaCurrentPartition) != 0 ){
3600- strlcpy (_otaCurrentPartition, partition, sizeof (_otaCurrentPartition));
3601- _otaLastPublishedPercentage = -1 ;
3602- oled.setOTAPartition (partition);
3603- oled.setPage (managerOled::PAGE_OTA_IN_PROGRESS );
3604- char msg[OLED_CHARACTERS_PER_LINE +1 ];
3605- snprintf (msg, sizeof (msg), " OTA %s update start" , partition);
3606- eventLog.createEvent (msg, EventLog::LOG_LEVEL_INFO );
3607- }
3608- oled.setProgressBar ((float )written / (float )total);
3609- int pct = (int )((float )written / (float )total * 100 );
3610- if (pct != _otaLastPublishedPercentage && deviceIdentity.enabled && mqttClient.connected ()){
3611- _otaLastPublishedPercentage = pct;
3612- JsonDocument mqttDoc;
3613- mqttDoc[" installed_version" ] = VERSION ;
3614- mqttDoc[" latest_version" ] = _otaLatestVersion;
3615- if (strlen (_otaReleaseUrl) > 0 ){ mqttDoc[" release_url" ] = _otaReleaseUrl; }
3616- mqttDoc[" in_progress" ] = true ;
3617- mqttDoc[" update_percentage" ] = pct;
3618- char title[32 ];
3619- snprintf (title, sizeof (title), " Updating %s" , _otaCurrentPartition);
3620- mqttDoc[" title" ] = title;
3621- char topic[MQTT_TOPIC_UPDATE_STATE_PATTERN_LENGTH +1 ];
3622- snprintf (topic, sizeof (topic), MQTT_TOPIC_UPDATE_STATE_PATTERN , deviceIdentity.data .uuid );
3623- char buffer[384 ];
3624- serializeJson (mqttDoc, buffer, sizeof (buffer));
3625- mqttClient.publish (topic, buffer);
3626- }
3627- mqttClient.loop ();
3628- });
3629-
3630- otaFirmware.onPartitionComplete ([](const char * partition, bool success){
3631- char msg[OLED_CHARACTERS_PER_LINE +1 ];
3632- snprintf (msg, sizeof (msg), " OTA %s %s" , partition, success ? " finished" : " failed" );
3633- eventLog.createEvent (msg, success ? EventLog::LOG_LEVEL_INFO : EventLog::LOG_LEVEL_NOTIFICATION );
3634- });
3635-
3636- otaFirmware.onComplete ([](bool success){
3637- _otaUpdateInProcess = false ;
3638- _otaPendingRequest = false ;
3639-
3640- if (mqttClient.connected ()){
3641- char topic[MQTT_TOPIC_UPDATE_STATE_PATTERN_LENGTH +1 ];
3642- snprintf (topic, sizeof (topic), MQTT_TOPIC_UPDATE_STATE_PATTERN , deviceIdentity.data .uuid );
3643- JsonDocument mqttDoc;
3644- mqttDoc[" in_progress" ] = false ;
3645- char buffer[256 ];
3646- serializeJson (mqttDoc, buffer);
3647- mqttClient.publish (topic, buffer);
3648- }
3649-
3650- if (success){
3651- eventLog.createEvent (" Rebooting..." , EventLog::LOG_LEVEL_NOTIFICATION );
3652- delay (5000 );
3653- ESP .restart ();
3654- }
3655- });
3656-
36573598 otaFirmware.onAvailable ([](const char * version, const char * app, const char * releaseUrl){
36583599 strlcpy (_otaLatestVersion, version, sizeof (_otaLatestVersion));
36593600 strlcpy (_otaReleaseUrl, releaseUrl ? releaseUrl : " " , sizeof (_otaReleaseUrl));
@@ -3702,7 +3643,7 @@ void setup_OtaFirmware(){
37023643/* *
37033644 * Executes a pending OTA update. If _otaPendingDoc is populated the update was
37043645 * triggered via the HTTP API (forced); otherwise it was triggered from HA via MQTT.
3705- * Both paths share the same callbacks registered in setup_OtaFirmware() .
3646+ * Callbacks are registered fresh here for every execution so both paths behave identically .
37063647*/
37073648void otaFirmware_checkPending (){
37083649
@@ -3747,6 +3688,7 @@ void otaFirmware_checkPending(){
37473688 mqttDoc[" latest_version" ] = _otaLatestVersion;
37483689 if (strlen (_otaReleaseUrl) > 0 ){ mqttDoc[" release_url" ] = _otaReleaseUrl; }
37493690 mqttDoc[" in_progress" ] = true ;
3691+ mqttDoc[" update_percentage" ] = 0 ;
37503692 char topic[MQTT_TOPIC_UPDATE_STATE_PATTERN_LENGTH +1 ];
37513693 snprintf (topic, sizeof (topic), MQTT_TOPIC_UPDATE_STATE_PATTERN , deviceIdentity.data .uuid );
37523694 mqttClient.beginPublish (topic, measureJson (mqttDoc), true );
@@ -3756,6 +3698,68 @@ void otaFirmware_checkPending(){
37563698 mqttClient.endPublish ();
37573699 }
37583700
3701+ otaFirmware.onProgress ([](const char * partition, size_t written, size_t total){
3702+ _otaUpdateInProcess = true ;
3703+ if (strcmp (partition, _otaCurrentPartition) != 0 ){
3704+ strlcpy (_otaCurrentPartition, partition, sizeof (_otaCurrentPartition));
3705+ _otaLastPublishedPercentage = -1 ;
3706+ oled.setOTAPartition (partition);
3707+ oled.setPage (managerOled::PAGE_OTA_IN_PROGRESS );
3708+ char msg[OLED_CHARACTERS_PER_LINE +1 ];
3709+ snprintf (msg, sizeof (msg), " OTA %s update start" , partition);
3710+ eventLog.createEvent (msg, EventLog::LOG_LEVEL_INFO );
3711+ }
3712+ oled.setProgressBar ((float )written / (float )total);
3713+ int pct = (int )((float )written / (float )total * 100 );
3714+ if (pct != _otaLastPublishedPercentage && deviceIdentity.enabled && mqttClient.connected ()){
3715+ _otaLastPublishedPercentage = pct;
3716+ JsonDocument mqttDoc;
3717+ mqttDoc[" installed_version" ] = VERSION ;
3718+ mqttDoc[" latest_version" ] = _otaLatestVersion;
3719+ if (strlen (_otaReleaseUrl) > 0 ){ mqttDoc[" release_url" ] = _otaReleaseUrl; }
3720+ mqttDoc[" in_progress" ] = true ;
3721+ mqttDoc[" update_percentage" ] = pct;
3722+ char title[32 ];
3723+ snprintf (title, sizeof (title), " Updating %s" , _otaCurrentPartition);
3724+ mqttDoc[" title" ] = title;
3725+ char topic[MQTT_TOPIC_UPDATE_STATE_PATTERN_LENGTH +1 ];
3726+ snprintf (topic, sizeof (topic), MQTT_TOPIC_UPDATE_STATE_PATTERN , deviceIdentity.data .uuid );
3727+ char buffer[384 ];
3728+ serializeJson (mqttDoc, buffer, sizeof (buffer));
3729+ mqttClient.publish (topic, buffer);
3730+ }
3731+ mqttClient.loop ();
3732+ });
3733+
3734+ otaFirmware.onPartitionComplete ([](const char * partition, bool success){
3735+ char msg[OLED_CHARACTERS_PER_LINE +1 ];
3736+ snprintf (msg, sizeof (msg), " OTA %s %s" , partition, success ? " finished" : " failed" );
3737+ eventLog.createEvent (msg, success ? EventLog::LOG_LEVEL_INFO : EventLog::LOG_LEVEL_NOTIFICATION );
3738+ });
3739+
3740+ otaFirmware.onComplete ([](bool success){
3741+ _otaUpdateInProcess = false ;
3742+ _otaPendingRequest = false ;
3743+
3744+ if (mqttClient.connected ()){
3745+ char topic[MQTT_TOPIC_UPDATE_STATE_PATTERN_LENGTH +1 ];
3746+ snprintf (topic, sizeof (topic), MQTT_TOPIC_UPDATE_STATE_PATTERN , deviceIdentity.data .uuid );
3747+ JsonDocument mqttDoc;
3748+ mqttDoc[" in_progress" ] = false ;
3749+ mqttClient.beginPublish (topic, measureJson (mqttDoc), true );
3750+ BufferingPrint bufferedClient (mqttClient, 32 );
3751+ serializeJson (mqttDoc, bufferedClient);
3752+ bufferedClient.flush ();
3753+ mqttClient.endPublish ();
3754+ }
3755+
3756+ if (success){
3757+ eventLog.createEvent (" Rebooting..." , EventLog::LOG_LEVEL_NOTIFICATION );
3758+ delay (5000 );
3759+ ESP .restart ();
3760+ }
3761+ });
3762+
37593763 if (isForced){
37603764 otaFirmware.execOTA (_otaPendingDoc);
37613765 _otaPendingDoc.clear ();
0 commit comments