diff --git a/esphome/components/update/update_entity.cpp b/esphome/components/update/update_entity.cpp index 515e4c2c18..7edea2fe22 100644 --- a/esphome/components/update/update_entity.cpp +++ b/esphome/components/update/update_entity.cpp @@ -2,12 +2,21 @@ #include "esphome/core/defines.h" #include "esphome/core/controller_registry.h" #include "esphome/core/log.h" +#include "esphome/core/progmem.h" namespace esphome { namespace update { static const char *const TAG = "update"; +// Update state strings indexed by UpdateState enum (0-3): UNKNOWN, NO UPDATE, UPDATE AVAILABLE, INSTALLING +PROGMEM_STRING_TABLE(UpdateStateStrings, "UNKNOWN", "NO UPDATE", "UPDATE AVAILABLE", "INSTALLING"); + +const LogString *update_state_to_string(UpdateState state) { + return UpdateStateStrings::get_log_str(static_cast(state), + static_cast(UpdateState::UPDATE_STATE_UNKNOWN)); +} + void UpdateEntity::publish_state() { ESP_LOGD(TAG, "'%s' >>\n" diff --git a/esphome/components/update/update_entity.h b/esphome/components/update/update_entity.h index 8eba78b44b..405346bee4 100644 --- a/esphome/components/update/update_entity.h +++ b/esphome/components/update/update_entity.h @@ -27,6 +27,8 @@ enum UpdateState : uint8_t { UPDATE_STATE_INSTALLING, }; +const LogString *update_state_to_string(UpdateState state); + class UpdateEntity : public EntityBase, public EntityBase_DeviceClass { public: void publish_state(); diff --git a/esphome/components/web_server/web_server.cpp b/esphome/components/web_server/web_server.cpp index 42219f3aac..dfd602be6b 100644 --- a/esphome/components/web_server/web_server.cpp +++ b/esphome/components/web_server/web_server.cpp @@ -29,6 +29,10 @@ #include "esphome/components/climate/climate.h" #endif +#ifdef USE_UPDATE +#include "esphome/components/update/update_entity.h" +#endif + #ifdef USE_WATER_HEATER #include "esphome/components/water_heater/water_heater.h" #endif @@ -2104,19 +2108,6 @@ std::string WebServer::event_json_(event::Event *obj, StringRef event_type, Json #endif #ifdef USE_UPDATE -static const LogString *update_state_to_string(update::UpdateState state) { - switch (state) { - case update::UPDATE_STATE_NO_UPDATE: - return LOG_STR("NO UPDATE"); - case update::UPDATE_STATE_AVAILABLE: - return LOG_STR("UPDATE AVAILABLE"); - case update::UPDATE_STATE_INSTALLING: - return LOG_STR("INSTALLING"); - default: - return LOG_STR("UNKNOWN"); - } -} - void WebServer::on_update(update::UpdateEntity *obj) { this->events_.deferrable_send_state(obj, "state", update_state_json_generator); } @@ -2158,7 +2149,7 @@ std::string WebServer::update_json_(update::UpdateEntity *obj, JsonDetail start_ JsonObject root = builder.root(); char buf[PSTR_LOCAL_SIZE]; - set_json_icon_state_value(root, obj, "update", PSTR_LOCAL(update_state_to_string(obj->state)), + set_json_icon_state_value(root, obj, "update", PSTR_LOCAL(update::update_state_to_string(obj->state)), obj->update_info.latest_version, start_config); if (start_config == DETAIL_ALL) { root[ESPHOME_F("current_version")] = obj->update_info.current_version;