From 528d3672b499650185427297cfb7399fedd8fcf4 Mon Sep 17 00:00:00 2001
From: Edward Firmo <94725493+edwardtfn@users.noreply.github.com>
Date: Mon, 13 Jan 2025 05:11:48 +0100
Subject: [PATCH] [psram] Improve total PSRAM display in logs by using rounded
 KB values (#8008)

Co-authored-by: Djordje Mandic <6750655+DjordjeMandic@users.noreply.github.com>
---
 esphome/components/psram/psram.cpp            | 9 ++++++++-
 tests/components/psram/test.esp32-s2-ard.yaml | 1 +
 tests/components/psram/test.esp32-s2-idf.yaml | 1 +
 tests/components/psram/test.esp32-s3-ard.yaml | 1 +
 tests/components/psram/test.esp32-s3-idf.yaml | 1 +
 5 files changed, 12 insertions(+), 1 deletion(-)
 create mode 100644 tests/components/psram/test.esp32-s2-ard.yaml
 create mode 100644 tests/components/psram/test.esp32-s2-idf.yaml
 create mode 100644 tests/components/psram/test.esp32-s3-ard.yaml
 create mode 100644 tests/components/psram/test.esp32-s3-idf.yaml

diff --git a/esphome/components/psram/psram.cpp b/esphome/components/psram/psram.cpp
index 68d8dfd697..d9a5bd101f 100644
--- a/esphome/components/psram/psram.cpp
+++ b/esphome/components/psram/psram.cpp
@@ -21,7 +21,14 @@ void PsramComponent::dump_config() {
   ESP_LOGCONFIG(TAG, "  Available: %s", YESNO(available));
 #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 1, 0)
   if (available) {
-    ESP_LOGCONFIG(TAG, "  Size: %d KB", heap_caps_get_total_size(MALLOC_CAP_SPIRAM) / 1024);
+    const size_t psram_total_size_bytes = heap_caps_get_total_size(MALLOC_CAP_SPIRAM);
+    const float psram_total_size_kb = psram_total_size_bytes / 1024.0f;
+
+    if (abs(std::round(psram_total_size_kb) - psram_total_size_kb) < 0.05f) {
+      ESP_LOGCONFIG(TAG, "  Size: %.0f KB", psram_total_size_kb);
+    } else {
+      ESP_LOGCONFIG(TAG, "  Size: %zu bytes", psram_total_size_bytes);
+    }
   }
 #endif
 }
diff --git a/tests/components/psram/test.esp32-s2-ard.yaml b/tests/components/psram/test.esp32-s2-ard.yaml
new file mode 100644
index 0000000000..dade44d145
--- /dev/null
+++ b/tests/components/psram/test.esp32-s2-ard.yaml
@@ -0,0 +1 @@
+<<: !include common.yaml
diff --git a/tests/components/psram/test.esp32-s2-idf.yaml b/tests/components/psram/test.esp32-s2-idf.yaml
new file mode 100644
index 0000000000..dade44d145
--- /dev/null
+++ b/tests/components/psram/test.esp32-s2-idf.yaml
@@ -0,0 +1 @@
+<<: !include common.yaml
diff --git a/tests/components/psram/test.esp32-s3-ard.yaml b/tests/components/psram/test.esp32-s3-ard.yaml
new file mode 100644
index 0000000000..dade44d145
--- /dev/null
+++ b/tests/components/psram/test.esp32-s3-ard.yaml
@@ -0,0 +1 @@
+<<: !include common.yaml
diff --git a/tests/components/psram/test.esp32-s3-idf.yaml b/tests/components/psram/test.esp32-s3-idf.yaml
new file mode 100644
index 0000000000..dade44d145
--- /dev/null
+++ b/tests/components/psram/test.esp32-s3-idf.yaml
@@ -0,0 +1 @@
+<<: !include common.yaml