diff --git a/esphome/core/__init__.py b/esphome/core/__init__.py index 983b844ced..bc9a070e83 100644 --- a/esphome/core/__init__.py +++ b/esphome/core/__init__.py @@ -703,7 +703,7 @@ class EsphomeCore: def platformio_cache_dir(self) -> str: """Get the PlatformIO cache directory path.""" # Check if running in Docker/HA addon with custom cache dir - if cache_dir := os.environ.get("PLATFORMIO_CACHE_DIR"): + if (cache_dir := os.environ.get("PLATFORMIO_CACHE_DIR")) and cache_dir.strip(): return cache_dir # Default PlatformIO cache location return os.path.expanduser("~/.platformio/.cache") diff --git a/tests/unit_tests/test_core.py b/tests/unit_tests/test_core.py index b36bc8f4c0..4677140ad2 100644 --- a/tests/unit_tests/test_core.py +++ b/tests/unit_tests/test_core.py @@ -687,6 +687,14 @@ class TestEsphomeCore: expected = os.path.expanduser("~/.platformio/.cache") assert target.platformio_cache_dir == expected + def test_platformio_cache_dir_whitespace_env_var(self): + """Test platformio_cache_dir with whitespace-only env var falls back to default.""" + target = core.EsphomeCore() + + with patch.dict(os.environ, {"PLATFORMIO_CACHE_DIR": " "}): + expected = os.path.expanduser("~/.platformio/.cache") + assert target.platformio_cache_dir == expected + def test_platformio_cache_dir_docker_addon_path(self): """Test platformio_cache_dir in Docker/HA addon environment.""" target = core.EsphomeCore()